HDU - 3567~~Eight II

題目描述:

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

方法一: 雙向bfs,枚舉最短路更新最小字典序

#include<stdio.h>
#include<iostream>
#include<string>
#include<queue>
#include<math.h>
#include<string.h>

using namespace std;

typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 362890;
const int inf = 0x3f3f3f3f;
const double Pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const char g[] = "dlru";
const int dir[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
const int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};
const LL pow4[]={1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864,268435456,1073741824,4294967296,17179869184,68719476736,274877906944,1099511627776,4398046511104,17592186044416,70368744177664,281474976710656,1125899906842624,4503599627370496,18014398509481984,72057594037927936,288230376151711744,1152921504606846976,4611686018427387904};

template<class T, class F> inline void mem(T a,F b, int c) {for(int i=0;i<=c;++i)a[i]=b;}
template<class T> inline void read(T &x,T xk=10) { // xk 爲進制
    char ch = getchar(); T f = 1, t = 0.1;
    for(x=0; ch>'9'||ch<'0'; ch=getchar()) if(ch=='-')f=-1;
    for(;ch<='9'&&ch>='0';ch=getchar())x=x*xk+ch-'0';if(ch=='.') 
    for(ch=getchar();ch<='9'&&ch>='0';ch=getchar(),t*=0.1)x+=t*(ch-'0');x*=f;
}

struct node{
    char str[9];
    int Hash;
    int flag;
    int step;
    int x, y;
    LL  path;
    void cantor() {
        Hash = 0;
        for(int i = 0; i < 9; ++ i) {
            for(int j = i + 1; j < 9; ++ j) {
                if (str[i] > str[j]) Hash += fac[8-i];
            }
        }
    }      
};

LL path[2][maxn];
int visit[2][maxn];

string get_path(int flat, int Hash) { 
    string res;
    LL m = path[flat][Hash];
    for(int i = visit[flat][Hash]; i >= 1; -- i) {
        res += g[m / pow4[i-1]];
        m %= pow4[i-1];
    }
    return res;
} 

void bfs(node n1, node n2){
    memset(visit, -1, sizeof(visit));
    queue<node> q;
    q.push(n1); visit[0][n1.Hash] = 0;
    q.push(n2); visit[1][n2.Hash] = 0;
    string cnt;
    if (n1.Hash == n2.Hash) {
        cout << 0 << endl;
        cout << endl;
        return ;
    }
    while(!q.empty()) {
        node temp = q.front();
        q.pop();
        for(int i = 0; i < 4; ++ i) {
            node c = temp;
            c.x = temp.x + dir[i][0];
            c.y = temp.y + dir[i][1];
            if (c.x >= 0 && c.x < 3 && c.y >= 0 && c.y < 3) {
                ++ c.step; 
                if (c.flag) c.path = c.path + (3-i) * pow4[temp.step];
                else c.path = c.path * 4 + i;
                swap(c.str[c.x*3+c.y], c.str[temp.x*3+temp.y]);
                c.cantor();
                if (visit[c.flag][c.Hash] == -1) { // 新點入隊
                    visit[c.flag][c.Hash] = c.step;
                    path[c.flag][c.Hash] = c.path;
                    q.push(c);
                }
                else { // 更新最短路徑
                    if (visit[c.flag][c.Hash] < c.step) continue;
                    if (path[c.flag][c.Hash] < c.path) continue;
                    path[c.flag][c.Hash] = c.path;
                    q.push(c);
                }
                if (visit[c.flag^1][c.Hash] != -1) { // 在同意最短路下找到最小字典序 
                    string res = get_path(0, c.Hash) + get_path(1, c.Hash);
                    if (cnt.empty()) cnt = res;
                    if (cnt.size() < res.size()) {
                        cout << cnt.size() << endl;
                        cout << cnt << endl;
                        return ;
                    }
                    cnt = min(cnt, res);
                }    
            }
        }
    }
}

int main() {
    int T; read(T);
    for(int k = 1; k <= T; ++ k) {
        node st, en;
        scanf("%s%s", st.str, en.str);
        for(int i = 0; i < 9; ++ i) {
            if (st.str[i] == 'X') {
                st.str[i] = '0';
                st.x = i / 3;
                st.y = i % 3; 
                st.flag = 0;
                st.step = 0;
                st.path = 0;
                st.cantor();
            }
            if (en.str[i] == 'X') {
                en.str[i] = '0';
                en.x = i / 3;
                en.y = i % 3;
                en.flag = 1;
                en.step = 0;
                en.path = 0;
                en.cantor();
            }
        }
        cout << "Case " << k << ": ";
        bfs(st, en);
    }
    return 0;
}

