POJ2243 Knight Moves(BFS)
来源:互联网 发布:java进阶书籍推荐 知乎 编辑:程序博客网 时间:2024/05/29 18:51
题意:
输入两组坐标,输出骑士从一个到另一个最少要几步(骑士走日字)
要点:
标准的BFS题,寻找最短路径,用队列做
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;bool visit[20][20];int dx[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };int dy[8] = { -1, 1, -2, 2, -2, 2, -1, 1 };struct node{int x;int y;char c;int num;};node a, b;queue<node> que;int xx, yy;void bfs(){que.push(a);visit[a.x][a.y] = false;while (!que.empty())//直到所有的点都遍历过{node temp = que.front();if (temp.x == b.x&&temp.y == b.y){printf("To get from %c%d to %c%d takes %d knight moves.\n", a.c, a.y, b.c, b.y, temp.num);return;}que.pop();for (int i = 0; i < 8; i++){node next;xx = temp.x + dx[i];yy = temp.y + dy[i];if (!visit[xx][yy] || xx < 1 || xx > 8 || yy < 1 || yy > 8)//越界就跳过continue;next.x = xx;next.y = yy;next.num = temp.num + 1;visit[xx][yy] = false;que.push(next);}}}int main(){while (~scanf("%c%d %c%d", &a.c, &a.y, &b.c, &b.y)){getchar();//除去换行符while (!que.empty())//每次都要要清空队列que.pop();a.x = a.c - 'a' + 1;b.x = b.c - 'a' + 1;a.num = 0;memset(visit, true, sizeof(visit));bfs();}return 0;}
0 0
- poj2243 Knight Moves (BFS)
- POJ2243 Knight Moves(BFS)
- poj2243-Knight Moves-水bfs
- poj2243 && hdu1372 Knight Moves(BFS)
- POJ2243 Knight moves (BFS求最短路)
- poj1915 & poj2243 Knight Moves
- poj2243 Knight Moves
- POJ2243 Knight Moves
- POJ2243 Knight Moves
- poj2243 Knight Moves--A*
- POJ2243 Knight Moves(A*算法)
- UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves
- POJ2243 Knight Moves(A*算法)
- POJ2243 Knight Moves —— A*算法
- Knight Moves hdu1372 bfs
- 【BFS】Knight Moves
- 1372 knight moves BFS
- Knight Moves(bfs)
- spring3.1.2整合quartz2.2.2 maven管理依赖
- Retrofit2+RxAndroid 详解
- Android EventBus框架
- ListView 列表显示
- WordPress杂志主题:Magazine
- POJ2243 Knight Moves(BFS)
- jquery知识小结
- 利用网线连接两台电脑
- Linux下的TCP Socket简单例子
- Minty主题下载-wordpress
- 9. iOS 开发中添加自定义汉语字体
- 关于值传递和引用传递的梳理
- 如何用消息系统避免分布式事务?(顶)
- AndroidStudio导入Eclipse项目并保留Eclipse目录结构