HDU 1372 Knight Moves (BFS)
来源:互联网 发布:鞋子 知乎 编辑:程序博客网 时间:2024/06/05 22:51
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372
简单的BFS。即给定马的起点和终点(走的方式和象棋一样),求最短的路径。关键是要弄清楚马的行进方向的顺序,即从右上第一个顺时针过来。
代码:
</pre><pre name="code" class="cpp"><span style="color:#000000;">#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;int sx,sy,ex,ey,a[10][10],use[10][10];int dx[8]= {-2,-1,1,2,2,1,-1,-2},dy[8]= {1,2,2,1,-1,-2,-2,-1};struct point{ int x,y,step;};queue<point> Q;int bfs(int x,int y){ point Start,New; Start.x=x; Start.y=y; Start.step=0; Q.push(Start); while(!Q.empty()) { Start = Q.front(); Q.pop(); if(Start.x==ex&&Start.y==ey) return Start.step; for(int i=0; i<8; i++) { New.x=Start.x+dx[i]; New.y=Start.y+dy[i]; New.step=Start.step; if(New.x>0&&New.y>0&&New.x<9&&New.y<9&&!use[New.x][New.y]) { use[New.x][New.y]=1; New.step=Start.step+1; Q.push(New); } } }}int main(){ char S[10],E[10]; while(scanf("%s %s",S,E)!=EOF) { while(!Q.empty()) Q.pop(); memset(use,0,sizeof(use)); sx=S[0]-'a'+1,sy=S[1]-'0'; ex=E[0]-'a'+1,ey=E[1]-'0'; use[sx][sy]=1; printf("To get from %s to %s takes %d knight moves.\n",S,E,bfs(sx,sy)); } return 0;}</span>
0 0
- 【hdu 1372】 Knight Moves (BFS)
- hdu 1372 Knight Moves (水bfs)
- hdu 1372 Knight Moves(BFS)
- Hdu 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves (BFS)
- hdu 1372 Knight Moves(bfs)
- HDU-#1372 Knight Moves(双向BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(bfs)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves (BFS)
- hdu 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves (BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372Knight Moves(bfs)
- 【HDU 1372 Knight Moves(BFS)】
- JDK API 下载
- 一种粗略的普通摄像头深度信息提取方法
- 7. 函数
- Unity学习笔记10——旋转(四元数和欧拉角)
- Saiku+Kylin多维分析平台探索
- HDU 1372 Knight Moves (BFS)
- jquery多属性选择器
- HDU 1072不用标记的BFS
- Linux下使Shell 命令脱离终端在后台运行
- 出现xxxViewController respondsToSelector 原因
- 面试题10:二进制数中1的个数
- 【c/c++】break和continue
- Spring的split()函数引发的错误
- C++第4次实验