热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ZOJ1091KnightMoves(BFS)

KnightMoves

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 i++){
21 for(int j = 0; j 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 }

看到一篇写的比较好的博文:


   



推荐阅读
author-avatar
拍友2602939213
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有