Eight2

传送门HDU3567

描述

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.

    输入

    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.

输出

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.

样例

  • Input
    2
    12X453786
    12345678X
    564178X23
    7568X4123

  • Output
    Case 1: 2
    dd
    Case 2: 8
    urrulldr

题解

  • 题意:与上一题不一样的是,此题要求从给定输入串处理到给定输出串
  • 将字符串采用映射的方式进行预处理,然后O(t)查询,t为答案长度。
  • 将输入串的X置0,其他的数从1到8一次排列形成映射,则初始状态就只有10种,预处理这10中状态能到达的状态。
  • 对于样例中的564178X23,我们将其映射为123456078,则7568X4123就相应得被映射为512603478。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<bits/stdc++.h>
#define INIT(a,b) memset(a,b,sizeof(a))
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int N=362885;
const int mod=1e9+7;
string ori[10]={"012345678","102345678","120345678","123045678","123405678",
"123450678","123456078","123456708","123456780"};
int rec[4]={3,-1,1,-3};
string ops="dlru";
int ans[10][N];
int pre[10][N];
int vis[10][N],fac[10];
int h[10];
struct node{
    string sta;
    int x;
};
int cantor(string a){
    int ans=0,c=0;
    for(int i=0;i<9;i++){
        c=0;
        for(int j=i+1;j<9;j++)
            if(a[j]<a[i]) c++;
        ans+=c*fac[9-i-1];
    }
    return ans;
}
bool judge(int x,int to){
    if(to<0||to>=9)return false;
    if((x==2&&to==3)||(x==3&&to==2))return false;
    if((x==5&&to==6)||(x==6&&to==5))return false;
    return true;
}
void bfs(int t){
    queue<node> que;
    int c=cantor(ori[t]);
    vis[t][c]=0;
    que.push((node){ori[t],t});
    while(!que.empty()){
        node q=que.front();que.pop();
        c=cantor(q.sta);
        for(int i=0;i<4;i++){
            int to=q.x+rec[i];
            if(judge(q.x,to)){
                swap(q.sta[q.x],q.sta[to]);
                int c1=cantor(q.sta);
                if(vis[t][c1]==-1){
                    vis[t][c1]=vis[t][c]+1;
                    que.push((node){q.sta,to});
                    ans[t][c1]=i;
                    pre[t][c1]=c;
                }
                swap(q.sta[q.x],q.sta[to]);
            }
        }
    }
}
void print(int n,int c){
    if(pre[n][c]==-1) return ;
    print(n,pre[n][c]);
    printf("%c",ops[ans[n][c]]);
}
int main(){
	//ios::sync_with_stdio(false);//不要关闭流同步,会wa
	//cin.tie(0);
	INIT(vis,-1);INIT(pre,-1);
    fac[0]=1;
    for(int i=1;i<10;i++)
        fac[i]=fac[i-1]*i;
    for(int i=0;i<9;i++) bfs(i);
    int t,n;
    cin>>t;
    string s1,s2;
    for(int _=1;_<=t;_++){
        cout<<"Case "<<_<<": ";
        cin>>s1>>s2;
        int m=1;
        for(int i=0;i<9;i++){
            if(s1[i]=='X'){
                n=i;
                h[0]=0;
            }
            else h[s1[i]-'0']=m++;
        }
        string ss="123456789";
        for(int i=0;i<9;i++){
            if(s2[i]=='X') s2[i]='0';
            ss[i]=h[s2[i]-'0']+'0';
        }
        int c=cantor(ss);
        cout<<vis[n][c]<<endl;
        print(n,c);
        if(vis[n][c]!=-1)cout<<endl;
    }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值