Eight II
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 130000/65536 K (Java/Others)Total Submission(s): 4541 Accepted Submission(s): 990
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.
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.
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.
Sample Input
2
12X453786
12345678X
564178X23
7568X4123
Sample Output
Case 1: 2
dd
Case 2: 8
urrulldr
题意:强化版本的八数码问题。(数据量T很大..而且要求输出最小字母序的操作方式)
题目分析:对于普通的八数码问题可以使用双向bfs解决,这个强化版本由于数据量过大,使用双向bfs可能也会TLE,而且使用双向bfs时反向bfs的过程不容易按最小字母序输出。
题解:由于起始状态与结束状态只是反映了一堆字母以及空格的相对位置关系,和字母本身的含义无关,所以可以预处理跑出空格X在不同位置下到达所有状态所用的最少操作方 式,然后按照每组数据给出的状态进行映射输出即可。这个过程仍然使用康拓展开进行hash。【注意:对string进行的操作真的很慢,所以康拓展开时使用int存状态,不用string】
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 string str1, str2; 9 bool vis[370000]; 10 int dx[4] = { 1,0,0,-1 }; 11 int dy[4] = { 0,-1,1,0 }; 12 char cs[4] = { 'd','l','r','u' }; 13 int pre[10][370000]; 14 int op[10][370000]; 15 int jc[12]; 16 int kt(int s) //康托展开 17 { 18 int code = 0; 19 int st[9]; 20 for (int i = 8; i >= 0; i--, s /= 10) 21 st[i] = s % 10; 22 for (int i = 0; i<9; i++) 23 { 24 int cnt = 0; 25 for (int j = i + 1; j<9; j++) 26 if (st[j]