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. CF1324E Sleeping Schedule 题解

    原题链接 简要题意: 每次可以将 \(a_i\) 减 \(1\) 或不变.求让 \(a_i\) 的前缀和 \(\% h\) 的值在 \([l,r]\) 区间中的最多的个数. E题是个水dp,也不怎样 ...

  2. 图-最短路-dijkstra-0/1BFS-1368. 使网格图至少有一条有效路径的最小代价

    2020-03-01 22:59:59 问题描述: 给你一个 m x n 的网格图 grid . grid 中每个格子都有一个数字,对应着从该格子出发下一步走的方向. grid[i][j] 中的数字可 ...

  3. 算法训练 瓷砖铺放 【递归】java

      算法训练 瓷砖铺放   时间限制:1.0s   内存限制:512.0MB     锦囊1 锦囊2 锦囊3 问题描述 有一长度为N(1<=N<=10)的地板,给定两种不同瓷砖:一种长度为 ...

  4. 福利,OpenCV最新中文版官方教程来了

    OpenCV 中文版官方教程来了. OpenCV是计算机视觉中经典的专用库,然而其中文版官方教程久久不来.近日,一款最新OpenCV4.1 版本的完整中文版官方教程出炉,读者朋友可以更好的学习了解Op ...

  5. 一文上手Tensorflow2.0(四)

    系列文章目录: Tensorflow2.0 介绍 Tensorflow 常见基本概念 从1.x 到2.0 的变化 Tensorflow2.0 的架构 Tensorflow2.0 的安装(CPU和GPU ...

  6. zookeeper 负载均衡

    1,原理 将启动的服务注册到zookeeper 注册中心上面,采用临时节点,zookeeper 客户端从注册中心上读取服务的信息,之后再本地采用负载均衡算法(取模算法),将请求轮询到每个服务. 同时z ...

  7. html第一个程序

    2020-04-05  每日一例第27天 1.打开记事本,输入html格式语言: 2.后台代码注释: <html> <head><!--标题语句--> <ti ...

  8. Python中类型的概念(一)

    本课程主要介绍6种Python语言中的类型:数字类型.字符串类型.元组类型.列表类型文件类型.字典类型 1.数字类型 Python语言包括三种数字类型:整数类型.浮点数类型.复数类型 (1)整数类型 ...

  9. 新安装的eclipse配置好了环境变量后,打开还是出现A Java runtime environment错误

    新安装的eclipse配置好了环境变量后,打开还是出现如下图的A Java runtime environment错误; 解决方法: 第一步: Windows环境下:把C:\Users\你的用户名 目 ...

  10. Light of future-冲刺Day 3

    目录 1.SCRUM部分: 每个成员进度 SCRUM 会议的照片 签入记录 代码运行截图 发布界面 用户浏览界面 管理员浏览界面 2.PM 报告: 时间表 燃尽图 任务总量变化曲线 每名成员的贡献比 ...