1077 Eight NI

A* 康拓cantor 曼哈顿距离
BFS是退化的A星
变量名写重了 搞得我找BUG找了好久 要注意四个方向的命名

参考 https://blog.csdn.net/dolfamingo/article/details/77855498
感觉他写的有些细节不好

Description
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.
Input

You will receive a description of a configuration of the 8 puzzle. The 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
Output

You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces and start at the beginning of the line.
Sample Input

2 3 4 1 5 x 7 6 8
Sample Output
ullddrurdllurdruldr

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int si = 10, MAXN = 1e6+10, goal = 1;
int fac[si];
int pre[MAXN];
bool vis[MAXN];
char path[MAXN];
char op[4] = {'u', 'd', 'l', 'r'};
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};

struct node {
    int h, g, f, loc, sta;
    int s[9];
    bool operator<(const node x)const{
        return f>x.f;
    }
};
int cantor(int array[]) {
    int sum = 0;
    for (int i = 0; i < 9; i++) {
        int num = 0;
        for (int j = i + 1; j < 9; j++) {
            if (array[j] < array[i]) num++;
        }
        sum += num * fac[8 - i];
    }
    return sum + 1;
}
int dish(int s[]) {
    int dis = 0;
    for (int i = 0; i < 9; i++) {
        if (s[i] == 9) continue;
        int xx = i / 3, yy = i % 3, c = s[i] - 1;
        int x = c / 3, y = c % 3;
        dis += abs(x - xx) + abs(y - yy);
    }
    return dis;
}
priority_queue pq;
bool Astar(node now) {
    fill(vis, vis + MAXN, 0);
    while (!pq.empty()) pq.pop();

    now.sta = cantor(now.s);
    now.g = 0;
    now.h = dish(now.s);
    now.f = now.g + now.h;
    pre[now.sta] = -1;
    vis[now.sta] = 1;
    pq.push(now);
    while (!pq.empty()) {
        now = pq.top(); pq.pop();
        if (now.sta == goal) return 1;
        int loc = now.loc;
        int x = loc / 3, y = loc % 3;
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (xx < 0 || yy < 0 || xx >= 3 || yy >= 3) continue;
            node tp = now;
            int newloc = xx * 3 + yy;
            tp.s[loc] = tp.s[newloc];
            tp.s[newloc] = 9;
            tp.sta = cantor(tp.s);

            if (vis[tp.sta]) continue;
            vis[tp.sta] = 1;
            tp.loc = newloc;
            tp.g++;
            tp.h = dish(tp.s);
            tp.f = tp.g + tp.h;

            pre[tp.sta] = now.sta;
            path[tp.sta] = op[i];
            pq.push(tp);
        }
    }
    return 0;
}
void Print(int sta) {
    if (pre[sta] == -1) return;
    Print(pre[sta]);
    printf("%c", path[sta]);
}
int main() {
    char str[50];
    fac[0] = 1;
    for (int i = 1; i < si; i++) fac[i] = fac[i - 1] * i;
    while(gets(str)) {
        node now;
        int pos = 0;
        for (int i = 0; str[i]; i++) {
            if (str[i] == ' ') continue;
            if (str[i] == 'x') {
                now.loc = pos;
                now.s[pos++] = 9;
                continue;
            }
            now.s[pos++] = str[i] - '0';
        }
        if (Astar(now)) Print(goal);
        else printf("unsolvable");
        puts("");
    }
    return 0;
}

你可能感兴趣的:(1077 Eight NI)