[UVA] 10181. 15-Puzzle Problem

題目

15-puzzle

分析

Note

IDA*

N-puzzle 是 NP Hard,所以用搜索演算法 IDA* heuristic function 使用曼哈頓距離。 事實上程式碼不長啊

AC 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
 99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;

const int dr[4] = {0, 0, +1, -1};
const int dc[4] = {+1, -1, 0, 0};
const int dir[4] = {'R', 'L', 'D', 'U'};
const int INF = 0x3f3f3f3f;
const int FOUND = -1;
vector<char> path;
int A[15][15], Er, Ec;

int H() {
    int h = 0;
    for (int r = 0; r < 4; r++) {
        for (int c = 0; c < 4; c++) {
            if (A[r][c] == 0) continue;
            int expect_r = (A[r][c] - 1) / 4;
            int expect_c = (A[r][c] - 1) % 4;
            h += abs(expect_r - r) + abs(expect_c - c);
        }
    }
    return h;
}

int dfs(int g, int pdir, int bound) {
    int h = H();
    int f = g + h;
    if (f > bound) return f;
    if (h == 0) return FOUND;

    int mn = INF;
    for (int i = 0; i < 4; i++) {
        if (i == (pdir ^ 1)) continue;

        int nr = Er + dr[i];
        int nc = Ec + dc[i];
        if (nr < 0 || nr >= 4) continue;
        if (nc < 0 || nc >= 4) continue;

        path.push_back(dir[i]);
        swap(A[nr][nc], A[Er][Ec]);
        swap(nr, Er); swap(nc, Ec);
        int t = dfs(g + 1, i, bound);
        if (t == FOUND) return FOUND;
        if (t < mn) mn = t;
        swap(nr, Er); swap(nc, Ec);
        swap(A[nr][nc], A[Er][Ec]);
        path.pop_back();
    }

    return mn;
}

bool IDAstar() {
    int bound = H();
    for (;;) {
        int t = dfs(0, -1, bound);
        if (t == FOUND) return true;
        if (t == INF) return false;
        // 下次要搜的 bound >= 50,真的解也一定 >= 50,剪枝
        if (t >= 50) return false;
        bound = t;
    }
    return false;
}

bool solvable() {
    int cnt = 0;
    bool occur[16] = {false};
    for (int r = 0; r < 4; r++) {
        for (int c = 0; c < 4; c++) {
            if (A[r][c] == 0) {
                Er = r;
                Ec = c;
            }
            else {
                cnt += count(occur + 1, occur + A[r][c], false);
                occur[A[r][c]] = true;
            }
        }
    }
    return (cnt + (Er + 1)) % 2 == 0;
}

int main() {
    int TC;
    scanf("%d", &TC);
    while (TC--) {
        for (int r = 0; r < 4; r++) {
            for (int c = 0; c < 4; c++) {
                scanf("%d", &A[r][c]);
            }
        }

        path.clear();

        if (!solvable() || !IDAstar()) {
            puts("This puzzle is not solvable.");
        }
        else {
            for (char c : path) {
                printf("%c", c);
            }
            puts("");
        }
    }

    return 0;
}