关闭

【IDA*】 HDOJ 1667 The Rotation Game

标签: IDA
236人阅读 评论(0) 收藏 举报
分类:

题目肯定会有解,使用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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:187741次
    • 积分:7443
    • 等级:
    • 排名:第2919名
    • 原创:587篇
    • 转载:9篇
    • 译文:0篇
    • 评论:17条
    最新评论