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.

InputThe 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. 
OutputFor 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.

思路 :骑士每次移动只能移动一个方格 其实就是一个日字(具体原因我也不知道)8个方向,,直接用BFS查找就可以了

#include<iostream>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
struct stu{
int a,b;
};
char a,b;
int aa,bb;
int arr[][];
int arr1[][];
int arr2[][];
int ar[][]={{-,-},{-,},{,},{,-},{,},{,-},{-,},{-,-}};
int bfs(int x1,int y1,int x2,int y2){
memset(arr1,,sizeof(arr1));
queue<stu>que;
que.push({x1,y1});
arr1[x1][y1]=;
arr2[x1][y1]=;
while(que.size()){
int x=que.front().a;
int y=que.front().b;
que.pop();
int dx,dy;
for(int i=;i<;i++)
{
dx=x+ar[i][];
dy=y+ar[i][];
if(dx>=&&dx<&&dy>=&&dy<&&arr1[dx][dy]!=){
arr1[dx][dy]=;
arr2[dx][dy]=arr2[x][y]+;
que.push({dx,dy});
if(dx==x2&&dy==y2){
return arr2[dx][dy];
}
}
}
}
return -;
}
int main()
{
while(~scanf("%c%d %c%d",&a,&aa,&b,&bb)){
getchar();
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
arr[i][j]=;
}
}
int x1=a-'a';
// cout<<x1<<endl;
int y1=aa-;
// cout<<y1<<endl;
int x2=b-'a';
// cout<<x2<<endl;
int y2=bb-;
// cout<<y2<<endl;
arr[x1][aa-]=;
arr[x2][bb-]=;
if(x1==x2&&y1==y2)
printf("To get from %c%d to %c%d takes 0 knight moves.\n",a,aa,b,bb);
else {
int y=bfs(x1,y1,x2,y2);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,aa,b,bb,y);
}
} return ;
}

H - Knight Moves DFS的更多相关文章

  1. UVA 439 Knight Moves --DFS or BFS

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

  2. 【UVa】439 Knight Moves(dfs)

    题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <a ...

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

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

  4. HDU 1372 Knight Moves (bfs)

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

  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. OpenJudge/Poj 1915 Knight Moves

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

  8. HDU-1372 Knight Moves (BFS)

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

  9. HDOJ/HDU 1372 Knight Moves(经典BFS)

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

随机推荐

  1. 洛谷 P3935 Calculating 题解

    原题链接 一看我感觉是个什么很难的式子-- 结果读完了才发现本质太简单. 算法一 完全按照那个题目所说的,真的把质因数分解的结果保留. 最后乘. 时间复杂度:\(O(r \sqrt{r})\). 实际 ...

  2. AQS源码详细解读

    AQS源码详细解读 目录 AQS源码详细解读 基础 CAS相关知识 通过标识位进行线程挂起的并发编程范式 MPSC队列的实现技巧 代码讲解 独占模式 独占模式下请求资源 独占模式下的释放资源 共享模式 ...

  3. 02 LED翻转与计数器使用

    一.  设计定义: 计数器设计与验证 LED,每500ms,状态翻转一次也就是亮灭. 第一步: 系统时钟频率为50M,对应为T= =20ns 计数周期或者时间是500ms,计数次数的计算: 计数值=( ...

  4. C# 基础知识系列- 6 Lambda表达式和Linq简单介绍

    前言 C#的lambda和Linq可以说是一大亮点,C#的Lambda无处不在,Linq在数据查询上也有着举足轻重的地位. 那么什么是Linq呢,Linq是 Language Intergrated ...

  5. Tensorboard 详解(上篇)

    花间提壶华小厨 1. Tensorboard简介 对大部分人而言,深度神经网络就像一个黑盒子,其内部的组织.结构.以及其训练过程很难理清楚,这给深度神经网络原理的理解和工程化带来了很大的挑战.为了解决 ...

  6. 知识图谱与机器学习|KG入门 -- Part2 建立知识图谱

    介绍 在本系列前面两篇文章中我一直在讨论Data Fabric,并给出了一些关于Data Fabric中的机器学习和深度学习的概念.并给出了我对Data Fabric的定义: Data Fabric是 ...

  7. CNN更新换代!性能提升算力减半,还即插即用

    传统的卷积运算,要成为过去时了. Facebook和新加坡国立大学联手提出了新一代替代品:OctConv(Octave Convolution),效果惊艳,用起来还非常方便. OctConv就如同卷积 ...

  8. NSObject常用方法

    类 @interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; } // 初始化加载 + (void)load; / ...

  9. mybatis 入门基础

    一.Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...

  10. MATLAB——m_map指南(1)

    1.例图 (1) clear all m_proj('oblique mercator');%确定投影方式和绘图界线 m_coast;%画出海岸线 m_grid;%添加格网 第一行代码初始化投影,对于 ...