Cod sursa(job #1231623)
Utilizator | Data | 21 septembrie 2014 09:31:50 | |
---|---|---|---|
Problema | Rj | Scor | 0 |
Compilator | fpc | Status | done |
Runda | Arhiva de probleme | Marime | 4.31 kb |
program suma;
type matrice=array[0..100,0..100] of integer;
el=record
l,c:integer;
end;
const dep:array[1..8] of el=((l:1;c:0),(l:0;c:1),(l:-1;c:0),(l:0;c:-1),(l:-1;c:1),(l:-1;c:-1),(l:1;c:1),(l:1;c:1));
var
co,v:array[1..46000] of el;
k,i,j,n,m,lj,lr,cj,cr,st,sf,x,k1,k2:integer;
pred:array[1..9000] of integer;
a,b:matrice;
f,g:text;
procedure citire;
var i,j:integer;
k:char;
begin
readln(f,n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(f,k);
if k='X' then
a[i,j]:=-1
else
if k=' ' then
a[i,j]:=0
else
if k='J' then
begin
lj:=i;
cj:=j;
end
else
if k='R' then
begin
lr:=i;
cr:=j;
end;
end;
readln(f);
end;
end;
procedure bordare;
var i:integer;
begin
for i:=0 to n+1 do
a[i,0]:=-1;
for i:=0 to m+1 do
a[0,i]:=-1;
for i:=0 to n+1 do
a[i,m+1]:=-1;
for i:=0 to m+1 do
a[n+1,i]:=-1;
end;
begin
assign(f,'rj.in');
reset(f);
assign(g,'rj.out');
rewrite(g);
citire;
bordare;
for i:=0 to n+1 do
begin
for j:=0 to m+1 do
begin
b[i,j]:=a[i,j];
end;
end;
st:=0;
sf:=1;
co[sf].l:=lr;
co[sf].c:=cr;
pred[sf]:=st;
a[lr,cr]:=2;
{ stj:=0;
sfj:=1;
coj[sfj].l:=lj;
coj[sfj].c:=cj; }
if (lj=lr) and (cr=cj) then
st:=sf
else
while (st<sf) do
begin
inc(st);
for i:=1 to 8 do
begin
if a[co[st].l+dep[i].l,co[st].c+dep[i].c]=0 then
begin
a[co[st].l+dep[i].l,co[st].c+dep[i].c]:=a[co[st].l,co[st].c]+1;
inc(sf);
pred[sf]:=st;
co[sf].l:=co[st].l+dep[i].l;
co[sf].c:=co[st].c+dep[i].c;
if (co[sf].l=lj) and (co[sf].c=cj) and ( a[co[st].l+dep[i].l,co[st].c+dep[i].c] mod 2=0) then
begin
x:=sf;
st:=sf;
end;
end;
end;
end;
while x<>0 do
begin
k:=k+1;
v[k].l:=co[x].l;
v[k].c:=co[x].c;
x:=pred[x];
end;
x:=0;
k2:=k;
k1:=1;
while x=0 do
begin
k1:=k1+1;
k2:=k2-1;
if k1=k2 then
begin
write(g,a[v[k1].l,v[k1].c]-1,' ',v[k1].l,' ',v[k1].c);
x:=1;
end;
end;
close(f);
close(g);
end.