Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7661 Accepted Submission(s): 4567
friend of you is doing research on the Traveling Knight Problem (TKP)
where you are to find the shortest closed tour of knight moves that
visits each square of a given set of n squares on a chessboard exactly
once. He thinks that the most difficult part of the problem is
determining the smallest number of knight moves between two given
squares and that, once you have accomplished this, finding the tour
would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your
job is to write a program that takes two squares a and b as input and
then determines the number of knight moves on a shortest route from a to
b.
input file will contain one or more test cases. Each test case consists
of one line containing two squares separated by one space. A square is a
string consisting of a letter (a-h) representing the column and a digit
(1-8) representing the row on the chessboard.
#include<cstdio>
#include<cstring>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char s[3],s2[3];
int vis[10][10];
struct node{
int x,y;
int step_cnt;
};
node now,nex;
node vs,vd;
int check(node v)
{
if(v.x>=1&&v.x<=8&&v.y>=1&&v.y<=8&&!vis[v.x][v.y])
return 1;
else return 0;
}
void bfs()
{
queue<node>que;
vs.step_cnt=0;
que.push(vs);
vis[vs.x][vs.y]=1;
while(!que.empty()){
now=que.front();
que.pop();
if(now.x==vd.x&&now.y==vd.y) return;
nex.x=now.x+2;nex.y=now.y+1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x+2;nex.y=now.y-1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-2;nex.y=now.y+1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-2;nex.y=now.y-1;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x+1;nex.y=now.y+2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x+1;nex.y=now.y-2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-1;nex.y=now.y+2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} nex.x=now.x-1;nex.y=now.y-2;nex.step_cnt=now.step_cnt+1;
if(check(nex)) {que.push(nex);vis[nex.x][nex.y]=1;} }
}
int main()
{
while(~scanf("%s%s",s,s2))
{
memset(vis,0,sizeof(vis));
vs.x=s[1]-'0';
vs.y=s[0]-96;
vd.x=s2[1]-'0';
vd.y=s2[0]-96;
bfs();
printf("To get from %s to %s takes %d knight moves.\n",s,s2,now.step_cnt);
}
}
给一个棋盘,算出马从一个点走到另一个点的最小步数(马走日)
Knight Moves的更多相关文章
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- hdu Knight Moves
这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...
- HDU 1372 (搜索方向稍有改变) Knight Moves
其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
随机推荐
- 20145213《Java程序设计》第五周学习总结补充
20145213<Java程序设计>第五周学习总结补充 教材学习内容总结 欠的账都是要还的!第九章的内容躲过对酒当歌的夜,躲不过四下无人的街.由于第五周贪玩,疏忽冷落了Collection ...
- September 20th 2016 Week 39th Tuesday
Failure is not fatal, but failure to change might be. 失败并不致命,但无法改变却可能是致命的. I need change, but it see ...
- 立方体旋转 【web前端学习部落22群120342833】
效果: HTML部分: <body class="body"> <div class="rect-wrap"> <!-- // ...
- 面向对象的小demo
两个类如下 package cao.com.duixiang; import java.util.Arrays; public class OtherTest { //求max public int ...
- Android -- FragmentActivity添加Fragment的序列图
FragmentActivity添加Fragment的序列图
- C#学习笔记--详解委托,事件与回调函数
.Net编程中最经常用的元素,事件必然是其中之一.无论在ASP.NET还是WINFrom开发中,窗体加载(Load),绘制(Paint),初始化(Init)等等.“protected void Pag ...
- 验证码的种类与实现 C#封装类 - .NET MVC WEBFORM
验证码方式 1.随机字母或者数字,纯文本验证码 这种非常容易破解 ,市场上有大量的现成接口或者工具,背景越复杂难度越高. 2.题库验证码 要破解这种验证码,需要人工收集题库才可以破解,可以免疫不是专门 ...
- WPF框架MVVM简单例子
MVVM是Model-View-ViewModel的缩写形式,它通常被用于WPF或Silverlight开发.Model——可以理解为带有字段,属性的类.View——可以理解为我们所看到的UI.Vie ...
- Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常
问题:显卡驱动异常. 缘由:驱动精灵万能显卡安装系统 解决方案:根据笔记本型号去官网下载适配显卡驱动.
- ExcelReport第一篇:使用ExcelReport导出Excel
导航 目 录:基于NPOI的报表引擎——ExcelReport 下一篇:ExcelReport源码解析 概述 本篇将通过导出学生成绩的示例演示“使用ExcelReport导出Excel”的步骤. ...