Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 56   Accepted Submission(s) : 41

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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<iostream>
#include<algorithm>
#include<string.h>
#include<cstdio>
#include<queue>
int d[8][2] = { {2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2} };
bool b[9][9];
using namespace std;
struct node
{
int x, y,step;
};
int main()
{
char a1[2], a2[2];
while (cin>>a1>>a2)
{
memset(b, 0, sizeof(b));
int x1, y1, x2, y2;
x1 = a1[0] - 'a' + 1; y1 = a1[1] - '0';
x2 = a2[0] - 'a' + 1; y2 = a2[1] -'0';
queue<node> P;
node dc; dc.x = x1, dc.y = y1,dc.step=0;
P.push(dc); b[x1][y1] = 1;
while (!P.empty())
{
node de = P.front(); P.pop();
if (de.x == x2&&de.y == y2)
{
printf("To get from %s to %s takes %d knight moves.\n", a1, a2, de.step);
break;
}
for (int i = 0; i < 8; i++)
{
int xx = de.x + d[i][0], yy = de.y + d[i][1], step2 = de.step + 1;
if (xx >= 1 && xx <= 8 && yy >= 1 && yy <= 8&&b[xx][yy]==0)
{
b[xx][yy] = 1;
dc.x = xx, dc.y = yy, dc.step = step2;
P.push(dc);
}
}
}
while (!P.empty()) P.pop();
}
return 0;
}

HDU Knight Moves相关推荐

  1. HDU 1372 Knight Moves

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

  2. poj - 2243 Knight Moves

    这题和poj 1915一样,用bfs做走马步.现在再看当时的代码,真是好幼稚啊. 1 #include <stdio.h> 2 #include <string.h> 3 in ...

  3. 1 Knight Moves

    Problem F- Knight Moves 题目来源:https://vjudge.net/contest/207868#problem/F Problem description: 题意概括:中 ...

  4. OpenJudge/Poj 1915 Knight Moves

    1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...

  5. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: 1 #include <iostream> 2 # ...

  6. Knight Moves

    Knight Moves poj-1915 题目大意:在国际象棋中有一个骑士,想从一个点到达另一个点.骑士可以从2*3的矩阵的一角跳到对角线的另一角.问:骑士最少需要跳多少次才能从一个点跳到另一个点. ...

  7. POJ2243 Knight Moves —— A*算法

    题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  8. zoj 1091 Knight Moves

    题目见zoj 1091 使用宽度搜索优先来求解,这个算法已经忘记的差不多了,所以写出来的代码很罗嗦,看起来很不清晰. 好像还可以直接用公式或者神经网络算法求解,详见Knight's Tour /* z ...

  9. (BFS)Knight Moves(hdu1372)

    题目: 在象棋王国,尼古拉斯.火山是一匹英俊的马,他非常幸运迎娶了白马王国的公主,他们将度蜜月,你现在是他们的女仆,火山会问你去一些地方最少需要多少步,这么简单的事当然难不倒你.由于火山是一匹马,他的 ...

最新文章

  1. LeetCode简单题之在长度 2N 的数组中找出重复 N 次的元素
  2. linux内核网络协议栈--ip层报文转发之ip_local_out()函数(六)
  3. start ssh-agent
  4. 居然是Firefox没有抛弃我们
  5. 左操作数必须为左值_进入“左转弯待转区”被罚怎么回事?
  6. 计算机基础应用的培养活动记录,小学少年宫计算机兴趣小组活动记录表
  7. 幸运数字(洛谷-P3292)
  8. 基于鸿蒙Hi3861和华为云平台的烟雾报警器(附源码)
  9. PHP100视频解压密码以及目录
  10. cad批量打印快捷键_批量打印CAD图(无删减版)
  11. 常见的弱口令字典1000~一石三鸟
  12. 计算机b类核心期刊有哪些,B类期刊推荐有哪些
  13. 谈谈我职业生涯中的三次潦倒 Leo病中的思考 续
  14. eovs实训报告总结心得_实训报告收获心得体会
  15. android实现wifi与移动数据的切换
  16. (二十七)张量表示定理 —— Cauchy 基本表示定理
  17. 云南省科协第十届学术年会在曲靖开幕
  18. TinyPng:在线PNG图片压缩工具
  19. python 列表迭代_Python | 以相反的顺序迭代列表
  20. 如何快速下载720yun场景图片

热门文章

  1. sublime安装插件常用的sublime插件
  2. java文件如何转换为class文件(HelloWorld举例)
  3. 图像识别的安全隐患:只用一张图片,你就可以实现隐身
  4. mybatis一对一关联查询
  5. c语言的argv,C语言的Argv的使用
  6. java —— 神奇的幻方
  7. Mysql高性能pdf阅读内容整理
  8. 你练到什么程度了(J2EE)
  9. 一篇文章带你搞定 Java 中 @Target、@Documented 和 @Inherited 注释
  10. moto z android7.1,联想新Moto Z曝光:骁龙835+安卓7.1.1,下月发?