Cod sursa(job #88732)

Utilizator raduzerRadu Zernoveanu raduzer Data 3 octombrie 2007 17:54:21
Problema Castel Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.44 kb
var b:array[0..30000]of integer;
    a,c:array[0..160,0..160]of integer;
    n,m,i,j,v,q,max:integer;

procedure back(i,j:integer);
begin
     if j=m then
     begin
          if b[(i-1)*m+j]=0 then inc(max);
          b[(i-1)*m+j]:=1;
     end
     else
     begin
          if b[(i-1)*m+j mod m]=0 then inc(max);
          b[(i-1)*m+j mod m]:=1;
     end;
     if (c[i-1][j]<2)and(b[a[i-1][j]]=1)and(i>1) then
     begin
          inc(c[i-1][j]);
          back(i-1,j);
          dec(c[i-1][j]);
     end;
     if (c[i][j+1]<2)and(b[a[i][j+1]]=1)and(j<m) then
     begin
          inc(c[i][j+1]);
          back(i,j+1);
          dec(c[i][j+1]);
     end;
     if (c[i+1][j]<2)and(b[a[i+1][j]]=1)and(i<n) then
     begin
          inc(c[i+1][j]);
          back(i+1,j);
          dec(c[i+1][j]);
     end;
     if (c[i][j-1]<2)and(b[a[i][j-1]]=1)and(j>1) then
     begin
          inc(c[i][j-1]);
          back(i,j-1);
          dec(c[i][j-1]);
     end;
end;

begin
     assign(input,'castel.in');
     reset(input);
     assign(output,'castel.out');
     rewrite(output);
     readln(n,m,v);
     for i:=1 to n do
     begin
          for j:=1 to m do
          begin
               read(a[i][j]);
          end;
          readln;
     end;
     q:=0;
     b[v]:=1;
     max:=1;
     if v mod n=0 then back(v div n,m)
                  else back(v div m+1,v mod m);
     writeln(max);
close(output);
end.