Knight Moves

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 Specification

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 Specification

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.
  1 //Problem Name: Knight Moves
2 //Source: ZOJ 1091
3 //Author: jinjin18
4 //Main idea:BFS
5 //Language: C++
6 //Point: init;deal with level in BFS--tail;input--gets rather than scanf;
7 //-upline input--sscanf;BFS model;
8 //======================================================================
9
10 #include<stdio.h>
11 #define MAXSIZE 100000
12 #define INF 10000000
13
14 typedef struct node{
15 char x;
16 int y;
17 }Point;
18 int visited[9][9];
19 void init(){ //初始化,需要二重循环,可用memset函数
20 for(int i = 0; i < 9 ;i++){
21 for(int j = 0; j <9; j++){
22 visited[i][j] = 0;
23 }
24 }
25 }
26 int BFS(char ax,int ay,char bx,int by){
27 int res = 0;
28 int tail = 1;
29 visited[ax-'a'+1][ay] = 1;
30 Point Q[MAXSIZE] = {0};
31 int pre = 0;
32 int last = 1;
33 Q[0].x = ax;
34 Q[0].y = ay;
35 while(pre < last){
36 //printf("OK");
37 char thisx = Q[pre].x;
38 int thisy = Q[pre].y;
39 pre++;
40 //printf("%d %c %d\n",res,thisx,thisy);
41 if(thisx == bx&&thisy == by){ //结束循环
42 return res;
43 }
44 if(thisx + 2 <= 'h'&&thisy + 1 <= 8&&visited[thisx+2-'a'+1][thisy+1]==0){
45 visited[thisx+2-'a'+1][thisy+1]=1;
46 Q[last].x = thisx+2;
47 Q[last].y = thisy+1;
48 last++;
49 }
50 if(thisx + 2 <= 'h'&&thisy - 1 >= 1&&visited[thisx+2-'a'+1][thisy-1]==0){
51 visited[thisx+2-'a'+1][thisy-1]=1;
52 Q[last].x = thisx+2;
53 Q[last].y = thisy-1;
54 last++;
55 }
56 if(thisx + 1 <= 'h'&&thisy + 2 <= 8&&visited[thisx+1-'a'+1][thisy+2]==0){
57 visited[thisx+1-'a'+1][thisy+2]=1;
58 Q[last].x = thisx+1;
59 Q[last].y = thisy+2;
60 last++;
61 }
62 if(thisx + 1 <= 'h'&&thisy - 2 >= 1&&visited[thisx+1-'a'+1][thisy-2]==0){
63 visited[thisx+1-'a'+1][thisy-2]=1;
64 Q[last].x = thisx+1;
65 Q[last].y = thisy-2;
66 last++;
67 }
68 if(thisx - 2 >= 'a'&&thisy - 1 >= 1&&visited[thisx-2-'a'+1][thisy-1]==0){
69 visited[thisx-2-'a'+1][thisy-1]=1;
70 Q[last].x = thisx-2;
71 Q[last].y = thisy-1;
72 last++;
73 }
74 if(thisx - 2 >= 'a'&&thisy + 1 <= 8&&visited[thisx-2-'a'+1][thisy+1]==0){
75 visited[thisx-2-'a'+1][thisy+1]=1;
76 Q[last].x = thisx-2;
77 Q[last].y = thisy+1;
78 last++;
79 }
80
81 if(thisx - 1 >= 'a'&&thisy - 2 >=1&&visited[thisx-1-'a'+1][thisy-2]==0){
82 visited[thisx-1-'a'+1][thisy-2]=1;
83 Q[last].x = thisx-1;
84 Q[last].y = thisy-2;
85 last++;
86 }
87 if(thisx - 1 >= 'a'&&thisy + 2 <= 8&&visited[thisx-1-'a'+1][thisy+2]==0){
88 visited[thisx-1-'a'+1][thisy+2]=1;
89 Q[last].x = thisx-1;
90 Q[last].y = thisy+2;
91 last++;
92 }
93 if(tail == pre){ //更新tail
94 tail = last;
95 res++;
96 }
97
98 }
99 return INF;
100
101 }
102
103 int main(){
104 int ay,by;
105 char ax,bx;
106 char S[10];
107 while(gets(S)){ //%s与%c不能用,思考为何?
108 sscanf(S,"%c%d %c%d",&ax,&ay,&bx,&by);
109 init();
110 int res = BFS(ax,ay,bx,by);
111 printf("To get from %c%d to %c%d takes %d knight moves.\n",ax,ay,bx,by,res);
112 }
113 return 0;
114
115 }

看到一篇写的比较好的博文:https://blog.csdn.net/z8110/article/details/49492479

最新文章

  1. UVa1593_Allgnment_Of_Code
  2. Jfianl
  3. window 内核详尽分析
  4. JAVA 数组作业——动手动脑以及课后实验性问题
  5. bootstrap插件之Carousel
  6. 关于Masonry框架(AutoLayout)的用法--面向初学者
  7. maven项目显示红叉的解决方法
  8. python import 自己的包
  9. 世界gis相关的资源网站分类整理
  10. win8.1 usb3 速度慢的解决方法
  11. wikioi 1002 旁路
  12. 是否使用安全模式启动word
  13. iOS-UINavigationBar【颜色设置】
  14. 一行命令更新所有 npm 依赖包
  15. 从GitHub下载demo时遇到的依赖问题
  16. poj2528 Mayor&#39;s posters (线段树+离散化)
  17. [python] 查询mysql返回datetime类型数据的处理
  18. 雷林鹏分享:XML to HTML
  19. Linux查看文件夹占用空间
  20. 设置模式之单例模式(附上一个Objective-C编写的播放音乐的单例类)

热门文章

  1. linux 漏洞列表
  2. thinkphp5.1 阿里云短信接口
  3. 多测师讲解pthon _函数__return_高级讲师肖sir
  4. matplotlib画图教程,设置坐标轴标签和间距
  5. MeteoInfoLab脚本示例:水汽通量散度计算
  6. day02 Pyhton学习
  7. Spring Boot 系列:日志动态配置详解
  8. 【学习笔记】陀螺 Treap
  9. lumen-ioc容器测试 (4)
  10. centos8安装lvs