Cod sursa(job #370388)

Utilizator arnold23Arnold Tempfli arnold23 Data 30 noiembrie 2009 23:05:23
Problema Rj Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.79 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;

var vek:array[1..101,1..101] of longint;
    v:array[1..101,1..101] of shortint;
    f:text;
    ro,ju:elem;
    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 bejar(romeo,julia:elem);
var  ux,uy,hany:longint;
begin
  new(elso);
  elso^.x:=romeo.x;
  elso^.y:=romeo.y;
  elso^.h:=1;
  elso^.kov:=nil;
  vege:=elso;
  betesz(julia.x,julia.y,1);

 while elso<>nil do 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) then
       if (vek[ux,uy]=0) then begin
        hany:=elso^.h+1;
        betesz(ux,uy,hany);
        vek[ux,uy]:=hany;
     end else begin
        hany:=elso^.h+1;
        if hany=vek[ux,uy] then begin min:=hany; hx:=ux; hy:=uy;  writeln(min,' ',hx,' ',hy); end;
     end; {if}
   end; {for}
   elso:=elso^.kov;
 end;  {while}

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.x:=i; ro.y:=j; v[i,j]:=-1; end
     else if a='J' then begin ju.x:=i; ju.y:=j; v[i,j]:=-1; end
     else v[i,j]:=0;
   end;
   readln(f);
 end;
 close(f);

 {kiiras}
 { for i:=1 to n do begin for j:=1 to m do write(v[i,j],' '); writeln; end;  }

 bejar(ro,ju);


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


end.