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 ...
随机推荐
- linux中使用随机数
(1)单纯使用rand重复调用n次,就会得到一个0-RAND_MAX之间的伪随机数,如果需要调整范围,可以得到随机数序列后再进行计算.(2)单纯使用rand来得到伪随机数序列有缺陷,每次执行程序得到的 ...
- 前端PHP入门-031-文件上传-六脉神剑
php.ini的设置 php.ini的文件太多,找不到的时候你可以使用 Ctrl+F 搜索相关配置项. 配置项 功能说明 file_uploads on 为开启文件上传功能,off 为关闭 post_ ...
- [实战篇]Tomcat发布项目-虚拟目录
在二阶段学习的过程中,我一直使用MyEclipse的方式把工作空间的项目发布到webapps目录下,这种方式自我感觉在实际开发中应用能在70%左右,但是如何涉及到一些上传操作等操作, 从新发布项目之后 ...
- UVA 12063 Zeros and Ones
https://vjudge.net/problem/UVA-12063 题意: 统计n为二进制数中,0和1相等且值为m的倍数的数有多少个 dp[i][j][k] 前i位二进制 有j个1 值模m等于k ...
- STL在算法比赛中简单应用
STL基础 和 简单的贪心问题 STL(Standard Template Library) 即 标准模板库. 它包含了诸多在计算机科学领域里所常用的基本数据结构和算法.这些数据结构可以与标准算法一起 ...
- dotnet core 实践——日志组件Serilog
前几天把基于quartz.net的部分项目代码移植到了dotnet core ,但是没增加日志功能,原因是没找到合适的组件. 今天终于找到了Serilog: https://github.com/s ...
- 【CodeForces】889 C. Maximum Element 排列组合+动态规划
[题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...
- win10本地搭建php运行环境
一.下载搭建环境所需软件,安装顺序也要按照列表顺序安装 1.Vc2015(根据需要安装Vc2012或者Vc2015) Vc2015:https://www.microsoft.com/zh-CN/do ...
- 【leetcode 简单】第三十七题 相交链表
编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: 如果两个 ...
- 天梯赛L2-008 最长对称子串 (字符串处理)
对给定的字符串,本题要求你输出最长对称子串的长度.例如,给定"Is PAT&TAP symmetric?",最长对称子串为"s PAT&TAP s&quo ...