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

题意:骑士走法是日字型,或说L型,给出起点和终点,求起点到终点的最少步数

分析:广搜(bfs)

#include<stdio.h>
#include<string.h>
int vis[][];
int fx[]= {,,-,-,,,-,-},
fy[]= {,-,,-,,-,,-};
struct node
{
int x,y,step;
bool operator == (const node b)
{
return x==b.x&&y==b.y;
}
bool ok()
{
return x<&&x>=&&y<&&y>=&&vis[x][y]==;
}
} fr,en,que[];
int bfs()
{
if(fr==en)return ;
int coun=,top=;
memset(que,,sizeof(que));
memset(vis,,sizeof(vis));
que[++coun]=fr;
while(top<=coun)
{
node d=que[top];
top++;
node p;
for(int i=; i<; i++)
{
p.x=d.x+fx[i];
p.y=d.y+fy[i];
p.step=d.step+;
if(p.ok())
{
if(p==en)return p.step;
vis[d.x][d.y]=;
que[++coun]=p;
}
}
}
}
int main()
{
int fry,eny;
char frx,enx;
while(~scanf("%c%d %c%d",&frx,&fry,&enx,&eny))
{
fr.x=frx-'a';
fr.y=fry-;
en.x=enx-'a';
en.y=eny-;
printf("To get from %c%d to %c%d takes %d knight moves.\n",frx,fry,enx,eny,bfs());
getchar();
}
return ;
}

  

【POJ 2243】Knight Moves的更多相关文章

  1. 1450:【例 3】Knight Moves

    1450:[例 3]Knight Moves  题解 这道题可以用双向宽度搜索优化(总介绍在  BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...

  2. 【广搜】Knight Moves

    题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...

  3. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

  4. 【POJ 2195】 Going Home(KM算法求最小权匹配)

    [POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  5. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  6. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  7. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  8. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  9. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

随机推荐

  1. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  2. css3爆炸效果更换图片轮播图

    思路:给一个div设置一个背景图片1.jpg,然后在这个div上面用两个for循环动态的创建一个列数为C行数为R数量的span,并给这些span设置宽高.定位并设置背景图片0.jpg,然后设置每个sp ...

  3. JS的Document属性和方法

    Attributes 存储节点的属性列表(只读)childNodes 存储节点的子节点列表(只读)dataType 返回此节点的数据类型Definition 以DTD或XML模式给出的节点的定义(只读 ...

  4. 【MySQL】游标的具体使用详解

    测试表 level ; )); 再 insert 些数据 ;代码,初始化 drop procedure if exists useCursor // 建立 存储过程 create CREATE PRO ...

  5. C#中成员初始化顺序

    http://blog.csdn.net/huangcailian/article/details/25958967 一.成员初始化整体顺序 1.成员赋值初始化先于构造函数: 2.成员赋值初始先从子类 ...

  6. Java开发环境的搭建

    确定自己的操作系统版本并下载安装JDK 1.下载JDK windows系统: 右键我的电脑->属性;如下图: 2.下载JDK 下载地址:http://www.oracle.com/index.h ...

  7. 机器学习算法 Python & R速查表

  8. C#基础——谈谈.NET异步编程的演变史

    http://www.cnblogs.com/fzrain/p/3545810.html 前言 C#5.0最重要的改进,就是提供了更强大的异步编程.C#5.0仅增加两个新的关键字:async和awai ...

  9. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...

  10. swift项目实战FoodPin目录

    好吧,据说写博客能够找到好工作,那我也来分享一个项目吧! 自己自学iOS开发也有半年多了,现在就来分享一个swift的小项目吧!这个项目的来源是<Beginning iOS8 programmi ...