USACO:Hamming Codes

/*
ID: Jang Lawrence
PROG: hamming
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#define mp make_pair
using namespace std;
int n,b,d,a[100],ma;
int countbit(int x){return x==0?0:1+countbit(x&(x-1));}
bool f(int x,int y)
{
    for(int i=1;i<=y;++i)
    if(countbit(x^a[i])<d) return false;
    return 1;
}
bool  dfs(int cnt)
{
    if(cnt==n+1)
    {
        for(int i=1;i<=n;++i)
        printf("%d%c",a[i],i==n||i%10==0?'\n':' ');
        return 1;
    }
    if(cnt==1)
    {
        for(int i=0;i<=ma-n+1;++i)
        {
            a[cnt]=i;
            if(dfs(cnt+1)) return 1;
        }
    }
    for(int i=a[cnt-1]+1;i<=ma;++i)
    if(f(i,cnt-1))
    {
       a[cnt]=i;
       if(dfs(cnt+1)) return 1;
       else continue;
    }
    return 0;
}
int main()
{
  #ifndef  DEBUG
  freopen("hamming.in","r",stdin);
  freopen("hamming.out","w",stdout);
  #endif
   scanf("%d%d%d",&n,&b,&d);
   ma=(1<<b)-1;
   dfs(1);
    return 0;
}

你可能感兴趣的:(USACO:Hamming Codes)