洛谷 P1443 马的遍历
2017-05-17 18:58
281 查看
洛谷 P1443 马的遍历
题目
题目描述有一个n*m的棋盘(1
题目
题解
BFS+手动左对齐(Pascal没有左对齐)代码(Pascal)
const flag:array[1..8,0..1]of longint=((2,1),(1,2),(-1,2),(2,-1),(-2,-1),(-1,-2),(-2,1),(1,-2)); var n,m,head,tail:longint; q:array[0..160005]of record x,y:longint; end; map:array[0..405,0..405]of longint; vis:array[0..405,0..405]of boolean; procedure init; begin readln(n,m,q[1].x,q[1].y); end; function check(x,y:longint):boolean; begin if (x<1)or(y<1)or(x>n)or(y>m) then exit(false); if vis[x,y]=true then exit(false); exit(true); end; procedure find(x,y:longint); begin inc(tail);vis[x,y]:=true; q[tail].x:=x;q[tail].y:=y; map[x,y]:=map[q[head].x,q[head].y]+1; end; procedure print(x:longint); var i,t,y:longint; begin t:=0;y:=x; if x<0 then begin x:=abs(x); inc(t); end; if (x<=9) then inc(t) else if (x<=99) then t:=t+2 else if (x<=999) then t:=t+3 else if (x<=9999) then t:=t+4; write(y); for i:=1 to 5-t do write(' '); end; procedure main; var i,j,x,y:longint; begin fillchar(vis,sizeof(vis),0); head:=0;tail:=1; for i:=1 to n do for j:=1 to m do map[i,j]:=-1; map[q[1].x,q[1].y]:=0; vis[q[1].x,q[1].y]:=true; while head<tail do begin inc(head); x:=q[head].x;y:=q[head].y; for i:=1 to 8 do if check(x+flag[i,0],y+flag[i,1]) then find(x+flag[i,0],y+flag[i,1]); end; for i:=1 to n do begin for j:=1 to m do print(map[i,j]); writeln; end; end; begin init; main; end.
相关文章推荐
- 洛谷p1443 马的遍历
- 洛谷 P1443 马的遍历(BFS)
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 【搜索】洛谷 P1443 马的遍历
- 洛谷 - P1443:马的遍历
- 洛谷 P1443 马的遍历(bfs)
- 洛谷P1443 马的遍历
- 洛谷 18-03-14-P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷——P1443 马的遍历
- P1443 马的遍历
- p1443 马的遍历
- 洛谷1443 马的遍历 解题报告
- P1443 马的遍历
- 洛谷Luogu-1443 马的遍历 (BFS) HQG_AC的博客
- 图的遍历 洛谷p3916
- [LUOGU]P1443 马的遍历