HDU - 3567 Eight II 【bfs打表+映射+康托展开】

版权声明:如需转载,记得标识出处 https://blog.csdn.net/godleaf/article/details/86990674

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567

这道题比较容易想到的就是双向bfs,在时间上是比较理想的,但是在内存上就比较紧张。

网上看到有人用映射的技巧做这题。

假设我知道起点是 12345678x这个状态到其他状态的所有路径。对于所有起点是 ********x这个状态到其他状态的所有路径我也能够知道。

假设我知道12x345678 这个状态到其他状态的路径。

而样例1要求 12x453786 到 123456789x的路径,我们存在这么一个映射(起点状态的映射)

1 --> 1

2 --> 2

4 --> 3

5 --> 4

3 --> 5

7 --> 6

8 --> 7

6 --> 8

按照上面的映射,我们要求的终点状态不再是 12345678x,而是12534867x

这时候我们要求的问题就变成起点为 12x345678 到终点 12534867x 的最短路径。

根据x的位置,我们只需要预先打表9种情况的路径,通过映射关系直接求出答案。

这种方法在时间上是比较理想的,其次存储答案的时候要注意内存上的限制,如果9种状态的路径统一保证下来的话,内存上是很紧张的。在存储路径的时候,我们不需要存储一整段路径,每一个状态只存一步的路径,并且记录好上一步状态的康托展开编号,当我们需要得到路径的时候,通过不断访问上一步康托展开编号去获取路径。这样每一个状态只需要存一个char和int。

因为这道题在时间上是比较紧张的,要注意不要有一些浪费时间而不必要的操作,比如输出路径的时候一个一个的输出(G++会超时,要存到字符串里面以字符串的形式输出)。

#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;
const int Maxn = 4e5+10;

struct Node {
    int pre, step;
    char c;
} node[11][Maxn];

struct nNode {
    char str[10];
    int pos, st, step;
};

char ch[5] = "dlru", s[10][10] = {"012345678", "102345678", "120345678", "123045678", "123405678",
"123450678", "123456078", "123456708", "123456780"}, ans[50];
int dir[4][2] = { {1, 0}, {0, -1}, {0, 1}, {-1, 0} }, n[15], Hash[10];
bool vis[Maxn];

int cantor(char *ss) {
    int ret = 0;
    for(int i = 0; i < 9; ++i) {
        int cnt = 0;
        for(int j = i+1; j < 9; ++j) {
            if(ss[j] < ss[i]) cnt++;
        }
        ret += n[8-i]*cnt;
    }
    return ret;
}

void bfs(int x) {
    memset(vis, false, sizeof(vis));
    queue<nNode> qu;
    int state = cantor(s[x]);
    vis[state] = true;
    nNode tmp;
    tmp.pos = x; tmp.st = state; tmp.step = 0;
    memcpy(tmp.str, s[x], sizeof(tmp.str));
    qu.push(tmp);

    while (!qu.empty()) {
        tmp = qu.front(); qu.pop();

        for(int i = 0; i < 4; ++i) {
            nNode now = tmp;
            int pi, pj;

            pi = now.pos/3+dir[i][0]; pj = now.pos%3+dir[i][1];
            if(pi < 0 || pi > 2 || pj < 0 || pj > 2) continue;
            swap(now.str[pi*3+pj], now.str[now.pos]);
            now.pos = pi*3+pj;
            state = cantor(now.str);
            if(vis[state]) continue;
            vis[state] = true;
            node[x][state].c = ch[i]; node[x][state].pre = tmp.st;
            node[x][state].step = tmp.step+1;
            now.st = state; now.step = tmp.step+1;
            qu.push(now);
        }
    }
}

void init() {
    for(int i = 0; i < 10; ++i)
        for(int j = 0; j < Maxn; ++j) node[i][j].pre = -1;

    n[0] = 1;
    for(int i = 1; i <= 10; ++i) n[i] = n[i-1]*i;
    for(int i = 0; i < 9; ++i) bfs(i);
}

int main(void)
{
    int T;
    init();
    scanf("%d", &T);
    for(int cas = 1; cas <= T; ++cas) {
        char str1[10], str2[10];
        scanf("%s%s", str1, str2);
        int pos;
        for(int i = 0; i < 9; ++i) {
            if(str1[i] == 'X') {
                str1[i] == '0'; pos = i;
            }
            if(str2[i] == 'X') str2[i] == '0';
        }
        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 state = cantor(str2);
        int cnt = node[pos][state].step;
        int p = cnt;

        printf("Case %d: %d\n", cas, cnt);
        while (node[pos][state].pre != -1) {
            ans[--cnt] = node[pos][state].c;
            state = node[pos][state].pre;
        }
        ans[p] = '\0';
        printf("%s\n", ans);
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/godleaf/article/details/86990674