hdu 1372 Knight Moves
来源:互联网 发布:复旦金融硕士 知乎 编辑:程序博客网 时间:2024/05/21 10:45
hdu 1372 Knight Moves
广搜, 那个Knight是走"日"字的, 就像中古想起里面的马走法一样哦, 那么它在不出范围的情况下就有8个方向哦.
#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct cell { int x, y; int step;};int dir[8][2] = {-2, -1, -2, 1, -1, 2, 1, 2, 2, 1, 2, -1, 1, -2, -1, -2}; // 8个方向哦bool visited[9][9];cell start, end, go, to;int bfs() { int i; queue<cell> Q; Q.push(start); memset(visited, false, sizeof(visited)); visited[start.x][start.y] = true; while (!Q.empty()) { go = Q.front(); Q.pop(); for (i = 0; i < 8; i++) { to.x = go.x + dir[i][0]; to.y = go.y + dir[i][1]; to.step = go.step; if (to.x == end.x && to.y == end.y) { return to.step + 1; } if (to.x >= 1 && to.x <= 8 && to.y >= 1 && to.y <= 8 && !visited[to.x][to.y]) { ++to.step; visited[to.x][to.y] = true; Q.push(to); } } } return 0;}int main() { char s[3], e[3]; int ans; while (scanf("%s %s", s, e) == 2) { start.x = s[1] - '0'; start.y = s[0] - 'a' + 1; start.step = 0; end.x = e[1] - '0'; end.y = e[0] - 'a' + 1; if (start.x == end.x && start.y == end.y) { printf("To get from %s to %s takes 0 knight moves.\n", s, e); continue ; } ans = bfs(); printf("To get from %s to %s takes %d knight moves.\n", s, e, ans); } return 0;}
- HDU 1372 knight moves
- HDU 1372 Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 Knight Moves
- Hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU-1372Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- hdu-1372-Knight Moves
- HDU 1372 Knight Moves
- HDU-1372-knight moves
- hdu(1372) Knight Moves
- hdu 1372 Knight Moves
- hdu 1372 Knight Moves
- shell脚本中蛮量替换(将命令运行结果存入变量中)
- Java UML类图浅析
- hdu 4090 GemAnd Prince dfs+bfs+最优性剪枝
- struts2下的“Exception starting filter struts2”错误的问题
- hdu2647 && TOJ3648 Reward
- hdu 1372 Knight Moves
- SQL SERVER中的CHECKPOINT
- oracle 传输表空间
- Linux进程状态
- 离散化+树状数组 求逆序数POJ2299
- http协议学习系列教程
- hdu 4089 Activation(概率dp)
- 计算机原理学习(1)-- 冯诺依曼体系和CPU工作原理
- Android瀑布流