uva 1343 The Rotation Game

本文介绍了一款名为《旋转游戏》的游戏,并采用IDA*算法进行求解。通过定义特定的数据结构和移动策略,实现了游戏状态的有效搜索。文章详细解释了如何通过IDA*算法找到最优解,包括状态评估和搜索过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:The Rotation Game


思路:IDA*。


注意:存储时可以就按题目中所给的形式。初始化时不能打错了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<sstream>
#include<queue>
using namespace std;

#define n 8
#define m 24
#define num 7

int const Move[n+5][n+5]= {{0},{0,1,3,7,12,16,21,23},{0,2,4,9,13,18,22,24},{0,11,10,9,8,7,6,5},{0,20,19,18,17,16,15,14},{0,24,22,18,13,9,4,2},{0,23,21,16,12,7,3,1},{0,14,15,16,17,18,19,20},{0,5,6,7,8,9,10,11}};
int const Central[n+5]= {0,7,8,9,12,13,16,17,18};

int maxd=0;
string Mstring;
int b;

int judge(int a[]) {
	int cnt[4]= {0};
	for(int i=1; i<=n; i++) {
		cnt[a[Central[i]]]++;
	}
	int Max=0,what;
	for(int i=1; i<=3; i++) {
		if(cnt[i]>Max) {
			Max=cnt[i],what=i;
		}
	}
	if(Max==8) return what;
	return 0-Max;
}

void DO(int (&c)[m+5],int x) {
	int t=c[Move[x][1]];
	for(int i=1; i<num; i++) {
		c[Move[x][i]]=c[Move[x][i+1]];
	}
	c[Move[x][num]]=t;
}

bool dfs(int d,int a[],string step) {
	if(d==maxd) {
		int x=judge(a);
		if(x<0) return false;
		b=x;
		Mstring=step;
		return true;
	}
	if(maxd<d+8+judge(a)) return false;
	for(int i=1; i<=n; i++) {
		int c[m+5];
		memcpy(c,a,sizeof(c));
		DO(c,i);
		if(dfs(d+1,c,step+(char)(i+'A'-1))) return true;
	}
	return false;
}

int main() {
	int a[m+5];
	while(~scanf("%d",&a[1])&&a[1]!=0) {
		for(int i=2; i<=m; i++) scanf("%d",&a[i]);
		int x=judge(a);
		if(x>0) {
			printf("No moves needed\n%d\n",x);
			continue;
		}
		maxd=0;
		while(++maxd) {
			if(dfs(0,a,"")) break;
		}
		cout<<Mstring<<endl;
		printf("%d\n",b);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值