HDU 1043 – Eight [最经典的BFS]

Link

传送门

Problem

The 15-puzzle has been around for over 100 years; even if you don’t know it by that name, you’ve seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let’s call the missing tile ‘x’; the object of the puzzle is to arrange the tiles so that they are ordered as:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15  x

where the only legal operation is to exchange ‘x’ with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

 1  2  3  4     1  2  3  4     1  2  3  4     1  2  3  4
 5  6  7  8     5  6  7  8     5  6  7  8     5  6  7  8
 9  x 10 12     9 10  x 12     9 10 11 12     9 10 11 12
13 14 11 15    13 14 11 15    13 14  x 15    13 14 15  x
 r->            d->            r->

The letters in the previous row indicate which neighbor of the ‘x’ tile is swapped with the ‘x’ tile at each step; legal values are ‘r’,’l’,’u’ and ‘d’, for right, left, up, and down, respectively.

Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and
frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing ‘x’ tile, of course).

In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three
arrangement.

You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are represented by numbers 1 to 8, plus ‘x’. For example, this puzzle

1 2 3
x 4 6
7 5 8

is described by this list:

1 2 3 x 4 6 7 5 8

Mean

经典的八数码问题,输出给定的状态到标准初始状态的路径。

Analysis

据说不做这道题,人生是不完美的……

为了节省时间,将标准状态作为起点bfs到所有状态一遍,将状态用康拓定理压缩成一个数,记录到本状态的路径和上一个状态,这样输入后直接查询即可。

Code

#include <bits/stdc++.h>
using namespace std;

struct Node {
    int code;
    int a[9];
};
const int maxn = 370000;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const char chg[4] = {'d', 'u', 'l', 'r'};

Node st;
int vis[maxn], fact[10];
int precode[maxn];
char oper[maxn];

void initLookTable() {
    fact[0] = 1;
    for(int i = 1; i < 9; ++i)
        fact[i] = fact[i-1] * i;
}

bool tryInsert(Node &u, bool ins) {
    int code = 0;
    for(int i = 0; i < 9; ++i) {
        int cnt = 0;
        for(int j = i + 1; j < 9; ++j)
            if(u.a[j] < u.a[i]) ++cnt;
        code += fact[8-i] * cnt;
    }
    u.code = code;
    if(!ins) return true;
    if(vis) return false;
    return vis = true;
}

void bfs() {
    for(int i = 0; i < 9; ++i) {
        if(i == 8) st.a[i] = 0;
        else st.a[i] = i + 1;
    }
    initLookTable();
    tryInsert(st, true);

    queue<Node> qu;
    qu.push(st);
    while(!qu.empty()) {
        Node u = qu.front(); qu.pop();
        int z;
        for(z = 0; z < 9; ++z)
            if(!u.a[z]) break;
        int x = z / 3, y = z % 3;
        for(int i = 0; i < 4; ++i) {
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            int zz = xx * 3 + yy;
            if(xx >= 0 && xx < 3 && yy >= 0 && yy < 3) {
                Node v = u;
                v.a[zz] = u.a[z];
                v.a[z] = u.a[zz];
                if(tryInsert(v, true)) {
                    precode[v.code] = u.code;
                    oper[v.code] = chg[i];
                    qu.push(v);
                }
            }
        }
    }
}

int main() {
    bfs();
    char buf[3]; Node cur;
    while(~scanf("%s", buf)) {
        for(int i = 0; i < 9; ++i) {
            if(i > 0) scanf("%s", buf);
            if(isalpha(buf[0])) buf[0] = '0';
            cur.a[i] = buf[0] - '0';
        }
        tryInsert(cur, false);
        if(vis[cur.code]) {
            int pos = cur.code;
            while(pos != st.code) {
                putchar(oper[pos]);
                pos = precode[pos];
            }
            puts("");
        } else {
            puts("unsolvable");
        }
    }
    return 0;
}

欢迎留言