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. Android系列----JUnit单元测试的使用

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  2. SqlMapConfig.xml中的setting属性设置

    <settings     cacheModelsEnabled="true"     lazyLoadingEnabled="false"     en ...

  3. title与h1的区别

    title与h1的联系: 从网站角度看,title更重于网站信息.title可以直接告诉搜索引擎和用户这个网站是关于什么主题和内容的. 从文章角度看,h1则是用于概括文章主题.用户进入内容页,想看到的 ...

  4. Nginx+UWSGI+Django配置全过程

    重度参阅 原理+实战http://zhou123.blog.51cto.com/4355617/1688434 原理http://www.cnblogs.com/fnng/p/5268633.html ...

  5. Gruntjs: task之文件映射

    由于大多数的任务执行文件操作,Grunt提供了一个强大的抽象声明说明任务应该操作哪些文件.这里总结了几种src-dest(源文件-目标文件)文件映射的方式,提供了不同程度的描述和控制操作方式. 1. ...

  6. m3u8字段意义解析

    m3u8字段意义解析 HLS,Http Live Streaming是由Apple公司定义的用于实时流传输的协议,HLS基于HTTP协议实现,传输内容包括两部分,一是M3U8描述文件,二是TS媒体文件 ...

  7. matlab 中的textscan

    textread 与textscan的区别  textscan更适合读入大文件: textscan可以从文件的任何位置开始读入,而textread 只能从文件开头开始读入: textscan也可以从上 ...

  8. JNDI全面总结(zz)

    原理:         在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...

  9. Ubuntu 16.04 LTS安装好需要设置的15件事(喜欢新版本)

    看到这篇文章说明你已经从老版本升级到 Ubuntu 16.04 或进行了全新安装,在安装好 Ubuntu 16.04 LTS 之后建议大家先做如下 15 件事.无论你是刚加入 Ubuntu 行列的新用 ...

  10. springmvc学习笔记(一)之简介

    一.简介 SpringMVC 是一个MVC框架,是基于Model-View-Controller模式实现的.类似于Struts2等mvc框架使数据-业务-展现很好的隔离开. 每当用户在web浏览器点击 ...