Cod sursa(job #273629)

Utilizator Razzoragamita dandanache Razzor Data 8 martie 2009 20:08:20
Problema Rj Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.63 kb
type vector=array[1..40000] of integer;
     matrice=array[0..101,0..101] of integer;
var a:matrice;
    n,m,x1,x2,y1,y2,i,j,v1,v2,pi,ps,xi,ti:integer;
    cx,cy:vector;
    gata:boolean;
    f,g:text;
    c:char;
begin

assign(f,'rj.txt');
reset(f);
assign(g,'rjout.txt');
rewrite(g);
readln(f,n,m);


for i:=1 to n do
begin
for j:=1 to m do
begin
	read(f,c);
    if c=' ' then
	a[i,j]:=0;

	if c='R' then
	begin
	x1:=i;
	y1:=j;
	end;

	if c='J' then
	begin
	x2:=i;
	y2:=j;
	end;

	if c='X' then
	a[i,j]:=-1;
end;
readln(f);
end;
close(f);
{in matrice am -1 unde nu pot trece si 0 unde pot trece}


for i:=0 to m+1 do
    begin
    a[0,i]:=-1;
    a[n+1,i]:=-1;
    end;
for i:=0 to n+1 do
    begin
    a[i,0]:=-1;
    a[i,m+1]:=-1;
    end;

{in cele 2 for-uri am pus -1 pe marinile matricii}
gata:=false;
pi:=1;
ps:=1;
cx[1]:=x1;
cy[1]:=y1;
a[x1,y1]:=1;
while (ps<=pi)and not gata do
      begin
      v1:=cx[ps];v2:=cy[ps];
      if (a[v1-1,v2]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1-1;
         cy[pi]:=v2;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

       if (a[v1,v2+1]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1;
         cy[pi]:=v2+1;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

       if (a[v1+1,v2]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1+1;
         cy[pi]:=v2;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

       if (a[v1,v2-1]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1;
         cy[pi]:=v2-1;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

		 if (a[v1-1,v2-1]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1-1;
         cy[pi]:=v2-1;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

		 if (a[v1-1,v2+1]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1-1;
         cy[pi]:=v2+1;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

		 if (a[v1+1,v2-1]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1+1;
         cy[pi]:=v2-1;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;

		 if (a[v1+1,v2+1]=0) then
         begin
         pi:=pi+1;
         cx[pi]:=v1+1;
         cy[pi]:=v2+1;
         a[cx[pi],cy[pi]]:=a[v1,v2]+1;
         end;
      ps:=ps+1;
      end;
if odd(a[x2,y2]) then
begin
write(g,(a[x2,y2]div 2)+1,' ');
for i:=1  to ps do
if a[cx[i],cy[i]]=(a[x2,y2] div 2)+1 then
write(g,cx[i],' ',cy[i]);
end
else
begin
write(g,a[x2,y2] div 2,' ');
for i:=1  to ps do
if a[cx[i],cy[i]]=a[x2,y2] div 2 then
write(g,cx[i],' ',cy[i]);
end;


close(g);
end.