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): 6731    Accepted Submission(s): 4059

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.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int mv[][] = {{-,-},{-,-},{-,},{-,},{,-},{,-},{,},{,}};
int v[][],map[][];
char a[],b[];
struct node
{
int x,y,ans;
}q[];
void bfs(int x,int y)
{
int e=;
int s=;
memset(v,,sizeof(v));
struct node t,f;
t.x=x;
t.y=y;
t.ans=;
v[t.x][t.y]=;
q[e++]=t;
while(s<e)
{
t=q[s++];
if(map[t.x][t.y]==)
{
printf("To get from %s to %s takes %d knight moves.\n",a,b,t.ans);
}
for(int i=;i<;i++)
{
f.x=t.x+mv[i][];
f.y=t.y+mv[i][];
f.ans=t.ans+;
if(f.x>=&&f.x<&&f.y>=&&f.y<&&v[f.x][f.y]==)
{
q[e++]=f;
v[f.x][f.y]=;
}
}
} }
int main()
{
while(scanf("%s%s",a,b)!=EOF)
{
memset(map,,sizeof(map));
map[b[]-''-][b[]-'a']=;//因为a-'0'从0开始,所以a[1]-'0'-1;
bfs(a[]-''-,a[]-'a'); }
return ;
}

最新文章

  1. ActionErrors和ActionError
  2. mybatis配置oracle的主键自增长
  3. JavaScript代码优化(下载时间和执行速度优化)
  4. javascript的setTimeout()用法总结,js的setTimeout()方法
  5. ViewPager循环广告位的实现
  6. 看IT牛人博客的哲理
  7. Spark Streaming连接TCP Socket
  8. C#设置richtextbox某一段文本颜色
  9. 图像插值:OpenCV_remap
  10. MyEclipse的Debug模式启动缓慢
  11. Android初级教程初谈自定义view自定义属性
  12. 末学者daylight__Linux磁盘管理及LVM
  13. JavaScriptDom和应用
  14. MySQL8.0 关闭二进制日志
  15. TFA(Trace File Analyzer)的安装与使用(ORACLE版本12C)
  16. 百度地图API示例:鼠标绘制点线面 控件修改
  17. 【MyBatis】MyBatis之别名typeAliases标签的使用
  18. spring下redis使用资料
  19. TOMCAT配置数据库连接池
  20. 修改gcc/g++默认include路径

热门文章

  1. css笔记 - 张鑫旭css课程笔记之 overflow 篇
  2. Linux 安装GCC讲解(在线和无网离线)
  3. Linux系统更改网卡名称
  4. 使用 intellijIDEA + gradle构建的项目如何debug
  5. ACE学习简单记录
  6. Android 浅谈 设计与屏幕适配 【1.6235449734285716】
  7. Rsync数据同步应用指南
  8. java中String与StringBuilder的区别
  9. poj1611 The suspects【并查集】
  10. 2.4scope