Cod sursa(job #749749)

Utilizator Buzu_Tudor_RoCont vechi Buzu_Tudor_Ro Data 18 mai 2012 17:05:38
Problema Rj Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.53 kb
Program Romeo_dinamic;
type matrice=array[-2..101,-2..101] of integer;
var bufi,bufa : array[0..1 shl 25] of char;
    i,n,j,m,max : longint;
    a,b:matrice; c:char;
    l01,l02,c01,c02 : integer;

Procedure undanumerica(var a:matrice; l0,c0:integer);
var u,k:integer;
begin
    u:=1;
    for k:=1 to max do
      begin
      for i:=1 to n do
        for j:=1 to m do
           if a[i,j]=u then begin
                            if a[i-1,j]=0 then a[i-1,j]:=u+1; {Nord}
                            if a[i-1,j-1]=0 then a[i-1,j-1]:=u+1; {Nord-Vest}
                            if a[i-1,j+1]=0 then a[i-1,j+1]:=u+1; {Nord-Est}
                            if a[i+1,j]=0 then a[i+1,j]:=u+1; {Sud}
                            if a[i+1,j-1]=0 then a[i+1,j-1]:=u+1; {Sud-Vest}
                            if a[i+1,j+1]=0 then a[i+1,j+1]:=u+1; {Sud-Est}
                            if a[i,j-1]=0 then a[i,j-1]:=u+1; {Vst}
                            if a[i,j+1]=0 then a[i,j+1]:=u+1; {Est};
                            end;
      u:=u+1;
      end;
end;

Procedure tipar;
var i0,j0,tmin:integer;
begin
    tmin:=maxint;
    for i:=n downto 1 do
       for j:=m downto 1 do
          if (a[i,j]<>-1) and
              (a[i,j]=b[i,j]) then begin
                                   if (a[i,j]<=tmin) and
                                      (a[i,j]<>0) then begin
                                                       tmin:=a[i,j];
                                                       i0:=i;
                                                       j0:=j;
                                                       end;
                                   end;
    write(tmin,' ',i0,' ',j0);
end;

begin
    assign(input,'rj.in'); reset(input);
    assign(output,'rj.out'); rewrite(output);
    settextbuf(input,bufi); settextbuf(output,bufa);
    max:=1; readln(n,m);

    for i:=1 to n do
      begin
      for j:=1 to m do begin
                       read(c);
                       if c='X' then begin
                                     a[i,j]:=-1;
                                     b[i,j]:=-1;
                                     end
                                else if c='R' then begin
                                                   a[i,j]:=1;
                                                   l01:=i;
                                                   c01:=j;
                                                   max:=max+1;
                                                   end
                                              else if c='J' then begin
                                                                 b[i,j]:=1;
                                                                 l02:=i;
                                                                 c02:=j;
                                                                 max:=max+1;
                                                                 end
                                                            else max:=max+1;
                       end;
      readln;
      end;

      max:=max+1;

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

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

      undanumerica(a,l01,c01);
      undanumerica(b,l02,c02);

      tipar;

    close(input); close(output);
end.