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 <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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,265评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,078评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,852评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,408评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,445评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,772评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,921评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,688评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,130评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,467评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,617评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,276评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,882评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,740评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,967评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,315评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,486评论 2 348

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,308评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 11,922评论 3 20
  • 老莫预谋着一场出走,预谋了很多年,却一直没有成行。 他是七十代生人,是家中的独子,他头上倒是有两个姐姐,但对他父母...
    甘草子的简书阅读 532评论 0 0
  • 老家村子里有一个集市,这集市不大,从街头到街尾也才几百米的样子,石板铺成的街道,两边并排着一栋栋历经岁月的木楼,看...
    龙门故事客栈阅读 568评论 0 26