ZOJ 1091 (HDU 1372) Knight Moves(BFS)
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) 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 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.
题目大意:国际象棋的骑士走日。在一个8*8的棋盘中,给定两个点,求骑士从一个点到达另一个点的最少步数。
BFS题目,代码如下:
1 # include<iostream> 2 # include<cstdio> 3 # include<queue> 4 # include<cstring> 5 using namespace std; 6 bool vis[8][8]; //可以大幅度缩短搜索时间,本题不加这个也可以AC 7 int dx[8] = {1,1,-1,-1,2,-2,2,-2}; 8 int dy[8] = {2,-2,2,-2,1,1,-1,-1}; 9 int sx,sy,ex,ey; 10 bool ismap(int x,int y){ 11 if(x<0 || y<0 ||x>=8 || y>=8) 12 return false; 13 return true; 14 } 15 16 struct Node{ 17 int x,y,step; 18 }qishi; 19 20 queue<Node>q; 21 22 int bfs(){ 23 memset(vis,0,sizeof(vis)); 24 qishi.x = sx; qishi.y = sy; qishi.step = 0; 25 while(!q.empty()) q.pop(); 26 vis[sx][sy] = 1; 27 q.push(qishi); 28 29 while(!q.empty()){ 30 Node tmp = q.front(); q.pop(); 31 if(tmp.x==ex && tmp.y == ey) 32 return tmp.step; 33 for(int i=0;i<8;i++){ 34 int x = tmp.x + dx[i]; 35 int y = tmp.y + dy[i]; 36 if(vis[x][y]) continue; 37 if(!ismap(x,y)) continue; 38 vis[x][y] = 1; 39 qishi.x = x; 40 qishi.y = y; 41 qishi.step = tmp.step + 1; 42 q.push(qishi); 43 } 44 } 45 } 46 47 int main(){ 48 char a[5],b[5]; 49 while(scanf("%s%s",a,b)!=EOF){ 50 sx = a[0]-'a'; sy = a[1]-'1'; 51 ex = b[0]-'a'; ey = b[1]-'1'; 52 printf("To get from %s to %s takes %d knight moves.\n", a, b, bfs()); 53 } 54 return 0; 55 }
相关文章
- HDU 1556-差分数组和线段树的对比分析-Color the ball
- hdu 3980 Paint Chain(SG函数)
- HDU 3336 KMP算法中对next数组的理解「建议收藏」
- HDU P3341 Lost’s revenge 题解+数据生成器
- hdu 3336 Count the string(kmp应用)
- hdu 3336 Count the string 用心写的题解
- hdu 1142_hdu1001
- HDU 3336 Count the string 解题报告
- [ACM] HDU 1006 解题报告
- H - Partial Tree HDU - 5534 【 完全背包 】
- HDU-2084 数塔
- 敌兵布阵(HDU 1166)
- I Hate It (HDU 1754)
- 校庆神秘建筑(HDU 1411)
- 还是畅通工程(HDU 1233)
- Friend-Graph (HDU 6152)2017中国大学生程序设计竞赛 - 网络选拔赛
- CaoHaha's staff (HDU 6154)(2017中国大学生程序设计竞赛 - 网络选拔赛)
- Oil Deposits (HDU - 1241 )(DFS思路 或者 BFS思路)
- 畅通工程续(HDU 1874)(简单最短路)
- Drainage Ditches (HDU - 1532)(最大流)
- Ancient Go ( HDU - 5546 ) ( BFS 搜索是否相连)
- 统计难题 【 HDU - 1251 】【 字典树 】
- Phone List 【 HDU - 1671 】 【 字典树判断是否存在前缀 】
- A * B Problem Plus【HDU 1402】 【FFT求高精度乘法】
- Bone Collector HDU - 2602【 01 背包 】