HDU1372 跳马问题(bfs)
来源:互联网 发布:德国警察 知乎 编辑:程序博客网 时间:2024/06/07 11:11
Problem H
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 18
Problem Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
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.
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib> #include<queue>using namespace std;const int MAXN=9;struct node{int x,y,w;node(int xx,int yy,int ww){x=xx;y=yy;w=ww;}};char map[MAXN][MAXN];bool done[MAXN][MAXN];int sx,sy,ex,ey;int dx[]={-1,1,-1,1,2,2,-2,-2}; int dy[]={2,2,-2,-2,1,-1,1,-1};int bfs(){int i;queue<node>q;memset(done,0,sizeof(done));q.push(node(sx,sy,0));while(!q.empty()){node e=q.front();q.pop();if(e.x==ex&&e.y==ey)return e.w;if(done[e.x][e.y])continue;done[e.x][e.y]=1;for(i=0;i<8;i++){int curx=e.x+dx[i];int cury=e.y+dy[i];if(curx<1||curx>8||cury<1||cury>8)continue;if(done[curx][cury]!=1){q.push(node(curx,cury,e.w+1));}}}return -1;}int main(){//freopen("123.txt","r",stdin);char c1,c2;while(~scanf("%c%d %c%d\n",&c1,&sy,&c2,&ey)){sx=c1-'a'+1;ex=c2-'a'+1;int step=bfs();printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,sy,c2,ey,step);}return 0;}
0 0
- HDU1372 跳马问题 BFS
- HDU1372 跳马问题(bfs)
- 跳马~~~HDU1372
- 跳马问题。bfs计数
- BFS:中国象棋中的跳马问题
- 中国象棋中的跳马问题(bfs)
- HDU1372 BFS
- hdu1372(bfs)
- SHUOJ 1659 跳马问题 (BFS)
- C++ - Nuc - 00004:跳马问题(bfs)
- 中国象棋的跳马问题(BFS)
- Knight Moves hdu1372 bfs
- hdu1372-简单的BfS
- HDU1372:Knight Moves(BFS)
- hdu1372 简单bfs
- hdu1372 Knight Moves (BFS)
- HDU1372:Knight Moves(BFS)
- HDU1372(BFS)
- ADO.net的操作
- Codeforces Round #249 (Div. 2) ABC
- 几张图看懂Oracle 你想要的 这里都有
- checkbox多选删除
- RT-Thread操作系统之—IPC
- HDU1372 跳马问题(bfs)
- POJ 2388 Who's in the Middle
- codeforces 96A (字符串模拟水题)
- 三层架构实战篇 上
- 高斯白噪声(White Gaussion Noise)的内容与实现
- oracle RAC集群体系结构
- sql数据类型
- 基于Linux C的聊天室客户端(一)传输规范
- 什么情况