【IDA*】 HDOJ 1667 The Rotation Game

题目肯定会有解,使用IDA*算法。。。枚举转的次数,对于确定的次数,dfs搜索,搜索剪枝是当前8个格子最大的相同的数量+当前还剩余的步数还到不了8个,就剪枝。。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 200005
#define maxm 400005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int dir[8][7] = {
{0, 2, 6, 11, 15, 20, 22},
{1, 3, 8, 12, 17, 21, 23},
{10, 9, 8, 7, 6, 5, 4},
{19, 18, 17, 16, 15, 14, 13},
{23, 21, 17, 12, 8, 3, 1},
{22, 20, 15, 11, 6, 2, 0},
{13, 14, 15, 16, 17, 18, 19},
{4, 5, 6, 7, 8, 9, 10}};
int f[8] = {5, 4, 7, 6, 1, 0, 3, 2};
vector<int> ans;
int a[maxn];
int flag, aa, bb, cc;

void fun(int x)
{
    for(int i = 0; i < 6; i++) swap(a[dir[x][i]], a[dir[x][i+1]]);
}

int calc(int x)
{
    int res = 0;
    for(int i = 6; i <= 8; i++) res += a[i] == x;
    for(int i = 15; i <= 17; i++) res += a[i] == x;
    res += a[11] == x;
    res += a[12] == x;
    return res;
}

void debug()
{
    for(int i = 0; i < 24; i++) printf("PPP %d\n", a[i]);
}

void dfs(int step)
{
    if(flag) return;
    int ok = 0;
    int a2 = calc(1), b2 = calc(2), c2 = calc(3);
    if(a2 == 8 || b2 == 8 || c2 == 8) {
        flag = 1;
        for(int i = 0; i < ans.size(); i++) printf("%c", ans[i] + 'A');
        if(ans.size() == 0) printf("No moves needed");
        printf("\n");
        if(a2 == 8) printf("1\n");
        else if(b2 == 8) printf("2\n");
        else printf("3\n");
        return;
    }
    if(aa >= 8 && a2 + step >= 8) ok = 1;
    if(bb >= 8 && b2 + step >= 8) ok = 1;
    if(cc >= 8 && c2 + step >= 8) ok = 1;
    if(!ok) return;
    if(step == 0) return;    
    
    for(int i = 0; i < 8; i++) {
        fun(i);
        ans.push_back(i);
        dfs(step-1);
        fun(f[i]);
        ans.pop_back();
        if(flag) return;
    }
}

void work()
{
    for(int i = 1; i < 24; i++) scanf("%d", &a[i]);
    aa = bb = cc = flag = 0;
    for(int i = 0; i < 24; i++) {
        if(a[i] == 1) aa++;
        if(a[i] == 2) bb++;
        if(a[i] == 3) cc++;
    }
    ans.clear();
    for(int i = 0; i <= 100; i++) {
        if(!flag) dfs(i);
    }
}

int main()
{
    while(scanf("%d", &a[0]), a[0]) {
        work();
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值