B

来源:互联网 发布:ps3模拟器数据损坏 编辑:程序博客网 时间:2024/04/30 07:13
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
212X45378612345678X564178X237568X4123
Sample Output
Case 1: 2ddCase 2: 8urrulldr

预处理+BFS注意要字典序最小代码不是很完美,c++交会TLE,G++就过了,感觉很GG

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <stack>using namespace std;const int maxn = 4e5;const int INF = 0x3f3f3f3f;int vist[10][maxn];int c[9]={1,1,2,6,24,120,720,5040,40320};int dx[4] = {1,0,0,-1};int dy[4] = {0,-1,1,0};char t[10],str1[10],str2[10],mmp[10];char ch[10] = {'d','l','r','u'};stack<int>S;struct node{int val,pos;}u,v;struct Path{int from,dir;}path[10][maxn];int getnum(){int res = 0;for(int i = 0; i < 9; i++)for(int j = i+1; j < 9; j++)if(t[i]>t[j])res += c[8-i];return res;}void getstr(int Val){int temp[10],flag[10];memset(temp,0,sizeof(temp));memset(flag,0,sizeof(flag));for(int i = 0; i < 9; i++){temp[i] = Val/c[8-i];Val = Val%c[8-i];}int sum;for(int i = 0; i < 9; i++){sum = 0;for(int j = 0; j < 9; j++){if(!flag[j]){sum++;if(sum == temp[i]+1){t[i] = j+1+'0';if(t[i] == '9')t[i] = 'X';flag[j] = 1;break;}}}}}void BFS(int py){memset(vist[py],0,sizeof(vist[py]));memset(path[py],-1,sizeof(path[py]));for(int i = 0; i < 9; i++){if(i < py)t[i] = i+1+'0';else if(i == py)t[i] = 'X';elset[i] = i+'0';}u.val = getnum();u.pos = py;vist[py][u.val] = 1;queue<node>Q;Q.push(u);int x,y;while(!Q.empty()){u = Q.front();Q.pop();getstr(u.val);for(int i = 0; i < 4; i++){x = u.pos/3+dx[i];y = u.pos%3+dy[i];if(x>=0 && x<3 && y>=0 && y<3){v.pos = x*3+y;swap(t[v.pos],t[u.pos]);v.val = getnum();swap(t[v.pos],t[u.pos]);if(!vist[py][v.val]){vist[py][v.val] = 1;path[py][v.val].from = u.val;path[py][v.val].dir = i;Q.push(v);}}}}}int main(){/*Init();*/for(int i = 0; i < 9; i++)BFS(i);int T,py,val1,val2,res;scanf("%d",&T);for(int tt = 1; tt <= T; tt++){scanf("%s %s",str1,str2);for(int i = 0; i < 9; i++){if(str1[i] == 'X'){py = i;break;}}for(int i = 0; i < 9; i++){if(i < py){t[i] = i+1+'0';mmp[str1[i]-'0'] = i+1+'0';}else if(i == py)t[i] = 'X';else{t[i] = i+'0';mmp[str1[i]-'0'] = i+'0';}}val1 = getnum();for(int i = 0; i < 9; i++){if(str2[i] == 'X')t[i] = 'X';elset[i] = mmp[str2[i]-'0'];}val2 = getnum();res = 0;while(val2 != val1){res++;S.push(path[py][val2].dir);val2 = path[py][val2].from;}printf("Case %d: %d\n",tt,res);while(!S.empty()){printf("%c",ch[S.top()]);S.pop();}puts("");}return 0;}


原创粉丝点击