POJ拨钟问题

枚举4^9次

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<algorithm>
using namespace std;


int CLOCK[9],temp[9];
int enu[9]= {0},result[9];
char cycle[9][10]= {"ABDE","ABC","BCEF","ADG","BDEFH","CFI","DEGH","GHI","EFHI"};

int MIN=1000;

bool test() {
	bool flag=true;
	for(int i=0; i<9; i++)
		if(temp[i]!=0) {
			flag=false;
			break;
		}
	return flag;
}
int main() {
	for(int i=0; i<9; i++) scanf("%d",&CLOCK[i]);
	for(int a=0; a<4; a++)
		for(int b=0; b<4; b++)
			for(int c=0; c<4; c++)
				for(int d=0; d<4; d++)
					for(int e=0; e<4; e++)
						for(int f=0; f<4; f++)
							for(int g=0; g<4; g++)
								for(int h=0; h<4; h++)
									for(int l=0; l<4; l++) {
										int k=0;//拨钟次数
										memcpy(temp,CLOCK,sizeof(int)*9);
										enu[0]=a;
										enu[1]=b;
										enu[2]=c;
										enu[3]=d;
										enu[4]=e;
										enu[5]=f;
										enu[6]=g;
										enu[7]=h;
										enu[8]=l;
										for(int i=0; i<9; i++) {//拨钟
											int t=enu[i];
											while(t--) {
												k++;
												for(int j=0; cycle[i][j]!=0; j++) temp[cycle[i][j]-'A']=(temp[cycle[i][j]-'A']+1)%4;
											}
										}
										if(k<MIN&&test()) {
											MIN=k;
											memcpy(result,enu,sizeof(int)*9);
										}
									}


	for(int i=0; i<9; i++) while(result[i]--) printf("%d ",i+1);
	return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值