E - Knight Moves

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream> using namespace std; int startx,starty,endx,endy;
char a[3],c[3];
int b[100][100]; int dir[8][2]= {1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};//往八个方向搜 struct node
{
int x,y,step;
}; void bfs(int x,int y)
{
node st,ed;
memset(b,0,sizeof(b));
queue<node>q;
st.x=x;
st.y=y;
st.step=0;
b[st.x][st.y]=1;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==endx&&st.y==endy)
{
printf("To get from %s to %s takes %d knight moves.\n",a,c,st.step);
return;
}
for(int k=0; k<8; k++)
{
ed.x=st.x+dir[k][0];
ed.y=st.y+dir[k][1];
if(ed.x>=1&&ed.x<=8&&ed.y>=1&&ed.y<=8&&!b[ed.x][ed.y])
{
b[ed.x][ed.y]=1;
ed.step=st.step+1;
q.push(ed);
}
}
}
} int main()
{
while(~scanf("%s%s",a,c))
{
startx=a[0]-'a'+1;//把字母转化为数字模拟地图
starty=a[1]-'0';
endx=c[0]-'a'+1;
endy=c[1]-'0';
bfs(startx,starty);
}
return 0;
}


hdu 1372Knight Moves的更多相关文章

  1. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

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

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

  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

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  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. 【BZOJ】2165: 大楼

    [题意]从第0层开始有无穷层,每层有n个房间,给定矩阵A,A[i][j]表示从第x层的房间 i 可以跳到第x+A[i][j]层的房间 j (x任意),A[i][j]=0表示不能跳.初始在第0层第1个房 ...

  2. 【CodeForces】932 E. Team Work

    [题目]E. Team Work [题意]给定n和k,n个人中选择一个大小为x非空子集的代价是x^k,求所有非空子集的代价和%1e9+7.n<=10^9,k<=5000. [算法]斯特林反 ...

  3. NYOJ 1012 RMQ with Shifts (线段树)

    题目链接 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each q ...

  4. C - Contest Setting Gym - 101982C dp 补题

    题目链接:https://vjudge.net/contest/273260#problem/C 学习了一下别人的思路,首先去重,然后离散化. dp数组开二维,每一次更新,状态转移方程,dp[ i ] ...

  5. Hive ORC表的使用

    创建普通临时表: create table if not exists test_orc_tmp(   name string,   gender string,   cnt BIGINT )row ...

  6. .htaccess技巧: URL重写(Rewrite)与重定向(Redirect)

    URL重定向是.htaccess的重头戏,它可以将长地址转为短地址.将动态地址转为静态地址.重定向丢失的页面.防止盗链.实现自动语言转换等.笔者觉得难点是在正则表达式的运用和理解上. 实现所有这些神奇 ...

  7. Mathtype公式位置偏上

    Mathtype公式位置偏上 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公式的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题.然后就是行距稍微大 ...

  8. linux CentOS 上安装chrome

    1.wget http://chrome.richardlloyd.org.uk/install_chrome.sh   (用wget下载shell文件)2.chmod u+x install_chr ...

  9. Ubuntu下Hadoop的安装和配置

    最近又需要要搭hadoop环境,所以开始学习,下面是我的笔记,仅供大家参考! Hadoop安装: JDK1.6+ 操作系统:Linux,Window和Unix也可以做Hadoop的开发,只有Linux ...

  10. Appium+python 一个简单的登录测试实例

    # coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTestRun ...