hdu 1372 Knight Moves(经典BFS)
来源:互联网 发布:淘宝运营常用软件 编辑:程序博客网 时间:2024/06/06 03:42
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1372
大意:至少要用多少步,骑士才能从给定位置到达另一个位置。(骑士的走法和象棋里的马的走法是一样的,均是日字型)
Sample Input
e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.To get from a1 to b2 takes 4 knight moves.To get from b2 to c3 takes 2 knight moves.To get from a1 to h8 takes 6 knight moves.To get from a1 to h7 takes 5 knight moves.To get from h8 to a1 takes 6 knight moves.To get from b1 to c3 takes 1 knight moves.To get from f6 to f6 takes 0 knight moves.
BFS核心是一次性先把所有最近的能走的统统遍历,然后再去遍历其他的点。这和队列联系在一起了,近点先进,远点后进,对队列的遍历就相当于层次遍历了。比如这个二叉树的层次遍历:
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;bool vis[9][9];int s[2],e[2];int dir[8][2]={{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-1,2},{-2,1}};typedef struct node{ int x,y,step; //pre[]}Node;Node que[90];int bfs(int sx,int sy){ int l=0,r=0; vis[sx][sy]=1; que[r].x=sx; que[r].y=sy; que[r++].step=0; while(l<r){ Node cur=que[l++],next; for(int i=0;i<8;i++){ next.x=cur.x+dir[i][0]; next.y=cur.y+dir[i][1]; next.step=cur.step+1; if(next.x>=1&&next.x<=8&&next.y<=8&&next.y>=1&&!vis[next.x][next.y]){ if(next.x==e[0]&&next.y==e[1]){ return next.step; } vis[next.x][next.y]=1; que[r++]=next; } } } return 0;}int main(){ //freopen("cin.txt","r",stdin); char start[5],end[5]; while(cin>>start>>end){ s[0]=start[0]-'a'+1; //加1啊,不要犯错。 s[1]=start[1]-'0'; e[0]=end[0]-'a'+1; e[1]=end[1]-'0'; memset(vis,0,sizeof(vis)); memset(que,0,sizeof(que)); printf("To get from %s to %s takes %d knight moves.\n",start,end,bfs(s[0],s[1])); } return 0;}
0 0
- hdu 1372 Knight Moves(经典BFS)
- HDU 1372 Knight Moves 【经典BFS】
- HDOJ/HDU 1372 Knight Moves(经典BFS)
- hdu 1372 bfs Knight Moves
- 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】
- Knight Moves(HDU 1372)(BFS)
- HDU-1372Knight Moves(BFS)
- hdu-1372-Knight Moves(BFS)
- Knight Moves hdu BFS
- hdu Knight Moves(bfs)
- hdu--1372 knight moves (BFS+数据结构)
- LocalBroadcastManager源码
- Linux - Bash shell的功能;内建命令type
- 读《数学之美》第三章 统计语言模型
- The constructor SimpleAdapter(new View.OnClickListener(){},...)is undefined的问题及解决
- HDOJ 1222 Wolf and Rabbit(数学解题技巧)
- hdu 1372 Knight Moves(经典BFS)
- 简易相册
- [HDU 2196] Computer 树形dp
- hdu 1558:Segment set
- makefile
- 三目运算符的简单介绍
- 最小生成树 prim算法和kruskal
- STP 根桥、根端口、指定端口是如何选举的
- MIT6.828 JOS系统 lab2