枚举之四

//poj 1166 The Clocks

#include
<iostream> //枚举+位运算 260K 204MS
#include<string>
using namespace std;
string str[10]={" ","ABDE","ABC","BCEF","ADG","BDEFH","CFI","DEGH","GHI","EFHI"};
// A 对应 arr[0][0], B 对应 arr[0][1],I 对应 arr[3][3]
long fac[9]={1,8,64,512,4096,32768,262144,2097152,16777216} ;
long mod=0x036DB6DB; //十六进制表示,等于 3+3*8+3*8^2+...+3*8^8 , 即是( 011 011 011 011 011 011 011 011 011 )

int main()
{
int n=0,arr[3][3],ans[10]; //ans[i]表示操作i的次数,比如ans[1]=2,则表示操作"ABDE"有2次
for(int i=0;i<3;++i)
for(int j=0;j<3;++j)
{
cin
>>arr[i][j];
for(int k=0;k<arr[i][j];++k)
n
+=fac[i*3+j]; //初始状态
}
for(ans[1]=0;ans[1]<4;++ans[1])
for(ans[2]=0;ans[2]<4;++ans[2])
for(ans[3]=0;ans[3]<4;++ans[3])
for(ans[4]=0;ans[4]<4;++ans[4])
for(ans[5]=0;ans[5]<4;++ans[5])
for(ans[6]=0;ans[6]<4;++ans[6])
for(ans[7]=0;ans[7]<4;++ans[7])
for(ans[8]=0;ans[8]<4;++ans[8])
for(ans[9]=0;ans[9]<4;++ans[9])
{
int t=n;
for(int i=1;i<10;++i)
for(int j=0;j<ans[i];++j)
for(int k=0;k<str[i].length();++k)
{
t
+=fac[str[i][k]-'A']; //对该位置的状态加上 1
t&=mod;
//与操作,相当于对九个位置的状态 %4 , 4变成0,即12点变成0点
}
if(t==0)
{
for(int i=1;i<10;++i)
for(int j=0;j<ans[i];++j)
cout
<<i<<" ";
cout
<<endl;

return 0;
}
}
}

  

posted on 2011-08-23 17:03  sysu_mjc  阅读(175)  评论(0编辑  收藏  举报

导航