Knight Moves(HDU 1372)(BFS)
2016-04-22 23:33
375 查看
A - Knight Moves
Time Limit:1000MS Memory Limit:32768KB
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
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
For each test case, print one line saying “To get from xx to yy takes n knight moves.”.
Sample Input
Sample Output
注意因为是char和int混搭又是多组数据,所以是要注意吸收掉\n(回车)。
熟练运用pair,queue!!这个几乎是模板题了。
Time Limit:1000MS Memory Limit:32768KB
题目:
HDU 1372Description
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
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
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.
题目大意:
国际象棋中马可以向 * 走一步,再向 * 走两步,然后就有了dx[8],dy[8],的走法了,之后用bfs求最短路(bfs树,最短路树),然后需要从e2走到e4类似这样的格式。注意因为是char和int混搭又是多组数据,所以是要注意吸收掉\n(回车)。
熟练运用pair,queue!!这个几乎是模板题了。
bfs伪代码:
通常用队列(先进先出,FIFO)实现 初始化队列Q. Q={起点s}; 标记s为己访问; while (Q非空) { 取Q队首元素u; u出队; if (u == 目标状态) {…} 所有与u相邻且未被访问的点进入队列; 标记u为已访问; }
代码:
#include<iostream> #include<algorithm> #include<queue> #include<stack> #include<string> #include<string.h> using namespace std; #define P pair<int,int> #define inf 0x3f3f3f3f const int maxn = 64; int map[8][8]; int dx[8] = { -1, -1, 1, 1, -2, -2, 2, 2 }, dy[8] = { 2, -2, 2, -2, 1, -1, 1, -1 }; int sx, sy, gx, gy; int res; bool check(int x,int y) { return x >= 0 && x < 8 && y >= 0 && y < 8; } int bfs() { queue<P> que; //所有位置初始化为INF memset(map, inf, sizeof(map)); que.push(P(sx, sy)); map[sx][sy] = 0; //不断循环直到队列长度为0 while (!que.empty()) { //从队列的最前端取出元素 P p = que.front(); que.pop(); //如果取出的状态已经是终点,则结束搜索 if (p.first == gx&&p.second == gy)break; for (int i = 0; i < 8; i++){ int nx = p.first + dx[i], ny = p.second + dy[i]; //判断是否出界以及是否访问过(d[nx][ny]!=inf即为已经访问过) if (check(nx, ny) && map[nx][ny] == inf){ //符合条件的话,则加入队列,并且该位置的距离确定为到p的距离+1 que.push(P(nx, ny)); map[nx][ny] = map[p.first][p.second] + 1; } } } return map[gx][gy]; } //下面这个是打印路径,从终点回溯步数减一的 /*void print_ans(int ans) { vector<P>path; P p = P(gx, gy); path.push_back(p); int nx = gx, ny = gy; while (map[nx][ny] != 0){ for (int i = 0; i < 8; i++){ nx = p.first + dx[i]; ny = p.second + dy[i]; if (check(nx, ny) && map[nx][ny] == ans-1){ p = P(nx, ny); path.push_back(p); ans--; } } } for (int i = path.size()-1; i >=0; i--) printf("%c%d\n", path[i].first+'a', path[i].second+1); }*/ int main() { char a, c; int b, d; while (~scanf("%c%d %c%d", &a, &b, &c, &d)) { getchar(); sx =a-'a'; sy = b - 1; gx = c - 'a'; gy = d - 1; res = bfs(); //print_ans(res); printf("To get from %c%d to %c%d takes %d knight moves.\n", a,b,c,d,res); } return 0; }
相关文章推荐
- POJ_3185_The_Water_Bowls_(反转)
- hdoj BestCoder Round #81 (div.2) AA Machine
- BestCoder Round #81 (div.2)-String(尺取法)
- linux基础优化
- 【dp】NOIP2010提高组乌龟棋
- VS2010中为OpenCV工程创建属性单
- ios - 纯代码创建collectionView
- Android JNI总结
- mac ulimit
- ceshi
- MATLAB函数运算
- [codevs]模板-二叉树基础
- ZooKeeper典型应用场景一览
- "数学口袋精灵"bug
- Android ADT 下载代理
- 读远--下电子书的好地方
- vb的学习
- Redis中7种集合类型应用场景&redis常用命令
- 制造高CPU使用率的简单方法
- 20150129--在线文本编辑器-02