1077 Eight NI

作者: smatrcHendsa | 来源:发表于2019-03-22 16:57 被阅读0次

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 <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
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<node> 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

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

  • 1077

    讲:“世间没有好,也没有坏,只有因果在,世间没有对,也没有错,只有分别在,世间没有是,也没有非,只有执着在。愿有缘...

  • 1077

    9月6日,农历八月十一,多云,周二 距离中秋节还有四天,这周六就过节。 下班直接去加油站,替娃爸提过节物品。回家咱...

  • 9Ni490D、9Ni590A、9Ni590B钢板

    9Ni490D、9Ni590A、9Ni590B钢板 系列牌号;9Ni490D、9Ni590A、9Ni590B 常用...

  • 幸福成功日记2022-09-22(第一百零七天)

    连续早起天数:1077天打卡 早睡打卡天数:944天打卡 累计天数:7/36/1077/944 日期: 2022....

  • eight

    军训结束的那天晚上,班主任开了班会,为管理班级方便同学,他又添了几个班委。 他先选了吕大鹏作班里的公物员,还...

  • Eight

    坚持就是胜利✌️继续努力,加油! 今日工作安排 一:练字✅ 二:三杯水✅ 三:开合跳50个➕瘦臂运动50个✅ 四:...

  • eight

    当这道影子完全出现的时候,又会有这种错觉,这道影子一直在这儿。 “程,好久不见,还是那么漂亮。”影子说完后径直来到...

  • Eight

    要记录些什么,作为今天的存在痕迹,年年岁岁花相似,岁岁年年人不同,钟表可以回到起点却已不是昨天,我该做些什么...

  • Eight

    由于最近一直处于逆境之中,心绪不宁,除了每天坚持练字以求静心之外,其他事都没有心情去做!实属无奈,虽然知道自己不该...

网友评论

    本文标题:1077 Eight NI

    本文链接:https://www.haomeiwen.com/subject/nkurvqtx.html