Cod sursa(job #875415)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 10 februarie 2013 00:29:58
Problema Rj Scor 50
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.97 kb
program romeo_si_julieta2004;
type matrice=array[0..101,0..101] of integer;
var  a,b:matrice; tmin:integer; i,j,n,m,xi,yi:byte;
     x:char; ok:boolean;
     f,g:text;
begin
assign(f,'rj.in');reset(f);
assign(g,'rj.out');rewrite(g);
readln(f,n,m);
for i:=1 to n do begin
  for j:=1 to m do begin
                   read(f,x);
                   if x='X' then begin
                                 a[i,j]:=-1; b[i,j]:=-1;
                                 end
                      else if x=' ' then begin
                                         a[i,j]:=0; b[i,j]:=0;
                                         end
                              else if x='R' then a[i,j]:=1
                                      else if x='J' then b[i,j]:=1;
                   end;
                 readln(f);
                 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;
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;
ok:=true;
while ok do begin
            ok:=false;
            for i:=1 to n do
             for j:=1 to m do if (a[i,j]>0) then
             begin
             if a[i-1,j]=0 then begin
                                a[i-1,j]:=a[i,j]+1;ok:=true;
                                end;
             if a[i-1,j-1]=0 then begin
                                a[i-1,j-1]:=a[i,j]+1;ok:=true;
                                end;
             if a[i-1,j+1]=0 then begin
                                a[i-1,j+1]:=a[i,j]+1;ok:=true;
                                end;
             if a[i,j-1]=0 then begin
                                a[i,j-1]:=a[i,j]+1;ok:=true;
                                end;
             if a[i,j+1]=0 then begin
                                a[i,j+1]:=a[i,j]+1;ok:=true;
                                end;
             if a[i+1,j-1]=0 then begin
                                a[i+1,j-1]:=a[i,j]+1;ok:=true;
                                end;
             if a[i+1,j]=0 then begin
                                a[i+1,j]:=a[i,j]+1;ok:=true;
                                end;
             if a[i+1,j+1]=0 then begin
                                a[i+1,j+1]:=a[i,j]+1;ok:=true;
                                end;
             end;
            end;
ok:=true;
while ok do begin
            ok:=false;
            for i:=1 to n do
             for j:=1 to m do if (b[i,j]>0) then
             begin
             if b[i-1,j]=0 then begin
                                b[i-1,j]:=b[i,j]+1;ok:=true;
                                end;
             if b[i-1,j-1]=0 then begin
                                b[i-1,j-1]:=b[i,j]+1;ok:=true;
                                end;
             if b[i-1,j+1]=0 then begin
                                b[i-1,j+1]:=b[i,j]+1;ok:=true;
                                end;
             if b[i,j-1]=0 then begin
                                b[i,j-1]:=b[i,j]+1;ok:=true;
                                end;
             if b[i,j+1]=0 then begin
                                b[i,j+1]:=b[i,j]+1;ok:=true;
                                end;
             if b[i+1,j-1]=0 then begin
                                b[i+1,j-1]:=b[i,j]+1;ok:=true;
                                end;
             if b[i+1,j]=0 then begin
                                b[i+1,j]:=b[i,j]+1;ok:=true;
                                end;
             if b[i+1,j+1]=0 then begin
                                b[i+1,j+1]:=b[i,j]+1; ok:=true;
                                end;
             end;
            end;
tmin:=32000;
for i:=1 to n do
 for j:=1 to m do
  if (a[i,j]=b[i,j])and(a[i,j]<>-1)and(a[i,j]<>0)then
     begin
     if a[i,j]<tmin then begin
                         tmin:=a[i,j]; xi:=i; yi:=j;
                         end;
     end;
write(g,tmin,' ',xi,' ',yi);
close(f); close(g);
end.