Cod sursa(job #303642)

Utilizator punkistBarbulescu Dan punkist Data 10 aprilie 2009 07:07:29
Problema Castel Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.64 kb
var sx,sy,m,n,k,chei:longint;
    cheimax:longint;
    mat:array[1..150,1..150] of record
                                  cx,cy:byte;
                                end;
    viz:array[1..150,1..150] of longint;
    vizch:array[1..150,1..150] of longint;

procedure Citeste;
var f:text;
    i,j,a:longint;
begin
assign(f,'castel.in');
reset(f);
readln(f,m,n,k);
for i:=1 to m do
 begin
  for j:=1 to n do
   begin
    read(f,a);
    mat[i,j].cx:=a div n+1;
    mat[i,j].cy:=a mod n;
    if mat[i,j].cy=0 then
     begin
      mat[i,j].cy:=n;
      mat[i,j].cx:=mat[i,j].cx-1;
     end;
    viz[i,j]:=0;
   end;
 end;
close(f);
sy:=k mod n;
sx:=k div n+1;
if sy=0 then
 begin
  sy:=n;
  sx:=sx-1;
 end;
chei:=0;
cheimax:=0;
end;

function Posibil(x,y:byte):boolean;
 var xc,yc:longint;
     e:boolean;
 begin
  e:=false;
  if (x>0) and (y>0) and (x<=m) and (y<=n) then
   if vizch[x,y]<chei then
    if viz[mat[x,y].cx,mat[x,y].cy]>0 then
     e:=true;
  Posibil:=e;
 end;

procedure Parcurge(x,y:byte);
 var a:longint;
 begin
  if viz[x,y]=0 then chei:=chei+1;
  if chei>cheimax then cheimax:=chei;
  viz[x,y]:=viz[x,y]+1;
  a:=vizch[x,y];
  vizch[x,y]:=chei;
  if Posibil(x+1,y) then Parcurge(x+1,y);
  if Posibil(x-1,y) then Parcurge(x-1,y);
  if Posibil(x,y-1) then Parcurge(x,y-1);
  if Posibil(x,y+1) then Parcurge(x,y+1);
  viz[x,y]:=viz[x,y]-1;
  vizch[x,y]:=a;
  if viz[x,y]=0 then chei:=chei-1;
 end;

procedure Scrie;
 var i,j:longint;
     f:text;
 begin
  assign(f,'castel.out');
  rewrite(f);
  writeln(f,cheimax);
  close(f);
 end;

begin
Citeste;
Parcurge(sx,sy);
Scrie;
end.