Cod sursa(job #56785)

Utilizator ProtomanAndrei Purice Protoman Data 30 aprilie 2007 14:24:17
Problema Castel Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.62 kb
var a:array[0..151,0..151] of integer;
    b:array[0..151,0..151] of integer;
    i,j,n,m,xa,ya,xi,yi,nr,k:longint;
    ok,ok1:boolean;
    f1,f2:text;

procedure vecini(x,y:longint);
begin
        ok1:=false;
        xa:=(a[x,y]+m-1) div m;
        ya:=a[x,y] mod m;
        if ya=0 then ya:=m;
        if b[xa,ya]=1 then
        begin
                if b[x,y]=0 then ok:=false;
                b[x,y]:=1;
                ok1:=true;
        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;
        nr:=0;
        xi:=(k+m-1) div m;
        yi:=k mod m;
        if yi=0 then yi:=m;
        b[xi,yi]:=1;
        while ok=false do begin
                ok:=true;
                for i:=1 to n do
                begin
                        for j:=1 to m do
                        begin
                                if b[i,j]=1 then
                                begin
                                        vecini(i,j+1);
                                        vecini(i,j-1);
                                        vecini(i-1,j);
                                        vecini(i+1,j);
                                end;
                        end;
                end;
        end;
        for i:=1 to n do for j:=1 to m do if b[i,j]=1 then inc(nr);
        writeln(f2,nr);
        close(f1);
        close(f2);
end.