Cod sursa(job #326363)

Utilizator levap1506Gutu Pavel levap1506 Data 24 iunie 2009 19:51:39
Problema Matrice 2 Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.62 kb
program matrice2;
 type warez=array[0..160,0..301,0..301] of shortint;
 var a,b:text;
  i,j,k,n,q,x1,y1,x2,y2,lmax:longint;
  z:array[1..300,1..300] of longint;
  yy:warez;
  procedure longest(x,y,lmin:longint;pas:integer);
  var i,j,qq:integer;
   begin
      yy[pas]:=yy[pas-1];
      yy[pas,x,y]:=1;
     if (x=x2) and (y=y2) then
      begin
       if lmin>lmax then lmax:=lmin;
       for i:=1 to n do
        for j:=1 to n do
         for qq:=1 to pas do
         if z[i,j]=lmax then yy[qq,i,j]:=1;
       exit;
      end;
      if lmin<lmax then exit;
      if (x>1) and (yy[pas,x-1,y]=0) then
        begin
          if z[x-1,y]<lmin then longest(x-1,y,z[x-1,y],pas+1) else longest(x-1,y,lmin,pas+1);
        end;
      if (y>1) and (yy[pas,x,y-1]=0) then
        begin
          if z[x,y-1]<lmin then longest(x,y-1,z[x,y-1],pas+1) else longest(x,y-1,lmin,pas+1);
        end;
      if (x<n) and (yy[pas,x+1,y]=0) then
        begin
          if z[x+1,y]<lmin then longest(x+1,y,z[x+1,y],pas+1) else longest(x+1,y,lmin,pas+1);
        end;
      if (y<n) and (yy[pas,x,y+1]=0) then
        begin
          if z[x,y+1]<lmin then longest(x,y+1,z[x,y+1],pas+1) else longest(x,y+1,lmin,pas+1);
        end;
   end;

  begin
   assign(a,'matrice2.in');
   assign(b,'matrice2.out');
   reset(a);
   rewrite(b);
   readln(a,n,q);
   for i:=1 to n do
    begin
     for j:=1 to n do
       Read(a,z[i,j]);
     Readln(a);
    end;
   for i:=1 to q do
    begin
     lmax:=-maxlongint;
     Readln(a,x1,y1,x2,y2);
     longest(x1,y1,z[x1,y1],1);
     Writeln(b,lmax);
    end;
    close(b);

  end.