Cod sursa(job #373476)

Utilizator arnold23Arnold Tempfli arnold23 Data 13 decembrie 2009 21:16:56
Problema Rj Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.59 kb
const cx:array[1..8] of shortint =(0,0,1,-1,1,-1,1,-1);
      cy:array[1..8] of shortint =(-1,1,0,0,1,-1,-1,1);

type lista=^elem;
     elem=record
          x,y,h:longint;
          kov:lista;
        end;
     adat=record
          kx,ky:longint;
        end;

var rom,jul:array[1..101,1..101] of longint;
    v:array[1..101,1..101] of shortint;
    f:text;
    ro,ju:adat;
    i,j,n,m,min,hx,hy:longint;
    a:char;
    elso,vege:lista;


procedure betesz(q,w,e:longint);
var p:lista;
begin
 new(p);
 p^.x:=q;
 p^.y:=w;
 p^.h:=e;
 p^.kov:=nil;
 vege^.kov:=p;
 vege:=p;
end;

procedure bejarrom(l:adat);
var  ux,uy,hany:longint;
     ok:boolean;
begin
  new(elso);
  elso^.x:=l.kx;
  elso^.y:=l.ky;
  elso^.h:=1;
  elso^.kov:=nil;
  vege:=elso;

  ok:=true;
 while ok do begin
 if elso=nil then ok:=false else begin
   for i:=1 to 8 do begin
     ux:=elso^.x+cx[i];
     uy:=elso^.y+cy[i];
     if (ux in [1..n]) and (uy in [1..m]) then
      if (v[ux,uy]<>-1) and (rom[ux,uy]=0) then begin
        hany:=elso^.h+1;
        betesz(ux,uy,hany);
        rom[ux,uy]:=hany;
        if (ux=ju.kx) and (uy=ju.ky) then ok:=false;
     end;
   end;
   elso:=elso^.kov;
  end;
 end;

end;

procedure bejarjul(l:adat);
var  ux,uy,hany:longint;
     ok:boolean;
begin
  new(elso);
  elso^.x:=l.kx;
  elso^.y:=l.ky;
  elso^.h:=1;
  elso^.kov:=nil;
  vege:=elso;

  ok:=true;
 while ok do begin
 if elso=nil then ok:=false else begin
   for i:=1 to 8 do begin
     ux:=elso^.x+cx[i];
     uy:=elso^.y+cy[i];
     if (ux in [1..n]) and (uy in [1..m]) then
      if (v[ux,uy]<>-1) and (jul[ux,uy]=0) then begin
        hany:=elso^.h+1;
        betesz(ux,uy,hany);
        jul[ux,uy]:=hany;
        if (ux=ro.kx) and (uy=ro.ky) then ok:=false;
     end;
   end;
   elso:=elso^.kov;
  end;
 end;

end;


begin
 assign(f,'rj.in');
 reset(f);
 readln(f,n,m);
 for i:=1 to n do begin
   for j:=1 to m do begin
     read(f,a);
     if a='X' then v[i,j]:=-1
     else if a='R' then begin ro.kx:=i; ro.ky:=j; end
     else if a='J' then begin ju.kx:=i; ju.ky:=j; end
     else v[i,j]:=0;
   end;
   readln(f);
 end;
 close(f);


 bejarrom(ro);
 bejarjul(ju);

 min:=maxlongint;
 for i:=1 to n do
   for j:=1 to m do
     if rom[i,j]=jul[i,j] then
       if (rom[i,j]<min) and (rom[i,j]+jul[i,j]<>0) then begin
                              min:=rom[i,j];
                              hx:=i;
                              hy:=j;
                            end;

 assign(f,'rj.out');
 rewrite(f);
 write(f,min,' ',hx,' ',hy);
 close(f);

end.