(已解决)hdu 1043 Eight (逆向bfs+打表+康托压缩)

来源:互联网 发布:双系统怎么重装ubuntu 编辑:程序博客网 时间:2024/05/22 06:04

又是玄学,感觉跟别人写的一模一样,怎么也过不了

不知道哪里有问题,先放上来好了

=============================

原来是忘了换行符。。。gg

#include <stdio.h>#include <string.h>#include <iostream>#include <queue>#include <string>using namespace std;#define maxn 370000int fac[10] = {1, 1, 2, 6, 24, 120, 720, 720 * 7, 720 * 7 * 8, 720 * 7 * 8 * 9};int dir[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};  // r, l, u, dint vis[maxn];string path[maxn];char step[5] = "lrdu";struct node {    int loc;  // x所在位置    int ct;   //康托值    int s[10];};int cantor(int a[]) {    int sum = 0;    for (int i = 0; i < 9; i++) {        int m = 0;        for (int j = i + 1; j < 9; j++) {            if (a[i] > a[j]) m++;        }        sum += m * (fac[9 - 1 - i]);    }    return sum + 1;}void BFS() {    queue<node> Q;    node cur;    for (int i = 0; i < 8; i++) {        cur.s[i] = i + 1;    }    cur.s[8] = 0;    cur.ct = cantor(cur.s);    cur.loc = 8;    vis[cur.ct] = 1;    path[cur.ct] = "";    Q.push(cur);    node next;    while (!Q.empty()) {        node cur = Q.front();        Q.pop();        for (int i = 0; i < 4; i++) {            int x = cur.loc / 3 + dir[i][0];            int y = cur.loc % 3 + dir[i][1];            if (x > 2 || x < 0 || y > 2 || y < 0) continue;            next = cur;            next.loc = 3 * x + y;            next.s[cur.loc] = next.s[next.loc];            next.s[next.loc] = 0;            next.ct = cantor(next.s);            if (!vis[next.ct]) {                vis[next.ct] = 1;                Q.push(next);                path[next.ct] = step[i] + path[cur.ct];            }        }    }}int main(void) {    char s[2];    int a[10];    int m;    memset(vis, 0, sizeof(vis));    BFS();    while (scanf("%s", s) != EOF) {        if (s[0] == 'x')            a[0] = 0;        else            a[0] = s[0] - '0';        for (int i = 1; i < 9; i++) {            scanf("%s", s);            if (s[0] == 'x')                a[i] = 0;            else                a[i] = s[0] - '0';        }        m = cantor(a);        if (vis[m])            cout << path[m] << '\n';        else            cout << "unsolvable";    }    return 0;}


原创粉丝点击