Cod sursa(job #197577)

Utilizator chelaru_t_achelaru traian andrei chelaru_t_a Data 5 iulie 2008 11:00:10
Problema Gropi Scor 40
Compilator fpc Status done
Runda Junior Challenge 2008 Marime 1.47 kb
type gropi=record
           x:1..2;
           y:longint;
           end;
     drum=record
          x1,x2:1..2;
          y1,y2:longint;
          end;
var c,n,m,i,t:longint;
    a:array [1..100000] of gropi;
    b:array [1..100000] of drum;
    f,g:text;

 procedure citire;
  var i:longint;
  begin
   readln(f,c,n);
   for i:=1 to n do readln(f,a[i].x,a[i].y);
   readln(f,m);
   for i:=1 to m do readln(f,b[i].x1,b[i].y1,b[i].x2,b[i].y2);
  end;

 function groapa(q:longint):boolean;
  var j:longint;
  begin
   groapa:=true;
   for j:=1 to n do
     if (a[j].x=b[i].x1) and (q=a[j].y) then groapa:=false;
  end;

begin
  assign(f,'gropi.in');
  assign(g,'gropi.out');
  reset(f);
  rewrite(g);
  citire;
  for i:=1 to m do
    begin
    t:=1;
    if b[i].y1<b[i].y2 then
      repeat
      if groapa(b[i].y1+1) then
        begin
        b[i].y1:=b[i].y1+1;
        t:=t+1;
        end
      else begin
           if b[i].x1=1 then b[i].x1:=2
           else b[i].x1:=1;
           t:=t+1;
           end;
      until b[i].y1=b[i].y2
    else repeat
         if groapa(b[i].y1-1) then
           begin
           b[i].y1:=b[i].y1-1;
           t:=t+1;
           end
         else begin
              if b[i].x1=1 then b[i].x1:=2
              else b[i].x1:=1;
              t:=t+1;
              end;
         until b[i].y1=b[i].y2;
    if b[i].x1<>b[i].x2 then t:=t+1;
    writeln(g,t);
    end;
  close(f);
  close(g);
end.