Cod sursa(job #56061)

Utilizator vanila0406Ionescu Victor vanila0406 Data 28 aprilie 2007 21:34:40
Problema Castel Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.78 kb
program castel;
type coord=record
        x,y:byte;
end;
const vx:array[1..4] of shortint=(0,1,0,-1);
      vy:array[1..4] of shortint=(1,0,-1,0);
var f,g:text;
        viz:array[1..100000] of boolean;
        m,n,k:longint;
        a:array[1..151,1..151] of integer;
        b,c:array[1..151,1..151] of boolean;
        co:array[1..30000] of coord;
        xi,yi:longint;
        ok:boolean;


procedure iofile;
var i,j:longint;
begin
        assign(f,'castel.in');
        reset(f);
        assign(g,'castel.out');
        rewrite(g);
        readln(f,m,n,k);
        xi:=k div n+1;
        if k mod n=0 then dec(xi);
        yi:=(k-1) mod n+1;
        for i:=1 to m do
                for j:=1 to n do
                        read(f,a[i,j]);
        fillchar(viz,sizeof(viz),false);
        fillchar(c,sizeof(c),false);
        c[xi,yi]:=true;
end;



procedure lee;
var p,u,i,l1,c1,ll,cc:longint;
begin
        fillchar(b,sizeof(b),false);
        b[xi,yi]:=true;
        p:=1;
        u:=1;
        co[p].x:=xi;
        co[p].y:=yi;
        viz[k]:=true;
        while p<=u do
                begin
                        for i:=1 to 4 do
                                begin
                                        l1:=co[p].x;
                                        c1:=co[p].y;
                                        ll:=l1+vx[i];
                                        cc:=c1+vy[i];
                                        if (ll>=1)and(ll<=m)and(cc>=1)and(cc<=n) then
                                                if not b[ll,cc] then
                                                if viz[a[ll,cc]] then
                                                begin
                                                        inc(u);
                                                        co[u].x:=ll;
                                                        co[u].y:=cc;
                                                        viz[(ll-1)*n+cc]:=true;
                                                        b[ll,cc]:=true;
                                                        if c[ll,cc]=false then
                                                        ok:=true;
                                                        c[ll,cc]:=true;
                                                end;
                                end;
                        inc(p);
                end;
end;



procedure prel;
var i,j:longint;
        nr:longint;
begin
        repeat
                ok:=false;
                lee;
        until not ok;
        nr:=0;
        for i:=1 to m do
                for j:=1 to n do
                        if c[i,j] then inc(nr);
        writeln(g,nr);
        close(g);
end;



begin
        iofile;
        prel;
end.