[UVA 11205] The broken pedometer (二进制枚举)

The broken pedometer

题目链接:
http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=19623

题目大意:

7
10
1 1 1 0 1 1 1
0 0 1 0 0 1 0
1 0 1 1 1 0 1
1 0 1 1 0 1 1
0 1 1 1 0 1 0
1 1 0 1 0 1 1
1 1 0 1 1 1 1
1 0 1 0 0 1 0
1 1 1 1 1 1 1
1 1 1 1 0 1 1
有7盏LED灯,开着表示1,关着表示0。用这7盏灯表示出10个不同的数,现在问最少可以用多少盏灯,就能够将这10个不同的数分辨出来

解题思路:

由于LED灯最多为15,用二进制枚举,一共就2^15种,再加上对每种情况进行判断,是否能够表示出不同的数,最多每组100次比较,所以时间复杂度为10^6左右,暴力可以完成。

代码:

#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<ctype.h>
#include<algorithm>
#include<string>
#define PI acos(-1.0)
#define maxn 1000
#define INF 1<<25
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
using namespace std;
int n, a[105], p;
void dfs()
{
    int i, j, t = 1 << p, mn = p + 1;
    for (i = 0; i < t; i++)
    {
        j = i;
        int ge = 0;
        while(j > 0)
        {
            ge += j & 1;
            j = j >> 1;
        }
        if (ge >= mn) continue;
        int bj[33000] = {0};
        for (j = 0; j < n; j++)
        {
            int k = a[j] & i;
            if (!bj[k]) bj[k] = 1;
            else break;
        }
        if (j == n) mn = ge;
    }
    printf("%d\n", mn);
    return ;
}
int main ()
{
    int t, i, j, k;
    cin>>t;
    while(t--)
    {
        scanf("%d%d", &p, &n);
        for (i = 0; i < n; i++)
        {
            a[i] = 0;
            for (j = 0; j < p; j++)
            {
                scanf("%d", &k);
                a[i] = a[i] * 2 + k;
            }
        }
        dfs();
    }
    return 0;
}


你可能感兴趣的:([UVA 11205] The broken pedometer (二进制枚举))