【例 7-12 UVA - 1343】The Rotation Game

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


迭代加深搜索。
每次抽动操作最多只会让中间那一块的区域离目标的“距离”减少1.
以这个作为剪枝。
枚举最大深度。
就能过了。

【代码】

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

const int N = 10;

int b[N][N] = { { 1, 3, 7, 12, 16, 21, 23 }, { 2, 4, 9, 13, 18, 22, 24 }, { 11, 10, 9, 8, 7, 6, 5 }, { 20, 19, 18, 17, 16, 15, 14 } };
int fan[N];
int a[30];
int c[N + 5] = { 7, 8, 9, 12, 13, 16, 17, 18 };
int maxdep, ans;
vector <int> v, vans;

void init(){
    for (int i = 0; i < 4; i++){
        int temp;
        if (i & 1) temp = 3; else temp = 5;
        int j = i + temp;
        fan[i] = j;
        fan[j] = i;
    }
    for (int i = 4; i < 8; i++){
        int pre = fan[i];
        int now = 0;
        for (int j = 6; j >= 0; j--){
            b[i][now] = b[pre][j];
            now++;
        }
    }
}

int ok(){
    int mi = 8;
    for (int i = 1; i <= 3; i++){
        int cnt = 0;
        for (int j = 0; j < 8; j++) if (a[c[j]] != i) cnt++;
        mi = min(mi, cnt);
    }
    return mi;
}

void Move(int idx){
    int temp = a[b[idx][0]];
    for (int i = 0; i < 6; i++){
        int x = b[idx][i], y = b[idx][i + 1];
        a[x] = a[y];
    }
    a[b[idx][6]] = temp;
}

void dfs(int dep){
    if (ans != 0) return;
    int now = ok();
    if (dep == maxdep){
        if (now == 0){
            vans = v;
            ans = a[c[0]];
        }
        return;
    }
    int rest = maxdep - dep;
    if (rest < now) return;
    for (int i = 0; i < 8; i++){
        v.push_back(i);
        Move(i);
        dfs(dep + 1);
        v.pop_back();
        Move(fan[i]);
    }
}

int main()
{
    #ifdef LOCAL_DEFINE
        freopen("F:\\c++source\\rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0), cin.tie(0);
    init();
    while (cin >> a[1] && a[1]){
        for (int i = 2; i <= 24; i++) cin >> a[i];
        if (ok() == 0){
            cout << "No moves needed" << endl;
            cout << a[c[0]] << endl;
            continue;
        }

        ans = 0;
        for (maxdep = 1;; maxdep++){
            dfs(0);
            if (ans != 0) break;
        }
        for (int i = 0; i<(int)vans.size(); i++){
            cout << (char)(vans[i] + 'A');
        }
        cout << endl;
        cout << ans << endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/8036610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值