题目链接

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.

分析:

题意求是求a点跳到b点。

一开始以为是简单的四个方向,发现步数老是不对,从a1到h8走了十几步,而案例只要6步。此时我就开始想可能不是简单的四个方向,开会仔细的验算案例。

可以看倒数第二个案例,从b1到c3,只要一步。对角线构成的矩形变成了有点”日“的形状,这正是象棋里的马走日。

所以是跳马。众所周知,中国象棋的马可以往八个方向跳。

所以改下方向。把输入弄好。

一个裸BFS。就能求出最小的步数了

代码:

#include <stdio.h>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
int vis[10][10];
int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}}; //马的八个方向
int sx,sy;
struct node
{
int x,y;
int step;
}
;
bool check(int x,int y)
{
if(x>8 ||x<1 ||y>8 ||y<1 ||vis[x][y])
return 0;
return 1;
}
int bfs(int x,int y)
{
node st,ed;
int i;
queue<node>q;
st.x=x;
st.y=y;
st.step=0;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==sx &&st.y==sy)
return st.step;
for(i=0;i<8;i++)
{
ed.x=st.x+dir[i][0];
ed.y=st.y+dir[i][1];
if(!check(ed.x,ed.y))
continue;
ed.step=st.step+1;
vis[ed.x][ed.y]=1;
q.push(ed);
}
}
}
int main()
{
char s[22];
char s1[22];
int x2,y2;
int i,j;
while(scanf("%s%s",s,s1)!=EOF)
{
sx=s1[0]-'a'+1;
sy=s1[1]-'0';
x2=s[0]-'a'+1;
y2=s[1]-'0'; // 控制好输入
// printf("%d %d\n",x2,y2); // 用这个可以检查转为后的坐标
// printf("%d %d\n",sx,sy);
memset(vis,0,sizeof(vis));
vis[x2][y2]=1;
int ans=bfs(x2,y2); //裸BFS
printf("To get from %s to %s takes %d knight moves.\n",s,s1,ans);
}
return 0;
}

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

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

  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. HDOJ/HDU 1372 Knight Moves(经典BFS)

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

  6. HDU 1372 Knight Moves(bfs)

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

  7. HDU 1372 Knight Moves 题解

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

  8. HDU 1372 Knight Moves【BFS】

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

  9. 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 ...

随机推荐

  1. OSG配置捷径,VS2013+WIN10

    在自己电脑上用CMAKE已经编译好了,上传到百度云里面了. 环境是WIN10+VS2013. 链接:http://pan.baidu.com/s/1hrO7GFE 密码:fwkw 解压之后放在C盘或者 ...

  2. PHPCMSV9 黄页新闻、产品、商机均无法浏览具体信息,显示您没有访问该信息的权限!

    原帖地址:http://bbs.phpcms.cn/forum.php?mod=viewthread&tid=294956&highlight=%C3%BB%D3%D0%B7%C3%C ...

  3. mysql突然无法启动的问题

    经常会有这样一个情况是:mysql跑了一段时间后,某一天我们需要重启服务的时候,发现停止后并不能正常启动,会报下面这种错误 这种情况发生的原因绝大多数都是权限的问题: 因为使用了一段时间后,使用期间表 ...

  4. React Components Template

    React Components Template "use strict"; /** * * @author xgqfrms * @license MIT * @copyrigh ...

  5. [剑指Offer] 51.构建乘积数组

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  6. poj 1719 Shooting Contest (二分匹配)

    Shooting Contest Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3812   Accepted: 1389 ...

  7. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  8. BZOJ4869:[SHOI2017]相逢是问候——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=4869 题面复制于洛谷:https://www.luogu.org/problemnew/show/P ...

  9. BZOJ2705:[SDOI2012]Longge的问题——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2705 Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在 ...

  10. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...