洛谷 P1443 马的遍历
来源:互联网 发布:深圳舞蹈工作室 知乎 编辑:程序博客网 时间:2024/05/16 19:02
洛谷 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.
阅读全文
1 0
- 洛谷P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷 P1443 马的遍历
- 洛谷p1443 马的遍历
- P1443 马的遍历
- P1443 马的遍历
- P1443 马的遍历
- 【搜索】洛谷 P1443 马的遍历
- [LUOGU]P1443 马的遍历
- 【p1443-马的遍历】解题记录
- luogu P1443 马的遍历 【队列 dfs】
- 洛谷 马的遍历
- 洛谷1443 马的遍历
- vijos P1443 银河英雄传说
- 图的遍历算法-马遍历棋盘
- mybatis处理xml中SQL语句的大于、小于、大于等于、小于等于
- 如何查看github的html页面显示效果
- 控制执行流程
- H5新增标签
- Retrofit + RxJava + OkHttp 让网络请求变的简单-封装篇
- 洛谷 P1443 马的遍历
- 软raid5的挂载
- python一种用staticmethod实现单例模式方法的探讨
- Hive存储过程HQL/SQL(二)– 在Hive存储过程中读写MySQL表
- 第11章:Shell编程
- 三句话影响人的一生
- 文件节点写函数xxxx_store返回值
- xxx is not in the sudoers file. This incident will be reported解决方法
- 很强大的一个jquery分页插件