UVa 439 Knight Moves——bfs

网友投稿 817 2022-11-07

UVa 439 Knight Moves——bfs

UVa 439 Knight Moves——bfs

#include #include #include #include #include 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 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 "<

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:UVa 536 Tree Recovery——树的遍历
下一篇:UVA 120 Stacks of Flapjacks ——思路题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~