Cod sursa(job #61183)

Utilizator ProtomanAndrei Purice Protoman Data 18 mai 2007 15:58:39
Problema Castel Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.74 kb
var a,b:array[0..151,0..151] of integer;
    vcx,vcy:array[1..22500] of longint;
    i,j,n,m,x,y,h,k,nr:longint;
    f1,f2:text;
    ok:boolean;
    v:array[1..22500] of longint;

procedure vecini(n1,n2:longint);
begin
        if v[a[n1,n2]]=1 then
        begin
                if b[n1,n2]=0 then
                begin
                        ok:=true;
                        inc(h);
                        vcx[h]:=n1;
                        vcy[h]:=n2;
                        b[n1,n2]:=1;
                        nr:=(n1-1)*m+n2;
                        v[nr]:=1;
                end;
        end;
end;

begin
        assign(f1,'castel.in');
        reset(f1);
        assign(f2,'castel.out');
        rewrite(f2);
        read(f1,n,m,k);
        for i:=1 to n do
                for j:=1 to m do
                        read(f1,a[i,j]);
        for i:=1 to n do
        begin
                a[i,0]:=-1;
                a[i,m+1]:=-1;
        end;
        for i:=1 to m do
        begin
                a[0,i]:=-1;
                a[n+1,i]:=-1;
        end;
        x:=(k-1+m) div m;
        y:=k mod m;
        if y=0 then y:=m;
        vcx[1]:=x;
        vcy[1]:=y;
        b[x,y]:=1;
        h:=1;
        v[k]:=1;
        ok:=true;
        while ok=true do
        begin
                ok:=false;
                i:=0;
                while i<h do
                begin
                        inc(i);
                        vecini(vcx[i]-1,vcy[i]);
                        vecini(vcx[i]+1,vcy[i]);
                        vecini(vcx[i],vcy[i]+1);
                        vecini(vcx[i],vcy[i]-1);
                end;
        end;
        writeln(f2,h);
        close(f1);
        close(f2);
end.