hdu 1372 Knight Moves

题目连接

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

Knight Moves

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.

朴素的bfs。

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::pair;
15 using std::queue;
16 using std::vector;
17 using std::multimap;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int N = 8;
28 typedef unsigned long long ull;
29 bool vis[N][N];
30 const int dx[8] = { 1, 2, 1, 2, -1, -2, -1, -2 }, dy[8] = { -2, -1, 2, 1, 2, 1, -2, -1 };
31 int Sx, Sy, Dx, Dy;
32 struct Node {
33     int x, y, s;
34     Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
35 };
36 int bfs() {
37     cls(vis, false);
38     queue<Node> que;
39     que.push(Node(Sx, Sy, 0));
40     vis[Sx][Sy] = true;
41     while (!que.empty()) {
42         Node t = que.front(); que.pop();
43         if (t.x == Dx && t.y == Dy) return t.s;
44         rep(i, 8) {
45             int nx = t.x + dx[i], ny = t.y + dy[i];
46             if (nx < 0 || nx >= N || ny < 0 || ny >= N || vis[nx][ny]) continue;
47             que.push(Node(nx, ny, t.s + 1));
48             vis[nx][ny] = true;
49         }
50     }
51     return 0;
52 }
53 int main() {
54 #ifdef LOCAL
55     freopen("in.txt", "r", stdin);
56     freopen("out.txt", "w+", stdout);
57 #endif
58     char s1[N], s2[N];
59     while (~scanf("%s %s", s1, s2)) {
60         Sx = s1[0] - 'a';
61         Sy = s1[1] - '0' - 1;
62         Dx = s2[0] - 'a';
63         Dy = s2[1] - '0' - 1;
64         printf("To get from %s to %s takes %d knight moves.
", s1, s2, bfs());
65     }
66     return 0;
67 }
View Code

双向bfs,写戳了%>_<%

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using std::cin;
10 using std::cout;
11 using std::endl;
12 using std::find;
13 using std::sort;
14 using std::pair;
15 using std::queue;
16 using std::vector;
17 using std::multimap;
18 #define pb(e) push_back(e)
19 #define sz(c) (int)(c).size()
20 #define mp(a, b) make_pair(a, b)
21 #define all(c) (c).begin(), (c).end()
22 #define iter(c) decltype((c).begin())
23 #define cls(arr,val) memset(arr,val,sizeof(arr))
24 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
25 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
26 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
27 const int N = 8;
28 typedef unsigned long long ull;
29 bool vis1[N][N], vis2[N][N];
30 const int dx[8] = { 1, 2, 1, 2, -1, -2, -1, -2 }, dy[8] = { -2, -1, 2, 1, 2, 1, -2, -1 };
31 int Sx, Sy, Dx, Dy;
32 struct Node {
33     int x, y, s;
34     Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {}
35 };
36 int dbfs() {
37     if (Sx == Dx && Sy == Dy) return 0;
38     cls(vis1, false), cls(vis2, false);
39     queue<Node> p, q;
40     p.push(Node(Sx, Sy, 0)), q.push(Node(Dx, Dy, 0));
41     vis1[Sx][Sy] = true, vis2[Dx][Dy] = true;
42     int ans1 = 0, ans2 = 0;
43     while (!p.empty() && !q.empty()) {
44         int tq = sz(p);
45         while (tq--) {
46             Node t = p.front(); p.pop();
47             rep(i, 8) {
48                 int nx = t.x + dx[i], ny = t.y + dy[i];
49                 if (nx < 0 || nx >= N || ny < 0 || ny >= N || vis1[nx][ny]) continue;
50                 ans1 = t.s;
51                 if (vis2[t.x][t.y]) return ans1 + ans2 + 1;
52                 p.push(Node(nx, ny, ans1 + 1));
53                 vis1[nx][ny] = true;
54             }
55         }
56         int tp = sz(q);
57         while (tp--) {
58             Node t = q.front(); q.pop();
59             rep(i, 8) {
60                 int nx = t.x + dx[i], ny = t.y + dy[i];
61                 if (nx < 0 || nx >= N || ny < 0 || ny >= N || vis2[nx][ny]) continue;
62                 ans2 = t.s;
63                 if (vis1[t.x][t.y]) return ans1 + ans2 + 1;
64                 q.push(Node(nx, ny, ans2 + 1));
65                 vis2[nx][ny] = true;
66             }
67         }
68     }
69     return 0;
70 }
71 int main() {
72 #ifdef LOCAL
73     freopen("in.txt", "r", stdin);
74     freopen("out.txt", "w+", stdout);
75 #endif
76     char s1[N], s2[N];
77     while (~scanf("%s %s", s1, s2)) {
78         Sx = s1[0] - 'a';
79         Sy = s1[1] - '0' - 1;
80         Dx = s2[0] - 'a';
81         Dy = s2[1] - '0' - 1;
82         printf("To get from %s to %s takes %d knight moves.
", s1, s2, dbfs());
83     }
84     return 0;
85 }
View Code
By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
原文地址:https://www.cnblogs.com/GadyPu/p/4611947.html