UVa-11205-The broken pedometer

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 3. Brute Force :: Elementary Skills


// 11205 - The broken pedometer
#include <iostream>
#include <cstring>
using namespace std;
 
int check(int* c, int n)
{
    for(int i=0; i<n; i++)
        for(int j=i+1; j<n; j++)
            if(c[i] == c[j])
                return 0;
    return 1;
}
 
int count(int k)
{
    int s = 0;
    while(k != 0)
    {
        s += k & 1;
        k >>= 1;
    }
    return s;
}
 
int main(void)
{
    int i, j, k, t, p, n, s, min;
    int a[101][16], b[101], c[101];// 分别存放输入/输入的十进制形式/与运算结果
    while(cin >> t)
    while(t--)
    {
        memset(b, 0, sizeof(b));
        cin >> p >> n;
        for(i=0; i<n; i++)
            for(j=0; j<p; j++)
            {
                cin >> a[i][j];				// 以“二进制”形式输入
                b[i] += a[i][j]<<(p-j-1);	// "二进制"转化为十进制
            }
 
        min = p;
        for(k=0; k<(1<<p); k++)         	// 枚举所有子集(0~2^p-1)
        {
            for(j=0; j<n; j++)
                c[j] = k & b[j];          	// 与运算
            if(check(c, n))                 // 判重
            {
                s = count(k);
                if(s < min)					// 最开始遇到的不一定最小
                	min = s;
            }
        }
 
        cout << min << endl;
    }
    return 0;
}


posted @ 2014-08-17 15:15  颜威  阅读(113)  评论(0编辑  收藏  举报