Cod sursa(job #81700)

Utilizator M@2Te4iMatei Misarca M@2Te4i Data 3 septembrie 2007 21:30:48
Problema Rj Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.25 kb
program romik_si_julietzik;

type sir=array[0..102,0..102]of integer;

var m,n:integer;
    a,b,v:sir;
    pas:byte;
    gasit:boolean;
    rx,ry,jx,jy:byte;

procedure citire;
var i,j:integer;
    c:char;
begin
assign(input,'rj.in');
reset(input);
readln(n,m);
for i:=1 to n do
    begin
    for j:=1 to m do
        begin
        read(c);
        if c='X' then
           a[i,j]:=-2;
        if c=' ' then
           a[i,j]:=0;
        if c='R' then
           begin
           a[i,j]:=-3;
           rx:=i;
           ry:=j;
           end;
        if c='J' then
           begin
           a[i,j]:=-4;
           jx:=i;
           jy:=j;
           end;
        end;
    readln;
    end;
close(input);
end;

procedure umplere_zona(i,j:integer;var a:sir);
begin
if a[i+1,j]=0 then
   begin
   a[i+1,j]:=pas;
   gasit:=true;
   end;
if a[i-1,j]=0 then
   begin
   a[i-1,j]:=pas;
   gasit:=true;
   end;
if a[i,j+1]=0 then
   begin
   a[i,j+1]:=pas;
   gasit:=true;
   end;
if a[i,j-1]=0 then
   begin
   a[i,j-1]:=pas;
   gasit:=true;
   end;
if a[i+1,j+1]=0 then
   begin
   a[i+1,j+1]:=pas;
   gasit:=true;
   end;
if a[i+1,j-1]=0 then
   begin
   a[i+1,j-1]:=pas;
   gasit:=true;
   end;
if a[i-1,j+1]=0 then
   begin
   a[i-1,j+1]:=pas;
   gasit:=true;
   end;
if a[i-1,j-1]=0 then
   begin
   a[i-1,j-1]:=pas;
   gasit:=true;
   end;
end;

procedure lee(var b:sir;w,q:byte);
var i,j:integer;
begin
b[w,q]:=1;
pas:=1;
while true do
      begin
      pas:=pas+1;
      gasit:=false;
      for i:=1 to n do
          for j:=1 to m do
              begin
              if b[i,j]=pas-1 then
                 umplere_zona(i,j,b);
              end;
      if not gasit then
         break;
      end;
end;

procedure gasire_pas;
var r,i,j,x,y:integer;
begin
r:=maxint;
gasit:=false;
for i:=1 to n do
    for j:=1 to m do
        if (b[i,j]=v[i,j]) and (b[i,j]>0) and (b[i,j]<r) then
           begin
           r:=b[i,j];
           x:=i;
           y:=j;
           gasit:=true;
           end;
if gasit then
   writeln(r,' ',x,' ',y);
end;

begin
citire;
assign(output,'rj.out');
rewrite(output);
b:=a;
lee(b,rx,ry);
v:=a;
lee(v,jx,jy);
gasire_pas;
close(output);
end.