Cod sursa(job #88737)

Utilizator savimSerban Andrei Stan savim Data 3 octombrie 2007 17:56:02
Problema Castel Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.23 kb
var gas,i,j,k,m,nr,e,n,p1,p2,t:longint;
    poz,a,b,c:array[0..151,0..151] of longint;
    chei:array[1..22500] of longint;
    f1,f2:text;
begin
    assign(f1,'castel.in');
    assign(f2,'castel.out');
    reset(f1);
    rewrite(f2);
    readln(f1,m,n,k);

    for i:=1 to m do
        for j:=1 to n do
          read(f1,a[i][j]);

    for i:=1 to m do
        for j:=1 to n do
            b[i][j]:=0;
    for i:=1 to m do
    begin
        b[i][0]:=-1;
        b[i][n+1]:=-1;
    end;

    for j:=1 to n do
    begin
        b[0][j]:=-1;
        b[m+1][j]:=-1;
    end;
    nr:=0;
    for i:=1 to m do
        for j:=1 to n do
        begin
            nr:=nr+1;
            poz[i][j]:=nr;
        end;

    i:=1;j:=0;nr:=0;
    repeat
        if j=n then begin i:=i+1;j:=1;end
               else inc(j);
        inc(nr);
    until nr=k;


    gas:=1;
    e:=1;
    chei[e]:=a[i][j];
    b[i][j]:=1;
    while gas=1 do
    begin
        for i:=1 to m do
            for j:=1 to n do
                c[i][j]:=b[i][j];
        for i:=1 to m do
            for j:=1 to n do
            begin
                if (b[i-1][j]=0) then
                begin
                    gas:=0;
                    for t:=1 to e do
                        if a[i-1][j]=chei[t] then begin gas:=1;break;end;
                    if gas=1 then
                    begin
                        inc(e);
                        chei[e]:=poz[i-1][j];
                        b[i-1][j]:=1;
                    end;
                end;

                if (b[i][j+1]=0) then
                begin
                    gas:=0;
                    for t:=1 to e do
                        if a[i][j+1]=chei[t] then begin gas:=1;break;end;
                    if gas=1 then
                    begin
                        inc(e);
                        chei[e]:=poz[i][j+1];
                        b[i][j+1]:=1;
                    end;
                end;

                if (b[i][j-1]=0) then
                begin
                    gas:=0;
                    for t:=1 to e do
                        if a[i][j-1]=chei[t] then begin gas:=1;break;end;
                    if gas=1 then
                    begin
                        inc(e);
                        chei[e]:=poz[i][j-1];
                        b[i][j-1]:=1;
                    end;
                end;

                if (b[i+1][j]=0) then
                begin
                    gas:=0;
                    for t:=1 to e do
                        if a[i+1][j]=chei[t] then begin gas:=1;break;end;
                    if gas=1 then
                    begin
                        inc(e);
                        chei[e]:=poz[i+1][j];
                        b[i+1][j]:=1;
                    end;
                end;
            end;
            gas:=0;
            for i:=1 to m do
            begin
                for j:=1 to n do
                    if c[i][j]<>b[i][j] then begin gas:=1;break;end;
                if gas=1 then break;
            end;
    end;
    nr:=0;
    for i:=1 to m do
        for j:=1 to n do
            if b[i][j]=1 then inc(nr);
    writeln(f2,nr);

    close(f1);
    close(f2);
end.