bzoj2102[Usaco2010 Dec]The Trough Game*

bzoj2102[Usaco2010 Dec]The Trough Game

题意:

m个要求,每个要求由一个长度为n的01串和一个数组成,表示只有与给出的01串按位与后1的个数为给出数的01串满足要求。求满足所有要求的01串。m≤100,n≤20。

题解:

暴力枚举01串,我以为会超时,但没有。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 30
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 int a[maxn*10][maxn]; int n,m,b[maxn*10],ans;
15 bool check(int x){
16     inc(i,1,m){
17         int k=0; inc(j,0,n-1)if(x&(a[i][j+1]<<j))k++;
18         if(k!=b[i])return 0;
19     }
20     return 1;
21 }
22 int main(){
23     n=read(); m=read();
24     inc(i,1,m){
25         char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); int j=0;
26         while(ch>='0'&&ch<='9')a[i][++j]=ch-'0',ch=getchar();
27         b[i]=read();
28     }
29     ans=-1;
30     inc(i,0,(1<<n)-1){
31         if(check(i)){
32             if(ans!=-1){printf("NOT UNIQUE"); return 0;}else ans=i;
33         }
34     }
35     if(ans==-1)printf("IMPOSSIBLE");else{
36         inc(i,0,n-1)printf("%d",ans&(1<<i)?1:0);
37     }
38     return 0;
39 }

 

20160824

posted @ 2016-08-24 22:29  YuanZiming  阅读(210)  评论(0编辑  收藏  举报