Cod sursa(job #129330)
Utilizator | Data | 29 ianuarie 2008 00:04:00 | |
---|---|---|---|
Problema | Rj | Scor | 0 |
Compilator | fpc | Status | done |
Runda | Arhiva de probleme | Marime | 7.1 kb |
const fi='rj.in';
fo='rj.out';
type mat=array[0..101,0..101] of integer;
var
f:text;
i,j,k,n,m:integer;
a,b,c:mat;
ch:char;
lR,cR,lJ,cJ:integer;
l:integer;
vb,mm:boolean;
procedure muta(i,j:integer;x,y:integer;var vb:boolean);
begin
if (a[x,y]=0)and (b[x,y]=0) then vb:=true
else vb:=false;
end;
procedure muta1(i,j:integer;x,y:integer;var vb:boolean);
begin
if (a[x,y]=0) and (c[x,y]=0) then vb:=true
else vb:=false;
end;
Begin
assign(f,fi);
reset(f);
readln(f,n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(f,ch);
if ch='R' then begin
lR:=i;
cR:=j;
end;
if ch='J' then begin
lj:=i;
cj:=j;
end;
if ch='X' then a[i,j]:=1;
end;
readln(f);
end;
close(f);
assign(f,fo);
rewrite(f);
for j:=0 to m+1 do
begin
b[0,j]:=-1;
c[0,j]:=-1;
b[n+1,j]:=-1;
c[n+1,j]:=-1;
end;
for i:=0 to n+1 do
begin
b[i,0]:=-1;
c[i,0]:=-1;
b[i,m+1]:=-1;
c[i,m+1]:=-1;
end;
k:=1;
l:=1;
b[lR,cR]:=1;
c[lJ,cJ]:=1;
repeat
for i:=1 to n do
for j:=1 to m do
begin
if (c[i,j]=b[i,j])and (b[i,j]<>0) then begin
write(f,k,' ',i,' ',j);
close(f);
exit;
end
else
if b[i,j]=k then begin
muta(i,j,i-1,j-1,vb);
if vb then begin
b[i-1,j-1]:=k+1;
mm:=true;
end;
muta(i,j,i,j+1,vb);
if vb then begin
b[i,j+1]:=k+1;
mm:=true;
end;
muta(i,j,i-1,j,vb);
if vb then begin
mm:=true;
b[i-1,j]:=k+1;
end;
muta(i,j,i-1,j+1,vb);
if vb then begin
mm:=true;
b[i-1,j+1]:=k+1;
end;
muta(i,j,i,j-1,vb);
if vb then begin
b[i,j-1]:=k+1;
mm:=true;
end;
muta(i,j,i+1,j-1,vb);
if vb then begin
mm:=true;
b[i+1,j-1]:=k+1;
end;
muta(i,j,i+1,j,vb);
if vb then begin
b[i+1,j]:=k+1;
mm:=true;
end;
muta(i,j,i+1,j+1,vb);
if vb then begin
mm:=true;
b[i+1,j+1]:=k+1;
end;
end;
if c[i,j]=l then begin
muta1(i,j,i-1,j-1,vb);
if vb then begin
c[i-1,j-1]:=l+1;
mm:=true;
end;
muta1(i,j,i,j+1,vb);
if vb then begin
mm:=true;
c[i,j+1]:=l+1;
end;
muta1(i,j,i-1,j,vb);
if vb then begin
c[i-1,j]:=l+1;
mm:=true;
end;
muta1(i,j,i-1,j+1,vb);
if vb then begin
mm:=true;
c[i-1,j+1]:=l+1;
end;
muta1(i,j,i,j-1,vb);
if vb then begin
c[i,j-1]:=l+1;
mm:=true;
end;
muta1(i,j,i+1,j-1,vb);
if vb then begin
mm:=true;
c[i+1,j-1]:=l+1;
end;
muta1(i,j,i+1,j,vb);
if vb then begin
c[i+1,j]:=l+1;
mm:=true;
end;
muta1(i,j,i+1,j+1,vb);
if vb then begin
mm:=true;
c[i+1,j+1]:=l+1;
end;
end;
end;
inc(k); inc(l);
until not mm;
end.