BFS——Knight Moves
2014-04-04 20:45
120 查看
Knight Moves
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
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.
the column and a digit (1-8) representing the row on the chessboard.
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status
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.
Input Specification
The input file 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) representingthe 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 takes n knight moves.".Sample Input
e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 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,借此机会复习了一下BFS和DFS,才发现,这么长时间没有用,两者都已经混为一谈,写篇博客总结一下啦
#include <stdio.h> #include <string.h> struct node { int x,y,step; }Q[1000]; int vis[100][100]; int fx[] = {-2,-2,-1,-1,1,2,2,1}; int fy[] = {1,-1,2,-2,-2,-1,1,2}; char b,d; int e,r; int i1,j1,i2,j2; void bfs(int i,int j) { node head,next; int a,z; a = z = 0; head.x = i; head.y = j; head.step = 0; Q[z++] = head; vis[i][j] = 1; while(a < z) { head = Q[a++]; vis[head.x][head.y] = 1; if(head.x == i2 && head.y == j2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",b,e,d,r,head.step); break; } else { int m; for(m = 0;m < 8;m++) { next.x = head.x + fx[m]; next.y = head.y + fy[m]; next.step = head.step; if(next.x > 0 && next.x < 9 && next.y > 0 && next.y < 9 && vis[next.x][next.y] == 0) { next.step = next.step + 1; Q[z++] = next; } } } } } int main() { while(~scanf("%c%d%*c",&b,&e)) { scanf("%c%d%*c",&d,&r); i1 = b-'a'+1; j1 = e; i2 = d-'a'+1; j2 = r; memset(vis,0,sizeof(vis)); bfs(i1,j1); } return 0; }
相关文章推荐
- (BFS)Knight Moves--HDOJ
- HDU 1372 Knight Moves 【经典BFS】
- HDOJ/HDU 1372 Knight Moves(经典BFS)
- zoj 1091 Knight Moves (BFS)(情况用循环控制,值得学习啊)
- poj 2243 bfs Knight Moves
- Knight Moves (zoj 1091 poj2243)BFS
- HDU1372:Knight Moves(BFS)
- PKU-2243 Knight Moves (单BFS、双BFS)
- HDU 1372 Knight Moves,bfs
- hud 1372 Knight Moves【bfs】
- HDU-#1372 Knight Moves(双向BFS)
- Knight Moves(BFS)
- POJ 2243 ZOJ 1091 UVaOJ 439 Knight moves(BFS广度优先搜索)
- Knight Moves BFS 水题
- HDU 1372--Knight Moves【BFS】
- //广搜bfs//Knight Moves------三T
- hdoj 1372 Knight Moves 【BFS+优先队列】
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
- poj 1915 Knight Moves【BFS】【简单】
- POJ 1915-Knight Moves (单向BFS && 双向BFS 比较)