求ZJU1091的标程
- 提问者网友:精神病院里
- 2021-05-03 10:42
- 五星知识达人网友:逃夭
- 2021-05-03 11:30
dx:array[1..8] of -2..2=(-2,-1,1,2,2,1,-1,-2);
dy:array[1..8] of -2..2=(-1,-2,-2,-1,1,2,2,1);
var
s1,s2:string;
a,b,m,n,z,t,w:integer;
tt:array[1..8,1..8] of boolean;
h:array[1..200] of integer;
fe:array[1..2000,1..3] of integer;
procedure doing;
var
i,j,x,y:integer;
begin
t:=1;
w:=1;
fe[1,1]:=a;
fe[1,2]:=m;
fe[1,3]:=0;
tt[a,m]:=false;
while t<=w do
begin
for i:=1 to 8 do
begin
x:=fe[t,1]+dx[i];
y:=fe[t,2]+dy[i];
if (x>0) and (x<=8) and (y>0) and (y<=8) and (tt[x,y])
then begin
inc(w);
fe[w,1]:=x;
fe[w,2]:=y;
fe[w,3]:=fe[t,3]+1;
tt[x,y]:=false;
if (x=b) and (y=n) then exit;
end;
end;
inc(t);
end;
end;
begin
while not eof do
begin
fillchar(tt,sizeof(tt),true);
readln(s1);
a:=ord(s1[1])-96;
val(s1[2],m,z);
b:=ord(s1[4])-96;
val(s1[5],n,z);
s2:=copy(s1,4,2);
delete(s1,3,3);
if s1=s2 then writeln('To get from ',s1,' to ',s2,' takes 0 knight moves.')
else begin
doing;
writeln('To get from ',s1,' to ',s2,' takes ',fe[w,3],' knight moves.');
end;
end;
end.
- 1楼网友:轻熟杀无赦
- 2021-05-03 11:58
自己写的,比较丑,呵呵。
const dx:array[1..8] of -2..2=(-2,-1,1,2,2,1,-1,-2); dy:array[1..8] of -2..2=(-1,-2,-2,-1,1,2,2,1); type ff=record x,y,step:longint; end; var s:string; b:array[1..8,1..8] of boolean; h:array[1..64] of ff; startx,starty,goalx,goaly:longint;
procedure bfs; var i,tx,ty,head,tail:longint; begin head:=1; tail:=1; h[1].x:=startx; h[1].y:=starty; h[1].step:=0; fillchar(b,sizeof(b),true); b[startx,starty]:=false; repeat for i:=1 to 8 do begin tx:=h[head].x+dx[i]; ty:=h[head].y+dy[i]; if (tx>0) and (tx<=8) and (ty>0) and (ty<=8) and (b[tx,ty]) then begin b[tx,ty]:=false; inc(tail); h[tail].x:=tx; h[tail].y:=ty; h[tail].step:=h[head].step+1; if (tx=goalx) and (ty=goaly) then begin writeln('To get from ',s[1],s[2],' to ',s[4],s[5],' takes ',h[tail].step,' knight moves.'); exit; end; end; end; inc(head); until head>tail; end;
begin while not eof do begin readln(s); startx:=ord(s[1])-ord('a')+1; starty:=ord(s[2])-ord('0'); goalx:=ord(s[4])-ord('a')+1; goaly:=ord(s[5])-ord('0'); if (startx=goalx) and (starty=goaly) then writeln('To get from ',s[1],s[2],' to ',s[4],s[5],' takes 0 knight moves.') else bfs; end; end.