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. 表格的删除与添加以及id的唯一性

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. jviisualvm监控远程主机java程序实战与问题排查

    1.远程主机运行jstatd 首先新建文件 jstatd.all.policy ,内容如下 grant codebase "file:${java.home}/../lib/tools.ja ...

  3. Oracle数据库的创建表全

    CREATE TABLE "库名"."表名" ( "FEE_ID" VARCHAR2(10 BYTE) constraint ABS_FEE ...

  4. node.js初步

    Node.js介绍 Node.js 诞生于2009年,Node.js采用C++语言编写而成,是 一个Javascript的运行环境.Node.js 是一个基于 Chrome V8 引擎的 JavaSc ...

  5. 基于 HTML5 WebGL 的故宫人流量动态监控系统

    前言 在当代社会,故宫已经成为一个具有多元意义的文化符号,在历史.艺术.文化等不同领域发挥着重要的作用,在国际上也成为能够代表中国文化甚至中国形象的国际符号.近几年故宫的观众接待量逐年递增,年接待量已 ...

  6. 深度学习、物联网专家Sunil Kumar Vuppala博士独家专访

    介绍 有多种方法可以学习数据科学,机器学习和深度学习概念.您可以观看视频,阅读文章,参加课程,参加会议等.但是有一件事是无法替代的----经验. 我个人从与数据科学专家和行业领袖的交流中学到了很多.他 ...

  7. Vue history路由模式 apache配置上线

    1. 首先在vue项下的router.js 文件配置 mode为history模式,并且设置好对应的base选项 说明:base配置为你当前项目实际上线时所在的目录文件夹, 我这就是放在站点的根目录下 ...

  8. coding++:Spring_IOC(控制反转)详解

    IoC是什么: 1):Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 2):在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的 ...

  9. SpringBoot中遇到的一些问题

    1.JQuery和bootstrap报404的问题 在html页面导入的js和css的时候,不要加static这级目录,直接跳过即可,例如 导入的时候不需要加static目录,直接导入js/和css/ ...

  10. SQL server 2008 简介

    一.简介 网状模型 关系模型(独立表) 拆分成有主键的表.连接表即可. 工资与奖金有了依赖关系.所以可以不保存奖金,计算得出结果. 二. 1. 2.环境配置 安装iis服务 https://jingy ...