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. 在Eclipse中手动安装pydev插件,eclipse开发python环境配置

    最近在学习Python,因为我是做java的,用惯了eclipse,所以就想用eclipse开发python,但是配置开发环境的时候发现按照网上的配置大多不行,而且都是用的在线安装,很垃圾,没办法,自 ...

  2. 【2016-08-18】转载:总结C++中几种结构体初始化的方法

    作者:Ac_Von 博客地址:http://www.cnblogs.com/vongang/ 文章地址:http://www.cnblogs.com/vongang/archive/2011/07/3 ...

  3. win7画板橡皮擦改变大小

    按住CTRL键不放,再按小键盘上的“+”号键,可以将橡皮擦放大

  4. python基础——定制类

    python基础——定制类 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方 ...

  5. python基础——多重继承

    python基础——多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 回忆一下Animal类层次的设计,假设我们要实现以下4种动物: Dog - 狗狗: Bat ...

  6. printf 与c的基本类型之间的关系

    型 書式 注意事項 ssize_t %zd size_t %zu intmax_t %jd uintmax_t %ju ptrdiff_t %t signed char %hhd unsigned c ...

  7. spring中scope作用域(转)

    今天研究了一下scope的作用域.默认是单例模式,即scope="singleton".另外scope还有prototype.request.session.global sess ...

  8. Java中length,length(),size()区别

    length属性:用于获取数组长度. eg: int ar[] = new int{1,2,3} /** * 数组用length属性取得长度 */ int lenAr = ar.length;//此处 ...

  9. 重温WCF之会话Session(九)

    转载地址:http://blog.csdn.net/tcjiaan/article/details/8281782 每个客户端在服务器上都有其的独立数据存储区,互不相干,就好像A和服务器在单独谈话一样 ...

  10. windows 10 上office2016 word崩溃的解决方案

    方案1:--个人试验可行,把如下路径改成自己系统路径 C:Documents and Settings/Administrator/application/data/microsoft/templat ...