Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14125    Accepted Submission(s): 8269

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.
 



Source
 



Recommend
Eddy   |   We have carefully selected several similar problems for you:  1072 1240 1312 1241 1016 
 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
本题为典型的bfs深搜的模板题
本题只要了解bfs算法就能AC
但是要对国际象棋中的马的运动方式熟悉一下就没问题了
下面上我注释了的
谁都能看懂的代码
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,step;
};
queue<node> q;
int dir[][]={{-,-},{-,-},{-,},{-,},{,},{,-},{,-},{,}};//马所能够跳的八个方向记录下来
int vis[][];
char temp[][];//定义一个字符串用于输入
int change(char c)//字符转数字
{
switch (c)
{
case 'a':return ;
break;
case 'b':return ;
break;
case 'c':return ;
break;
case 'd':return ;
break;
case 'e':return ;
break;
case 'f':return ;
break;
case 'g':return ;
break;
case 'h':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
}
}
int bfs(int x,int y,int x1,int y1)
{
while(!q.empty())//队列的初始化,全部清空
q.pop();
int i;
q.push(node{x,y,});
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x1&&t.y==y1)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<&&dy<&&!vis[dx][dy])//基本搜索
{
vis[dx][dy]=;
q.push(node{dx,dy,t.step+});
}
}
}
return ;
}
int main()
{
while(scanf("%s%s",temp[],temp[])!=EOF)
{
memset(vis,,sizeof(vis));
int x=change(temp[][]);
int y=change(temp[][]);
int x1=change(temp[][]);
int y1=change(temp[][]);//确定首尾点
int ans=bfs(x,y,x1,y1);
printf("To get from %s to %s takes %d knight moves.\n",temp[],temp[],ans);//输出
}
return ;
}

2018-11-16  00:03:31  Author:LanceYu

HDU 1372 Knight Moves 题解的更多相关文章

  1. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  3. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  4. HDU 1372 Knight Moves

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

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

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

  6. HDU 1372 Knight Moves (bfs)

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

  7. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  8. ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves Time Limit: 2 Seconds      Memory Limit: 65536 KB A friend of you is doing research on ...

  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. 集合 List ,Set

    https://www.cnblogs.com/jmsjh/p/7740123.html

  2. 数据结构——链队列(linked queue)

    /* linkedQueue.c */ /* 链队列 */ #include <stdio.h> #include <stdlib.h> #include <stdboo ...

  3. 日常歌颂zyj

    今年的中秋节... 我貌似遇到了一个灰常 灰常灰常优秀的 大哥哥~~ (貌似是条高二狗) 最开始在贴吧颓废... 然后... 开始逐条的回复... 开始去,,, 逐步查看,,, 发现这个优秀的楼主会 ...

  4. python读写、创建文件、文件夹等等

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  5. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  6. loj 2955 「NOIP2018」保卫王国 - 树链剖分 - 动态规划

    题目传送门 传送门 想抄一个短一点ddp板子.然后照着Jode抄,莫名其妙多了90行和1.3k. Code /** * loj * Problem#2955 * Accepted * Time: 26 ...

  7. [原创]A/B测试系统调研思维导图

    [原创]A/B测试系统调研思维导图

  8. vue+elementui搭建后台管理界面(1登录)

    1 node环境安装 从 node官网下载安装包 2 vue-cli npm install vue-cli -g 3 新建项目 vue init webpack vue-project 可保持默认, ...

  9. presto整合hive

    Presto安装   前提条件: hadoop安装好了(并启动了) + hive安装好了        文档网址:http://prestodb.jd.com/docs/current/install ...

  10. Windwos Server 2016 远程桌面授权

    https://blog.csdn.net/hanzheng260561728/article/details/80443135 第二步激活客户的的时候,注意事项