方法二: 單向bfs打表,把初始狀態hash成有序狀態,單向打表

#include<stdio.h>
#include<iostream>
#include<string>
#include<queue>
#include<math.h>
#include<string.h>

using namespace std;

typedef long long LL;
const int mod = 1e9 + 7;
const int maxn = 362890;
const int inf = 0x3f3f3f3f;
const double Pi = acos(-1.0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const char g[] = "dlru";
const int dir[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
const int fac[10]={1,1,2,6,24,120,720,5040,40320,362880};
const LL pow4[]={1,4,16,64,256,1024,4096,16384,65536,262144,1048576,4194304,16777216,67108864,268435456,1073741824,4294967296,17179869184,68719476736,274877906944,1099511627776,4398046511104,17592186044416,70368744177664,281474976710656,1125899906842624,4503599627370496,18014398509481984,72057594037927936,288230376151711744,1152921504606846976,4611686018427387904};
const char Map[9][10] = {"012345678", "102345678", "120345678", "123045678", "123405678", "123450678", "123456078", "123456708", "123456780"};

template<class T, class F> inline void mem(T a,F b, int c) {for(int i=0;i<=c;++i)a[i]=b;}
template<class T> inline void read(T &x,T xk=10) { // xk 爲進制
    char ch = getchar(); T f = 1, t = 0.1;
    for(x=0; ch>'9'||ch<'0'; ch=getchar()) if(ch=='-')f=-1;
    for(;ch<='9'&&ch>='0';ch=getchar())x=x*xk+ch-'0';if(ch=='.') 
    for(ch=getchar();ch<='9'&&ch>='0';ch=getchar(),t*=0.1)x+=t*(ch-'0');x*=f;
}

struct node {
    char str[9];
    int x, y;
    int Hash;
    void cantor() {
        Hash = 0;
        for(int i = 0; i < 9; ++ i) {
            for(int j = i + 1; j < 9; ++ j) {
                if (str[i] > str[j]) Hash += fac[8-i];
            }
        }
    }
};

int visit[9][maxn];
char path[9][maxn];

void bfs(int x, node a) {
    queue<node> q;
    q.push(a);
    visit[x][a.Hash] = -2;
    while(!q.empty()) {
        node temp = q.front();
        q.pop();
        for(int i = 0; i < 4; ++ i) {
            node c = temp;
            c.x = temp.x + dir[i][0];
            c.y = temp.y + dir[i][1];
            if (c.x >= 0 && c.x < 3 && c.y >= 0 && c.y < 3) {
                swap(c.str[c.x*3+c.y], c.str[temp.x*3+temp.y]);
                c.cantor();
                if (visit[x][c.Hash] != -1) continue;
                visit[x][c.Hash] = temp.Hash;
                path[x][c.Hash] = g[i];
                q.push(c);
            }
        }
    }
}

int main() {
    memset(visit, -1, sizeof(visit));
    int A[9];
    for(int i = 0; i < 9; ++ i) {
        node a;
        for(int j = 0; j < 9; ++ j) a.str[j] = Map[i][j];
        a.x = i / 3;
        a.y = i % 3;
        a.cantor();
        A[i] = a.Hash;
        bfs(i, a);
    }
    int T; read(T);
    for(int K = 1; K <= T; ++ K) {
        node st, en;
        scanf("%s%s", st.str, en.str);
        int tot = 0, b[9], k;
        for(int i = 0; i < 9; ++ i) {
            if (st.str[i] == 'X') k = i;
            else b[st.str[i] - '0'] = ++ tot;
        }
        for(int i = 0; i < 9; ++ i) {
            if (en.str[i] == 'X') en.str[i] = '0';
            else en.str[i] = b[en.str[i] - '0'] + '0';
            if (i == 8) en.cantor();
        }
        char res[100];
        int ans = en.Hash, cnt = 0;
        while(visit[k][ans] != -2) {
            res[++cnt] = path[k][ans];
            ans = visit[k][ans];
        }
        printf("Case %d: %d\n", K, cnt);
        for(int i = cnt; i >= 1; -- i) {
            printf("%c",res[i]);
        }
        printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章