Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7661    Accepted Submission(s): 4567

Problem Description
A
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
The
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.
 
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
 
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
 
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
 
#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);
}
}

给一个棋盘,算出马从一个点走到另一个点的最小步数(马走日)

Source

Knight Moves的更多相关文章

  1. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  2. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  3. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  4. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  5. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  6. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  7. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  8. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  9. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

随机推荐

  1. 【leetcode】Implement strStr() (easy)

    Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...

  2. 【Git】笔记3

    来源:廖雪峰 远程仓库 远程仓库采用github 准备工作:创建远程仓库 1.创建一个github账号 2.在本地设置ssh,获取/home/user/.ssh/id_rsa.pub内容 3.在git ...

  3. HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...

  4. 9.12/ css3拓展、js基础语法、程序基本知识、数据类型、运算符表达方式、语句知识点

    css3拓展: <display:none>  将某个元素隐藏       <visibility:hidden>  也是将某个元素隐藏 <display:block&g ...

  5. Does the OpenSceneGraph have a native file format?

    From OpenSceneGraph-3.0 onwards we have new native file formats based on generic serializers that ar ...

  6. -bash: pod: command not found

    OS X 系统没升级之前用的 cocoapods 一点儿问题都没有,但是升级成版本10.11.4 OS X EI Capitan之后,在终端除了cd 指令可以用之外,其他任何指令输入都是提示-bash ...

  7. instanceof、 isinstance 与 isAssignableFrom的区别

    instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false. 形象地:自身实例或子类实例 instanc ...

  8. jq点击和鼠标移上效果以及一个等号"=" 二个等号"==" 三个等号"===" 的区别

    <body> <div class="a" bs='1' style="width:100px; height:30px; border:1px sol ...

  9. Pyqt QSystemTrayIcon 实现托盘效果

    pyqt的托盘效果很好实现,在Pyqt的demo中有个例子 路径:PyQt4\examples\desktop\systray.py 今天我就仿这个Tray效果做效果 一. 创建UI trayicon ...

  10. GoLang文件增删遍历基本操作

    先学一学GO语言实用的一面. package main import ( "path/filepath" "flag" "os" " ...