题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567
Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 3420 Accepted Submission(s): 742
Problem Description
Eight-puzzle, which is also called "Nine grids", comes from an old game.
In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.
We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.
A state of the board can be represented by a string S using the rule showed below.
The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.
We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.
A state of the board can be represented by a string S using the rule showed below.
The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.
Input
The first line is T (T <= 200), which means the number of test cases of this problem.
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.
Output
For each test case two lines are expected.
The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.
Sample Input
2 12X453786 12345678X 564178X23 7568X4123
Sample Output
Case 1: 2 dd Case 2: 8 urrulldr
Author
zhymaoiing
Source
题解:
POJ1077 的强化版。
问:为什么加了vis判重比不加vis判重还要慢?
答:因为当引入vis判重时,就需要知道棋盘的状态,而计算一次棋盘的状态,就需要增加(8+7+……1)次操作,结果得不偿失。
更新:其实IDA*算法不能加vis判重,因为IDA*的本质就是dfs, 根据dfs的特性, 第一次被访问所用的步数并不一定是最少步数,所以如果加了vis判重,就默认取了第一次被访问时所用的步数,而这个步数不一定是最优的。所以第二份代码是错误的,即使过了oj的数据。
未加vis判重(202MS):
2017-09-10 10:25:57 | Accepted | 3567 | 202MS | 1712K |
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <vector>
7 #include <queue>
8 #include <stack>
9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e9+7;
18 const int MAXN = 1e6+10;
19
20 //M为棋盘, pos_goal为目标状态的每个数字所在的位置, pos_goal[dig] = pos,
21 //即表明:在目标状态中,dig所在的位置为pos。pos_goal与M为两个互逆的数组。
22 int M[MAXN], pos_goal[MAXN];
23
24 int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
25 int dir[4][2] = { 1,0, 0,-1, 0,1, -1,0 };
26 char op[4] = {'d', 'l', 'r', 'u' };
27
28 int cantor(int s[]) //获得哈希函数值
29 {
30 int sum = 0;
31 for(int i = 0; i<9; i++)
32 {
33 int num = 0;
34 for(int j = i+1; j<9; j++)
35 if(s[j]<s[i]) num++;
36 sum += num*fac[8-i];
37 }
38 return sum+1;
39 }
40
41 int dis_h(int s[]) //获得曼哈顿距离
42 {
43 int dis = 0;
44 for(int i = 0; i<9; i++)
45 if(s[i]!=9)
46 {
47 int x = i/3, y = i%3;
48 int xx = pos_goal[s[i]]/3, yy = pos_goal[s[i]]%3; //此处须注意
49 dis += abs(x-xx) + abs(y-yy);
50 }
51 return dis;
52 }
53
54 char path[100];
55 int kase, nextd;
56 bool IDAstar(int loc, int depth, int pre, int limit)
57 {
58 int h = dis_h(M);
59 if(depth+h>limit)
60 {
61 nextd = min(nextd, depth+h);
62 return false;
63 }
64
65 if(h==0)
66 {
67 path[depth] = '\0';
68 printf("Case %d: %d\n", kase, depth);
69 puts(path);
70 return true;
71 }
72
73 int x = loc/3;
74 int y = loc%3;
75 for(int i = 0; i<4; i++)
76 {
77 if(i+pre==3) continue; //方向与上一步相反, 剪枝
78 int xx = x + dir[i][0];
79 int yy = y + dir[i][1];
80 if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
81 {
82 int tmploc = xx*3+yy;
83 swap(M[loc], M[tmploc]);
84 path[depth] = op[i];
85 if(IDAstar(xx*3+yy, depth+1, i, limit))
86 return true;
87 swap(M[loc], M[xx*3+yy]);
88 }
89 }
90 return false;
91 }
92
93 int main()
94 {
95 int T;
96 char str[50];
97 scanf("%d",&T);
98 for(kase = 1; kase<=T; kase++)
99 {
100 int loc;
101 scanf("%s", str);
102 for(int i = 0; i<9; i++)
103 {
104 if(str[i]=='X') M[i] = 9, loc = i;
105 else M[i] = str[i]-'0';
106 }
107
108 scanf("%s", str);
109 for(int i = 0; i<9; i++)
110 {
111 if(str[i]=='X') pos_goal[9] = i;
112 else pos_goal[str[i]-'0'] = i;
113 }
114
115 for(int limit = dis_h(M); ; limit = nextd) //迭代加深搜
116 {
117 nextd = INF;
118 if(IDAstar(loc, 0, INF, limit))
119 break;
120 }
121 }
122 }
View Code
加了vis判重(936MS)
2017-09-10 10:26:10 | Accepted | 3567 | 936MS | 5620K |
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <vector>
7 #include <queue>
8 #include <stack>
9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int MOD = 1e9+7;
18 const int MAXN = 1e6+10;
19
20 int M[MAXN], pos_goal[MAXN];
21
22 int fac[9] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320};
23 int dir[4][2] = { 1,0, 0,-1, 0,1, -1,0 };
24 char op[4] = {'d', 'l', 'r', 'u' };
25
26 int cantor(int s[]) //获得哈希函数值
27 {
28 int sum = 0;
29 for(int i = 0; i<9; i++)
30 {
31 int num = 0;
32 for(int j = i+1; j<9; j++)
33 if(s[j]<s[i]) num++;
34 sum += num*fac[8-i];
35 }
36 return sum+1;
37 }
38
39 int dis_h(int s[]) //获得曼哈顿距离
40 {
41 int dis = 0;
42 for(int i = 0; i<9; i++)
43 if(s[i]!=9)
44 {
45 int x = i/3, y = i%3;
46 int xx = pos_goal[s[i]]/3, yy = pos_goal[s[i]]%3;
47 dis += abs(x-xx) + abs(y-yy);
48 }
49 return dis;
50 }
51
52 char path[100];
53 int kase, nextd, vis[MAXN];
54 bool IDAstar(int loc, int depth, int pre, int limit)
55 {
56 int h = dis_h(M);
57 if(depth+h>limit)
58 {
59 nextd = min(nextd, depth+h);
60 return false;
61 }
62
63 if(h==0)
64 {
65 path[depth] = '\0';
66 printf("Case %d: %d\n", kase, depth);
67 puts(path);
68 return true;
69 }
70
71 int x = loc/3;
72 int y = loc%3;
73 for(int i = 0; i<4; i++)
74 {
75 if(i+pre==3) continue; //方向与上一步相反, 剪枝
76 int xx = x + dir[i][0];
77 int yy = y + dir[i][1];
78 if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
79 {
80 int tmploc = xx*3+yy;
81 swap(M[loc], M[tmploc]);
82 int status = cantor(M);
83 if(!vis[status])
84 {
85 vis[status] = 1;
86 path[depth] = op[i];
87 if(IDAstar(xx*3+yy, depth+1, i, limit))
88 return true;
89 vis[status] = 0;
90 }
91 swap(M[loc], M[xx*3+yy]);
92 }
93 }
94 return false;
95 }
96
97 int main()
98 {
99 int T;
100 char str[50];
101 scanf("%d",&T);
102 for(kase = 1; kase<=T; kase++)
103 {
104 int loc;
105 scanf("%s", str);
106 for(int i = 0; i<9; i++)
107 {
108 if(str[i]=='X') M[i] = 9, loc = i;
109 else M[i] = str[i]-'0';
110 }
111
112 scanf("%s", str);
113 for(int i = 0; i<9; i++)
114 {
115 if(str[i]=='X') pos_goal[9] = i;
116 else pos_goal[str[i]-'0'] = i;
117 }
118
119 vis[cantor(M)] = 1;
120 for(int limit = dis_h(M); ; limit = nextd) //迭代加深搜
121 {
122 nextd = INF;
123 ms(vis,0);
124 if(IDAstar(loc, 0, INF, limit))
125 break;
126 }
127 }
128 }
View Code