[POJ2286] The Rotation Game

这道题显然可以用bfs,但是呢,状态空间比较多,我们还得先枚举最后的是哪个数,然后还要用hash,实在太麻烦。
所以我们决定用IDA,那么什么是IDA,IDA*=IDDFS+估价函数
实际上就是剪枝

code

点击查看代码
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=1e5+5;
int a[30],dep,step[1000],tot,mn;
char s[9]={'A','B','C','D','E','F','G','H'};
int way[8][7]={
	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,
	24,22,18,13,9,4,2,
	23,21,16,12,7,3,1,
	14,15,16,17,18,19,20,
	5,6,7,8,9,10,11
};
int p[8]={7,8,9,12,13,16,17,18};
bool pd(){
	fo(i,1,7) if (a[p[i]]!=a[7]) return 0;
	return 1;
}
int h(){
	mn=0;
	fo(num,1,3) {
		tot=0;
		fo(i,0,7) if (a[p[i]]!=num) ++tot;
		if (!mn) mn=tot;
		else mn=min(mn,tot);
	}
	return mn;
}
bool dfs(int x){
	if (x-1==dep) {
		if (!pd()) return 0;
		if (!(x-1)) {
			puts("No moves needed");
		}
		else{
			fo(i,1,x-1) printf("%c",s[step[i]]);
			printf("\n");
		}
		return 1;
	}
	
	if (x-1>dep) return 0;
	
	if (h()+x-1>dep) return 0;
	int b[25];
	fo(i,1,24) b[i]=a[i];
	fo(i,0,7) {
		step[x]=i;
		fo(j,0,5) {
			swap(a[way[i][j]],a[way[i][j+1]]);
		}
		if (dfs(x+1)) return 1;
		fo(j,1,24) a[j]=b[j];
	}
	return 0;
}
int main(){
//	freopen("data.in","r",stdin);
	while (scanf("%d",&a[1]) && a[1]){
		fo(i,2,24) scanf("%d",&a[i]);
	
		dep=0;
		while (1) {
			if (dfs(1)) break;
			dep++;
		}
		printf("%d\n",a[7]);
	}
}
posted @ 2022-09-24 16:16  gan_coder  阅读(43)  评论(0)    收藏  举报