179. 八数码

在一个 3×3 的网格中,1∼8 这 8 个数字和一个 X 恰好不重不漏地分布在这 3×33×3 的网格中。

例如:

1 2 3
X 4 6
7 5 8

在游戏过程中,可以把 X 与其上、下、左、右四个方向之一的数字交换(如果存在)。

我们的目的是通过交换,使得网格变为如下排列(称为正确排列):

1 2 3
4 5 6
7 8 X

例如,示例中图形就可以通过让 X 先后与右、下、右三个方向的数字交换成功得到正确排列。

交换过程如下:

1 2 3   1 2 3   1 2 3   1 2 3
X 4 6   4 X 6   4 5 6   4 5 6
7 5 8   7 5 8   7 X 8   7 8 X

把 X 与上下左右方向数字交换的行动记录为 udlr

现在,给你一个初始网格,请你通过最少的移动次数,得到正确排列。

输入格式

输入占一行,将 3×3 的初始网格描绘出来。

例如,如果初始网格如下所示:

1 2 3 
x 4 6 
7 5 8 

则输入为:1 2 3 x 4 6 7 5 8

输出格式

输出占一行,包含一个字符串,表示得到正确排列的完整行动记录。

如果答案不唯一,输出任意一种合法方案即可。

如果不存在解决方案,则输出 unsolvable

输入样例:

2  3  4  1  5  x  7  6  8 

输出样例

ullddrurdllurdruldr

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <unordered_map>
using namespace std;
constexpr int N=100+7;
//#define int long long
const int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1};
const char op[] = "urdl";
typedef pair <int,string> PIS;
char ch;
string start,s;
int f (string state) {
    int ans = 0;
    for (int i = 0;i < 9;i++) {
        if (state[i] == 'x') continue;
        int t = state[i] - '1';
        ans += abs (i / 3 - t / 3) + abs (i % 3 - t % 3);
    }
    return ans;
}
string Astar () {
    string end = "12345678x";
    unordered_map <string,int> dist;
    unordered_map <string,pair <char,string>> prev;
    priority_queue <PIS,vector <PIS>,greater <PIS> > heap;
    dist[start] = 0;
    heap.push ({f (start),start});
    while (!heap.empty ()) {
        auto t = heap.top ();
        heap.pop ();
        string state = t.second;
        if (state == end) break;
        int x,y;
        for (int i = 0;i < 9;i++) {
            if (state[i] == 'x') {
                x = i / 3,y = i % 3;
                break;
            }
        }
        string source = state;
        for (int i = 0;i < 4;i++) {
            int a = x + dx[i],b = y + dy[i];
            if (a < 0 || a > 2 || b < 0 || b > 2) continue;
            swap (state[a*3+b],state[x*3+y]);
            if (!dist.count (state) || dist[state] > dist[source] + 1) {
                dist[state] = dist[source] + 1;
                prev[state] = {op[i],source};
                heap.push ({dist[state] + f(state),state});
            }
            state = source;
        }
    }
    string ans;
    while (end != start) {
        ans = prev[end].first + ans;
        end = prev[end].second;
    }
    return ans;
}
int main () {
    for (int i = 0;i < 9;i++) {
        cin >> ch;
        start += ch;
        if (ch != 'x') s += ch;
    }
    int cnt = 0;
    for (int i = 0;i < 8;i++) {
        for (int j = i;j < 8;j++) {
            if (s[i] > s[j]) cnt++;
        }
    }
    if (cnt & 1) puts ("unsolvable");
    else cout << Astar () << endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值