Cod sursa(job #80027)

Utilizator coderninuHasna Robert coderninu Data 25 august 2007 15:45:39
Problema Rj Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 7.14 kb
program romeosijulieta;
type matrice=array[1..100,1..100] of byte;
var x:array[1..100,1..100] of char;
    ro,ju:matrice;
    i,j,rx,ry,jx,jy,tmin,m,n,rezx,rezy,k:byte;
    fin,fout:text;

{--------------------------}
procedure citire;
begin
 assign(fin,'rj.in'); reset(fin);
 readln(fin,n,m);
 for i:=1 to n do
     begin
     for j:=1 to m do
         begin
         read(fin,x[i,j]);
         if x[i,j]='R' then begin rx:=i; ry:=j; end;
         if x[i,j]='J' then begin jx:=i; jy:=j; end;
         end;
     readln(fin);
     end;
 close(fin);
end;

{---------------------------}
function inside(a,b:byte):boolean;
begin
 inside:=(a>0) and (a<=n) and (b>0) and (b<=m);
end;

{-----------------------------}
procedure constr;
begin
 ro[rx,ry]:=1;
 for k:=1 to 6 do
 begin
 for i:=1 to n do
     for j:=1 to m do
         if ro[i,j]>0 then
             begin
             if inside(i-1,j-1) and (x[i-1,j-1]<>'X') then
                 if (ro[i-1,j-1]=0) or (ro[i,j]+1<ro[i-1,j-1]) then
                     ro[i-1,j-1]:=ro[i,j]+1;
             if inside(i-1,j) and (x[i-1,j]<>'X') then
                 if (ro[i-1,j]=0) or (ro[i,j]+1<ro[i-1,j]) then
                     ro[i-1,j]:=ro[i,j]+1;
             if inside(i-1,j+1) and (x[i-1,j+1]<>'X') then
                 if (ro[i-1,j+1]=0) or (ro[i,j]+1<ro[i-1,j+1]) then
                     ro[i-1,j+1]:=ro[i,j]+1;
             if inside(i,j+1) and (x[i,j+1]<>'X') then
                 if (ro[i,j+1]=0) or (ro[i,j]+1<ro[i,j+1]) then
                     ro[i,j+1]:=ro[i,j]+1;
             if inside(i+1,j+1) and (x[i+1,j+1]<>'X') then
                 if (ro[i+1,j+1]=0) or (ro[i,j]+1<ro[i+1,j+1]) then
                     ro[i+1,j+1]:=ro[i,j]+1;
             if inside(i+1,j) and (x[i+1,j]<>'X') then
                 if (ro[i+1,j]=0) or (ro[i,j]+1<ro[i+1,j]) then
                     ro[i+1,j]:=ro[i,j]+1;
             if inside(i+1,j-1) and (x[i+1,j-1]<>'X') then
                 if (ro[i+1,j-1]=0) or (ro[i,j]+1<ro[i+1,j-1]) then
                     ro[i+1,j-1]:=ro[i,j]+1;
             if inside(i,j-1) and (x[i,j-1]<>'X') then
                 if (ro[i,j-1]=0) or (ro[i,j]+1<ro[i,j-1]) then
                     ro[i,j-1]:=ro[i,j]+1;

             end;
 for i:=n downto 1 do
     for j:=m downto 1 do
         if ro[i,j]>0 then
             begin
             if inside(i-1,j-1) and (x[i-1,j-1]<>'X') then
                 if (ro[i-1,j-1]=0) or (ro[i,j]+1<ro[i-1,j-1]) then
                     ro[i-1,j-1]:=ro[i,j]+1;
             if inside(i-1,j) and (x[i-1,j]<>'X') then
                 if (ro[i-1,j]=0) or (ro[i,j]+1<ro[i-1,j]) then
                     ro[i-1,j]:=ro[i,j]+1;
             if inside(i-1,j+1) and (x[i-1,j+1]<>'X') then
                 if (ro[i-1,j+1]=0) or (ro[i,j]+1<ro[i-1,j+1]) then
                     ro[i-1,j+1]:=ro[i,j]+1;
             if inside(i,j+1) and (x[i,j+1]<>'X') then
                 if (ro[i,j+1]=0) or (ro[i,j]+1<ro[i,j+1]) then
                     ro[i,j+1]:=ro[i,j]+1;
             if inside(i+1,j+1) and (x[i+1,j+1]<>'X') then
                 if (ro[i+1,j+1]=0) or (ro[i,j]+1<ro[i+1,j+1]) then
                     ro[i+1,j+1]:=ro[i,j]+1;
             if inside(i+1,j) and (x[i+1,j]<>'X') then
                 if (ro[i+1,j]=0) or (ro[i,j]+1<ro[i+1,j]) then
                     ro[i+1,j]:=ro[i,j]+1;
             if inside(i+1,j-1) and (x[i+1,j-1]<>'X') then
                 if (ro[i+1,j-1]=0) or (ro[i,j]+1<ro[i+1,j-1]) then
                     ro[i+1,j-1]:=ro[i,j]+1;
             if inside(i,j-1) and (x[i,j-1]<>'X') then
                 if (ro[i,j-1]=0) or (ro[i,j]+1<ro[i,j-1]) then
                     ro[i,j-1]:=ro[i,j]+1;

             end;

 end;
end;

{-----------------------------}
procedure constj;
begin
 ju[jx,jy]:=1;
 for k:=1 to 6 do
 begin
 for i:=1 to n do
     for j:=1 to m do
         if ju[i,j]>0 then
             begin
             if inside(i-1,j-1) and (x[i-1,j-1]<>'X') then
                 if (ju[i-1,j-1]=0) or (ju[i,j]+1<ju[i-1,j-1]) then
                     ju[i-1,j-1]:=ju[i,j]+1;
             if inside(i-1,j) and (x[i-1,j]<>'X') then
                 if (ju[i-1,j]=0) or (ju[i,j]+1<ju[i-1,j]) then
                     ju[i-1,j]:=ju[i,j]+1;
             if inside(i-1,j+1) and (x[i-1,j+1]<>'X') then
                 if (ju[i-1,j+1]=0) or (ju[i,j]+1<ju[i-1,j+1]) then
                     ju[i-1,j+1]:=ju[i,j]+1;
             if inside(i,j+1) and (x[i,j+1]<>'X') then
                 if (ju[i,j+1]=0) or (ju[i,j]+1<ju[i,j+1]) then
                     ju[i,j+1]:=ju[i,j]+1;
             if inside(i+1,j+1) and (x[i+1,j+1]<>'X') then
                 if (ju[i+1,j+1]=0) or (ju[i,j]+1<ju[i+1,j+1]) then
                     ju[i+1,j+1]:=ju[i,j]+1;
             if inside(i+1,j) and (x[i+1,j]<>'X') then
                 if (ju[i+1,j]=0) or (ju[i,j]+1<ju[i+1,j]) then
                     ju[i+1,j]:=ju[i,j]+1;
             if inside(i+1,j-1) and (x[i+1,j-1]<>'X') then
                 if (ju[i+1,j-1]=0) or (ju[i,j]+1<ju[i+1,j-1]) then
                     ju[i+1,j-1]:=ju[i,j]+1;
             if inside(i,j-1) and (x[i,j-1]<>'X') then
                 if (ju[i,j-1]=0) or (ju[i,j]+1<ju[i,j-1]) then
                     ju[i,j-1]:=ju[i,j]+1;

             end;
 for i:=n downto 1 do
     for j:=m downto 1 do
         if ju[i,j]>0 then
             begin
             if inside(i-1,j-1) and (x[i-1,j-1]<>'X') then
                 if (ju[i-1,j-1]=0) or (ju[i,j]+1<ju[i-1,j-1]) then
                     ju[i-1,j-1]:=ju[i,j]+1;
             if inside(i-1,j) and (x[i-1,j]<>'X') then
                 if (ju[i-1,j]=0) or (ju[i,j]+1<ju[i-1,j]) then
                     ju[i-1,j]:=ju[i,j]+1;
             if inside(i-1,j+1) and (x[i-1,j+1]<>'X') then
                 if (ju[i-1,j+1]=0) or (ju[i,j]+1<ju[i-1,j+1]) then
                     ju[i-1,j+1]:=ju[i,j]+1;
             if inside(i,j+1) and (x[i,j+1]<>'X') then
                 if (ju[i,j+1]=0) or (ju[i,j]+1<ju[i,j+1]) then
                     ju[i,j+1]:=ju[i,j]+1;
             if inside(i+1,j+1) and (x[i+1,j+1]<>'X') then
                 if (ju[i+1,j+1]=0) or (ju[i,j]+1<ju[i+1,j+1]) then
                     ju[i+1,j+1]:=ju[i,j]+1;
             if inside(i+1,j) and (x[i+1,j]<>'X') then
                 if (ju[i+1,j]=0) or (ju[i,j]+1<ju[i+1,j]) then
                     ju[i+1,j]:=ju[i,j]+1;
             if inside(i+1,j-1) and (x[i+1,j-1]<>'X') then
                 if (ju[i+1,j-1]=0) or (ju[i,j]+1<ju[i+1,j-1]) then
                     ju[i+1,j-1]:=ju[i,j]+1;
             if inside(i,j-1) and (x[i,j-1]<>'X') then
                 if (ju[i,j-1]=0) or (ju[i,j]+1<ju[i,j-1]) then
                     ju[i,j-1]:=ju[i,j]+1;

             end;
end;

end;

{------------main------------}
begin
 citire;
 constr;
 constj;
 tmin:=255;
 for i:=1 to n do
     for j:=1 to m do
         if (ro[i,j]=ju[i,j]) and (x[i,j]<>'X') then
             if (ju[i,j]<tmin) and (ju[i,j]<>0) then
                 begin
                 tmin:=ju[i,j];
                 rezx:=i;
                 rezy:=j;
                 end;
 assign(fout,'rj.out'); rewrite(fout);
 write(fout,tmin,' ',rezx,' ',rezy);
 close(fout);
end.