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:
- It is of minimum length among all possible solutions.
- 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
思路
八数码升级版,这次是最终目标不一致给定起始情况和终点情况,问从起始到终点需要多少步以及步数相同的情况下最小的字典序。还是采用离线打表的方式,不过这次打表需要打出大约37*10万种情况。还需要结合哈希映射,bfs打表 + 康托展开 + hash,两道八数码题折腾了一天,错误提交加起来有3页多吧。这道题不适合新手玩家做,做hash之前把脸和手洗干净不然容易出错,心态摆正就没什么问题。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
int sum[10] = {1,1,2,6,24,120,720,5040,40320,362880};
int dx[] = {0,-1,1,0};
int dy[] = {1,0,0,-1};
char dir[5] = "dlru";
char s[10][10] = {"012345678", "102345678", "120345678", "123045678",
"123405678","123450678", "123456078", "123456708", "123456780"};
struct node{
int pre;
char way;
int step;
}Node[370000][11];
struct info{
int ct;
char state[10];
int num;
};
queue<info>q;
int Cantor(char s[])
{
int result = 0;
for(int i = 0;i < 9;i++){
int cnt = 0;
for(int j = i+1;j < 9;j++){
if(s[i] > s[j]){
cnt++;
}
}
result += cnt * sum[8-i];
}
return result + 1;
}
void bfs(int x)
{
while(!q.empty()){
info ptr = q.front(),p;
q.pop();
for(int i = 0;i < 4;i++){
int nx = ptr.num%3 + dx[i];
int ny = ptr.num/3 + dy[i];
int nz = nx + 3*ny;
if(nx < 0 || nx >= 3 || ny < 0 || ny >= 3){
continue;
}
memcpy(p.state,ptr.state,sizeof(p.state));
swap(p.state[nz],p.state[ptr.num]);
p.num = nz;
p.ct = Cantor(p.state);
if(Node[p.ct][x].pre == -1){
Node[p.ct][x].step = Node[ptr.ct][x].step + 1;
Node[p.ct][x].pre = ptr.ct;
Node[p.ct][x].way = dir[i];
q.push(p);
}
}
}
}
void clear_queue(int j)
{
while(!q.empty()){
q.pop();
}
for(int i = 0;i < 370000;i++){
Node[i][j].pre = -1;
}
}
void slove()
{
for(int i = 0;i < 9;i++){
clear_queue(i);
info p;
memcpy(p.state,s[i],sizeof(p.state));
p.ct = Cantor(s[i]);
p.num = i;
Node[p.ct][i].step = 0;
Node[p.ct][i].pre = 0;
q.push(p);
bfs(i);
}
}
int main()
{
// freopen("C:\\Users\\Administrator\\Desktop\\input.txt","r",stdin);
// freopen("C:\\Users\\Administrator\\Desktop\\output.txt","w",stdout);
slove();
int t;
scanf("%d",&t);
for(int j = 1;j <= t;j++){
char str1[100],str2[100];
int pos;
scanf("%s%s",str1,str2);
for(int i = 0;i < 9;i++){
if(str1[i] == 'X'){
str1[i] = '0';
pos = i;
}
if(str2[i] == 'X'){
str2[i] = '0';
}
}
int hash[10];
for(int i = 0;i < 9;i++){ //哈希处理
hash[str1[i] - '0'] = s[pos][i] - '0';
}
for(int i = 0;i < 9;i++){
str2[i] = hash[str2[i] - '0'] + '0';
}
str2[9] = '\0';
int ctvalue = Cantor(str2);
int k = Node[ctvalue][pos].step;
printf("Case %d: %d\n",j,k);
//printf("%c",Node[ctvalue][pos].way);
int p = k;
char ans[50];
while(ctvalue){
ans[--k] = Node[ctvalue][pos].way;
ctvalue = Node[ctvalue][pos].pre;
}
ans[p] = '\0';
printf("%s\n",ans);
}
return 0;
}
愿你走出半生,归来仍是少年~