Cod sursa(job #197642)

Utilizator matemariaescuMaria Mateescu matemariaescu Data 5 iulie 2008 12:56:14
Problema Gropi Scor 0
Compilator fpc Status done
Runda Junior Challenge 2008 Marime 1.77 kb
program gropi;
type vector = array[0..3,0..1001] of integer;
var oras:vector;
    x,y,li,ci,ls,cs:longint;
    m,n,i,c:longint;

function lee(oras:vector):longint;
var Q:array [1..1000] of record
                           x,y,v:longint;
                         end;
    cc,lc,i,s:longint;
begin
   cc:=ci;
   lc:=li;
   q[1].x:=lc;
   q[1].y:=cc;
   q[1].v:=1;
   i:=1; s:=1;
   while s>=i do
     begin
       lc:=q[i].x;
       cc:=q[i].y;
       oras[lc,cc]:=q[i].v;
       i:=i+1;
       if oras[lc,cc+1]= 0 then
         begin
           s:=s+1;
           Q[s].v:=oras[lc,cc]+1;
           Q[s].x:=lc;
           Q[s].y:=cc+1;
         end;
       if oras[lc,cc-1]= 0 then
         begin
           s:=s+1;
           Q[s].v:=oras[lc,cc]+1;
           Q[s].x:=lc;
           Q[s].y:=cc-1;
         end;
       if oras[lc-1,cc]= 0 then
         begin
           s:=s+1;
           Q[s].v:=oras[lc,cc]+1;
           Q[s].x:=lc-1;
           Q[s].y:=cc;
         end;
       if oras[lc+1,cc]= 0 then
         begin
           s:=s+1;
           Q[s].v:=oras[lc,cc]+1;
           Q[s].x:=lc+1;
           Q[s].y:=cc;
         end;
       if (lc=ls) and(cc=cs) then
         break;
     end;
   lee:=oras[ls,cs];
end;

begin
  assign(input,'gropi.in'); reset(input);
  assign(output,'gropi.out'); rewrite(output);
  readln(c,n);
  for i := 1 to n do
    begin
      readln(x,y);
      oras[x,y]:=-1;
    end;
  for i := 1 to c do
     begin
       oras[0,i]:=-1;
       oras[3,i]:=-1;
     end;
  for i := 0 to 3 do
    begin
      oras[i,c+1]:=-1;
      oras[i,0]:=-1;
    end;
  readln(m);
  for i := 1 to m do
    begin
      readln(li,ci,ls,cs);
      writeln(lee(oras));
    end;
  close(input);
  close(output);
end.