UVa 439 Knight Moves——bfs
2017-06-10 21:30
323 查看
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <queue> using namespace std; int G[10][10], sx, sy, ex, ey; struct Node { int x, y, step; }node; bool edge(int x, int y) { if (0 <= x && x < 8 && 0 <= y && y < 8) return true; else return false; } int bfs() { queue<Node> q; q.push((Node){sx, sy, 0}); while (!q.empty()) { node = q.front(); q.pop(); if (node.x == ex && node.y == ey) return node.step; if (edge(node.x - 2, node.y + 1)) q.push((Node){node.x-2, node.y+1, node.step+1}); if (edge(node.x - 1, node.y + 2)) q.push((Node){node.x-1, node.y+2, node.step+1}); if (edge(node.x + 1, node.y + 2)) q.push((Node){node.x+1, node.y+2, node.step+1}); if (edge(node.x + 2, node.y + 1)) q.push((Node){node.x+2, node.y+1, node.step+1}); if (edge(node.x + 2, node.y - 1)) q.push((Node){node.x+2, node.y-1, node.step+1}); if (edge(node.x + 1, node.y - 2)) q.push((Node){node.x+1, node.y-2, node.step+1}); if (edge(node.x - 1, node.y - 2)) q.push((Node){node.x-1, node.y-2, node.step+1}); if (edge(node.x - 2, node.y - 1)) q.push((Node){node.x-2, node.y-1, node.step+1}); } } int main() { string temp1, temp2; while (cin >> temp1 >> temp2) { sx = 8 - temp1[1] + '0'; sy = temp1[0] - 'a'; ex = 8 - temp2[1] + '0'; ey = temp2[0] - 'a'; cout << "To get from "<<temp1<<" to "<<temp2<<" takes "<<bfs()<<" knight moves." << endl; } return 0; }
相关文章推荐
- UVA 439 Knight Moves 基础BFS
- uva 439 Knight Moves 骑士移动 —— bfs
- uva 439 Knight Moves(骑士的移动)BFS
- UVA 439 Knight Moves (BFS)
- [uva-439]Knight Moves(BFS)
- UVa439---Knight Moves(BFS应用)
- uva439 - Knight Moves(BFS求最短路)
- UVa 439 - Knight Moves (BFS)
- UVA 439 骑士(Knight Moves ) 很基础的BFS
- uva 439 Knight Moves(BFS)
- 【UVA439】 Knight Moves BFS (3/1000)
- UVA 439 Knight Moves --DFS or BFS
- UVa - 439 - Knight Moves(bfs求最短路)
- Knight Moves UVA - 439(BFS典例)
- UVA 439-Knight Moves(bfs)
- UVA------439 - Knight Moves(bfs简单)
- uva 439 Knight Moves(BFS遍历)
- uva-439 - HDOJ-1372-Knight Moves-翻译详解-BFS
- UVA 439 Knight Moves 走象棋 (DFS or BFS)
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)