码迷,mamicode.com
首页 > 其他好文 > 详细

Sicily Knight Moves(BFS)

时间:2016-05-06 21:58:27      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

1000. Knight Moves
 
     
     
     
 
Time Limit: 1sec    Memory Limit:32MB
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

There are multiple test cases. The first line contains an integer T, indicating the number of 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
 Copy sample input to clipboard
8
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.

这道题和前面的那题走迷宫的十分类似,就是这里走“日“字,有8个方向。而且这里没有不可以走的格子。
就是vector实现BFS就可以了。
只是,被最后输出的字符串的一个空格坑了一小下。
 1 #include<iostream>
 2 #include<queue>
 3 #include<vector>
 4 #include<cstring>
 5 using namespace std;
 6 
 7 struct node
 8 {
 9     int x;
10     int y;
11 };
12 
13 //骑士走日 
14 int move[8][2] = {{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
15 int used[9][9];
16 int startx,starty,endx,endy;
17 
18 bool safe(int x,int y)
19 {
20     if(x > 0 && y > 0 && x <= 8 && y <= 8 && used[x][y] == 0)
21     {
22         return true;
23     }
24     return false;
25 }
26 
27 int main()
28 {
29     int T;
30     
31     cin>>T;
32     
33     while(T--)
34     {
35         char s,e;
36         vector <node> v[64];
37                 
38         cin>>s>>startx;
39         starty = s - a + 1;
40         cin>>e>>endx;
41         endy = e - a + 1;
42         
43         node no;
44         no.x = startx;
45         no.y = starty;
46         int k = 0;
47         int flag = 0;
48         
49         v[0].push_back(no);
50         //用memset初始化
51         memset(used,0,sizeof(used));
52         used[startx][starty] = 1;
53         
54         if(startx != endx || starty != endy)
55         {
56                        //vector实现BFS
57             while(1)
58             {
59                 vector <node>::iterator it;
60                 for(it=v[k].begin();it!=v[k].end();it++)
61                 {
62                     node f = *it; 
63                     for(int i = 0; i < 8; i++)
64                     {
65                         node tmp;
66                         tmp.x = f.x + move[i][0];
67                         tmp.y = f.y + move[i][1];
68                         if(tmp.x == endx && tmp.y == endy)
69                         {
70                             flag = 1;
71                             break;
72                         }
73                         if(safe(tmp.x,tmp.y))
74                         {
75                             v[k + 1].push_back(tmp);
76                             used[tmp.x][tmp.y] = 1;
77                         }
78                     }
79                     if(flag == 1)
80                     {
81                         break;
82                     }
83                 }
84                 k++;
85                 if(flag == 1)
86                 {
87                     break;
88                 }    
89             }
90             cout<<"To get from "<<s<<startx<<" to "<<e<<endx<<" takes "<<k<<" knight moves."<<endl;
91             
92         }
93         else
94         {
95             cout<<"To get from "<<s<<startx<<" to "<<e<<endx<<" takes "<<"0"<<" knight moves."<<endl;
96         }
97     }
98     return 0;
99 }

 

 

Sicily Knight Moves(BFS)

标签:

原文地址:http://www.cnblogs.com/huang22/p/5467127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!