poj2243 Knight Moves(BFS)
题目链接
http://poj.org/problem?id=2243
题意
输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径。
思路
使用bfs求解,注意国际象棋中马的走法。
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
using namespace std; struct Node
{
int r, c;
int steps; Node(int r, int c, int s) :r(r), c(c), steps(s) {}
}; const int N = ;
int boasd[N][N];
int visit[N][N];
int dir[][] = { {-, -}, {-, -}, {-, }, {-, }, {, -}, {, -}, {, }, {, } };
string ss, se;
int sr, sc;
int er, ec; void bfs()
{
memset(visit, , sizeof(visit));
queue<Node> q;
q.push(Node(sr, sc, ));
visit[sr][sc] = ;
while (!q.empty())
{
Node node = q.front();
q.pop();
if (node.r == er && node.c == ec)
{
printf("To get from %s to %s takes %d knight moves.\n", ss.c_str(), se.c_str(), node.steps);
return;
}
for (int i = ; i < ; i++)
{
int nr = node.r + dir[i][];
int nc = node.c + dir[i][];
if (nr > && nr <= && nc > && nc <= && !visit[nr][nc])
{
visit[nr][nc] = ;
q.push(Node(nr, nc, node.steps + ));
}
}
}
} int main()
{
//freopen("poj2243.txt", "r", stdin);
while (cin >> ss >> se)
{
sr = ss[] - '';
sc = ss[] - 'a' + ;
er = se[] - '';
ec = se[] - 'a' + ;
bfs();
}
return ;
}
poj2243 Knight Moves(BFS)的更多相关文章
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
- HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- ZOJ 1091 Knight Moves(BFS)
Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...
- poj1915 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...
- uva439 - Knight Moves(BFS求最短路)
题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...
- Knight Moves(BFS,走’日‘字)
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- K8S dashboard 创建只读账户
1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...
- Centos下 自动化配置SSH免密码登陆
hosts文件,存储要部署的节点IP地址,其中以#开头表示注释掉 192.168.101.52 192.168.101.53 192.168.101.54 192.168.101.55 192.168 ...
- 2017北京国庆刷题Day2 afternoon
期望得分:100+100+50=250 实际得分:100+70+50=220 T1 最大值(max) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一 ...
- 关于Http协议、ASP.NET 核心知识(2)
简介HTTP (对于http协议的描述我前部分有写,但基于保证文档独立完整性的原则,我再写一遍.反正又不花钱.) 这货的学名叫:超文本传输协议 英文名字:(HTTP,HyperText Transfe ...
- 多进程Process
多进程旧式写法 from multiprocessing import Pool def f(x): return x*x if __name__ == '__main__': p = Pool(5) ...
- 关于servlet中重定向、转发的地址问题
先写一个正斜杠"/",再判断是服务器使用该地址还是网站使用该地址. 访问网络资源用/,访问硬盘资源用\. 例如: 转发: request.getRequestDispat ...
- APP版本号记录
VoLTE版本: VT_BV0800V1.0.0B06 800M版本: NETARTIST_BV0800V1.0.0B01 看详细版本号:9831275#
- python基础===拆分字符串,和拼接字符串
给定某字符,只需要保留其中的有效汉字或者字母,数字之类的.去掉特殊符号或者以某种格式进行拆分的时候,就可以采用re.split的方法.例如 ============================== ...
- photoshop 安装问题
问题:“安装程序检测到计算机重新启动操作可能处于挂起状态.建议您退出安装程序,重新启动并重试.” 解决: 1.运行 regedit 打开注册表编辑器. 2.依次展开HKEY_LOCAL_MACHINE ...
- Spring 控制台运行及RestTemplate实现Eurka负载均衡
spring使用控制台运行方式 spring.main.web-application-type=none新老版本的配置有点差异 Maven的modules只是实现了一个顺序编译,一次多个项目一起生成 ...