宽度优先搜索1185: Knight Moves
来源:互联网 发布:2017最流行的编程语言 编辑:程序博客网 时间:2024/05/29 18:37
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 visitseach square of a given set ofn 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 solvesthe "difficult" part.
Your job is to write a program that takes two squares a and bas input and then determines the number of knight moves on a shortest routefroma to b.
Input Specification
The input file will contain one or more test cases. Each test case consists of one linecontaining 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 Specification
For each test case, print one line saying "To get from xx to yy takesn 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.第一次用bfs写题,有点麻烦,以后改进吧#include <stdio.h>#include <string.h>#include <queue>#include <iostream>using namespace std;int dist[10][10]; //记录距离 bfs必备int vis[10][10]; //记录是否算过void bfs (int n,int m){ queue<int> q; q.push(n); int a,b; a=q.front()/8; b=q.front()%8; if(b==0) {a-=1;b=8;} //一直在这出错。。。。 vis[a][b]=1; dist[a][b]=0; while(q.front()!=m) { int a,b; a=q.front()/8; b=q.front()%8; if(b==0) {a-=1;b=8;} if(a-1>=1&&b-2>=1&&!vis[a-1][b-2]) { dist[a-1][b-2]=dist[a][b]+1; vis[a-1][b-2]=1; q.push((a-1)*8+(b-2));} if(a+1<=8&&b-2>=1&&!vis[a+1][b-2]) { dist[a+1][b-2]=dist[a][b]+1; vis[a+1][b-2]=1; q.push((a+1)*8+(b-2));} if(a+1<=8&&b+2<=8&&!vis[a+1][b+2]) { dist[a+1][b+2]=dist[a][b]+1; vis[a+1][b+2]=1; q.push((a+1)*8+(b+2));} if(a-1>=1&&b+2<=8&&!vis[a-1][b+2]) { dist[a-1][b+2]=dist[a][b]+1; vis[a-1][b+2]=1; q.push((a-1)*8+(b+2));} if(b-1>=1&&a-2>=1&&!vis[a-2][b-1]) { dist[a-2][b-1]=dist[a][b]+1; vis[a-2][b-1]=1; q.push((a-2)*8+(b-1));} if(b+1<=8&&a-2>=1&&!vis[a-2][b+1]) { dist[a-2][b+1]=dist[a][b]+1; vis[a-2][b+1]=1; q.push((a-2)*8+(b+1));} if(b+1<=8&&a+2<=8&&!vis[a+2][b+1]) { dist[a+2][b+1]=dist[a][b]+1; vis[a+2][b+1]=1; q.push((a+2)*8+(b+1));} if(b-1>=1&&a+2<=8&&!vis[a+2][b-1]) { dist[a+2][b-1]=dist[a][b]+1; vis[a+2][b-1]=1; q.push((a+2)*8+(b-1)); q.pop(); }}int main (){ char a,b,c,d; int e,f; while(scanf("%c%d%c%c%d%c",&a,&e,&c,&b,&f,&d)==6) { //memset(fa,0,sizeof(fa)); memset(dist,0,sizeof(dist)); memset(vis,0,sizeof(vis)); int n=(a-'a'+1)*8+e; int m=(b-'a'+1)*8+f; bfs(n,m); int x,y; x=m/8; y=m%8; if(y==0) {x-=1;y=8;} printf("To get from %c%d to %c%d takes %d knight moves./n",a,e,b,f,dist[x][y]); } return 0;}
- 宽度优先搜索1185: Knight Moves
- Knight Moves--广度优先搜索
- POJ1915 - Knight Moves - 广度优先搜索
- C++广度优先搜索之Knight Moves
- Knight Moves bfs(广度优先搜索)
- pku 1915 Knight Moves(双向广度优先搜索)
- HDU 1372 广度优先搜索(BFS) Knight Moves
- HDU 1372 Knight Moves 广度优先搜索 bfs
- hdu1372-Knight Moves--搜索
- Knight Moves - UVa 439 搜索
- 搜索—Problem_1015-Knight Moves
- POJ 2243 ZOJ 1091 UVaOJ 439 Knight moves(BFS广度优先搜索)
- HDU/HDOJ 1372 Knight Moves(骑士游走问题) 简单广度优先搜索
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
- hdu 1372 || poj 2243 Knight Moves(搜索:BFS+优先队列水题)
- JOJ 1185: Knight Moves
- zoj - 1091 - Knight Moves(广度优先法)
- hdoj 1372 Knight Moves 【BFS+优先队列】
- 三层模式开发使用GridPanel(.net)控件代码实例详解(展示层代码)
- 视频聊天网站的研究、发展以及趋势时间
- 小情歌
- Troubleshooting for Kerberos
- Java InputStream 的mark 和reset操作
- 宽度优先搜索1185: Knight Moves
- GridPanel的各项属性总结
- jquert $.post()
- 关于字符串拷贝!!
- 分享][轉載]这些道理不懂,你注定就是穷打工的命!
- 常用算法总结之查找(二)----折半查找
- 追寻
- C语言声明优先级规则
- Android基础 : Android Content Provider[转]