博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2243 Knight Moves(BFS)
阅读量:7092 次
发布时间:2019-06-28

本文共 1295 字,大约阅读时间需要 4 分钟。

题意:

输入两组坐标,输出骑士从一个到另一个最少要几步(骑士走日字)

要点:

标准的BFS题,寻找最短路径,用队列做

15170656 Accepted 176K 16MS 1099B 2016-02-17 17:36:15
#include
#include
#include
#include
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
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;}

转载于:https://www.cnblogs.com/seasonal/p/10343848.html

你可能感兴趣的文章
Scribe快速安装方法
查看>>
5-1 array 数组的基本概念
查看>>
httpclient4.3 设置cookie
查看>>
dd懒也是一种境地
查看>>
使用SQL语句中的Group by分组并计算每组的数量
查看>>
ThinkPHP自定义标签
查看>>
Ioc容器
查看>>
Eclipse插件开发 RCP生成jar包后获取jar包中的Plugin/Bundle文件资源——以FreeMarker为例...
查看>>
String去掉后面空格
查看>>
Linux常用命令(1)
查看>>
linux命令
查看>>
Adobe吸引世界目光 数字出版让生活更精彩——软盛携Adobe DPS闪耀2013中国武汉期刊交易博览会...
查看>>
程序员之路
查看>>
Windows Server 2012 Hyper-V新特性(10)
查看>>
不指定文件类型日志
查看>>
4. 抽象方法
查看>>
convert time-24小时制转换为12小时制
查看>>
MISP2:初始阶段
查看>>
在Linux下创建空文件的方法
查看>>
项目整体管理
查看>>