http://acm.hdu.edu.cn/showproblem.php?pid=1372

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6439    Accepted Submission(s): 3886

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.
 
AC代码:
<span style="font-size:24px;">#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std; struct node
{
int x,y,num;
}; int a1,a2,b1,b2;
char c1,c2;
int move[8][2]={-2,1,-2,-1,-1,2,-1,-2,1,-2,1,2,2,1,2,-1},v[9][9]; void bfs(int i,int j)
{
node now,temp;
queue<node>q;
now.x=i;
now.y=j;
now.num=0;
memset(v,0,sizeof(v));
q.push(now);
v[now.x][now.y]=1;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==b2&&now.y==a2)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",c1,a1,c2,a2,now.num);
return ;
}
for(int t=0;t<8;t++)
{
temp.x=now.x+move[t][0];
temp.y=now.y+move[t][1];
if(temp.x>0&&temp.x<9&&temp.y>0&&temp.y<9&&!v[temp.x][temp.y])
{
v[temp.x][temp.y]=1;
temp.num=now.num+1;
q.push(temp);
}
}
}
} int main()
{
while(cin>>c1>>a1>>c2>>a2)
{
b1=c1-'a'+1;
b2=c2-'a'+1;
bfs(b1,a1);
}
return 0;
}</span>




版权声明:本文博主原创文章,博客,未经同意不得转载。

杭州电 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. Knight Moves(hdu1372 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

  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 题解

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

  5. HDU 1372 Knight Moves (bfs)

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

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

  7. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

  8. HDU 1372 Knight Moves(bfs)

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

  9. HDU 1372 Knight Moves

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

随机推荐

  1. spring-qualifier解释

    如果一个class有两个对应的beanId,在Autowired的时候,必须指定Qualifier("指定其中一个beanId"). org.springframework.bea ...

  2. zookeeper管理solr的配置文件

    zookeeper可以管理solr和其他软件的配置文件.配置文件还是保存在linux服务器的磁盘上,但是不是改变solr读取solr/home配置的配置文件的位置. 现在solr/home配置文件的位 ...

  3. Wcf+EF框架搭建实例

    一.最近在使用Wcf创建数据服务,但是在和EF框架搭建数据访问时遇到了许多问题 下面是目前整理的基本框架代码,经供参考使用,源代码地址:http://git.oschina.net/tiama3798 ...

  4. Starting nagios:This account is currently not available nagios

    nagios在启动时报错 # service nagios restartRunning configuration check…done.Stopping nagios: done.Starting ...

  5. iOS中枚举定义的三种方式

    最简单的方式 typedef enum{ num1 = 0, num2 = 1, num3 = 2 }num; 同时我们还可以使用NS_ENUM的方式定义枚举 typedef NS_ENUM (NSI ...

  6. 东软实训1 -jsp内置对象及其常用方法

    JSP 的内置对象及方法详细说明 一. request 对象 客户端的请求信息被封装在 request 对象中,通过它才能了解到客户的需 求,然后做出响应.它是 HttpServletRequest ...

  7. VS2010 C++ 优化配置

    个人感觉VC6.0太土了,而且有很多bug存在,且微软早就不对其更新.所以,在选择C++编程的时候.使用IDE,VC6.0一段时间以后,我毅然决然的放弃了,觉得还是使用VS2010比较有前途. 但是当 ...

  8. bat判断某个目录是否存在

    使用批处理判断某个目录是否存在的语句. if not exist xxx_folder_name md xxx_folder_name

  9. 文件头 MAGE_FILE_HEADER

    IMAGE_FILE_HEADER这个结构的定义如下: typedef struct _IMAGE_FILE_HEADER { 00h WORD Machine; //运行平台 02h WORD Nu ...

  10. .NET MVC插件化开发(支持Script和css压缩)

    上一篇博文里面,没有支持Script和css的压缩功能以及script和css的路径问题也没有解决,所以重新发布一个版本,解决了这几个问题,并且优化了插件路由注册,现在可以很方便的实现热插拔web插件 ...