UVA11205 The broken pedometer【位运算+暴力】

A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs):
在这里插入图片描述
    But the pedometer does not work properly (possibly the sweat affected the batteries) and only some of the LEDs are active. The runner wants to know if all the possible symbols:
在这里插入图片描述
can be correctly identified. For example, when the active LEDs are:
在这里插入图片描述
numbers 2 and 3 are seen as:
在这里插入图片描述
so they cannot be distinguished. But when the active LEDs are:
在这里插入图片描述
the numbers are seen as:
在这里插入图片描述
and all of them have a different representation.
    Because the runner teaches algorithms at University, and he has some hours to think while he is running, he has thought up a programming problem which generalizes the problem of his sweat pedometer. The problem consists of obtaining the minimum number of active LEDs necessary to identify each one of the symbols, given a number P of LEDs, and N symbols to be epresented with these LEDs (along with the codification of each symbol).
    For example, in the previous sample P = 7 and N = 10. Supposing the LEDs are numbered as:
在这里插入图片描述
The codification of the symbols is: “0” = 1 1 1 0 1 1 1; “1” = 0 0 1 0 0 1 0; “2” = 1 0 1 1 1 0 1; “3” = 1 0 1 1 0 1 1; “4” = 0 1 1 1 0 1 0; “5” = 1 1 0 1 0 1 1; “6” = 1 1 0 1 1 1 1; “7” = 1 0 1 0 0 1 1; “8” = 1 1 1 1 1 1 1; “9” = 1 1 1 1 0 1 1. In this case, LEDs 5 and 6 can be suppressed without losing information, so the solution is 5.
Input
The input file consists of a first line with the number of problems to solve. Each problem consists of a first line with the number of LEDs (P), a second line with the number of symbols (N), and N lines each one with the codification of a symbol. For each symbol, the codification is a succession of 0s and 1s, with a space between them. A 1 means the corresponding LED is part of the codification of the symbol. The maximum value of P is 15 and the maximum value of N is 100. All the symbols have different codifications.
Output
The output will consist of a line for each problem, with the minimum number of active LEDs necessary to identify all the given symbols.
Sample Input
2
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
6
10
0 1 1 1 0 0
1 0 0 0 0 0
1 0 1 0 0 0
1 1 0 0 0 0
1 1 0 1 0 0
1 0 0 1 0 0
1 1 1 0 0 0
1 1 1 1 0 0
1 0 1 1 0 0
0 1 1 0 0 0
Sample Output
5
4

问题链接UVA11205 The broken pedometer
问题简述
    给定p和n,表示有p段(p<=15)n位(n<=100以)的显示,问用几位就能够区别这些符号。
问题分析
    暴力一下就好,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA11205 The broken pedometer */

#include <bits/stdc++.h>

using namespace std;

const int P = 15;
const int N = 100;
int a[N];

int main()
{
    int t, p, n, ans;
    scanf("%d", &t);
    while(t--) {
        memset(a, 0, sizeof(a));
        scanf("%d%d", &p, &n);
        for (int i = 0; i < n; ++ i) {
            int b = 0;
            for (int j = 0; j < p; ++ j) {
                scanf("%d",  &b);
                a[i] = a[i] * 2 + b;
            }
        }

        ans = P;
        for(int i = 0; i < (1 << p); i++) {
            set<int> s;
            bool flag = false;
            for(int j = 0; j < n; j++) {
                int t = i & a[j];
                if(s.find(t) != s.end()) { flag = true; break;}
                s.insert(t);
            }
            if(!flag) {
                int res = 0;
                for(int j = 0; j < p; j++)
                    if(i & (1 << j)) res++;
                ans = min(ans, res);
            }
        }

        printf("%d\n", ans);
    }

    return 0;
}

posted on 2019-03-01 16:14  新海岛Blog  阅读(119)  评论(0)    收藏  举报

导航

// ... runAll: function() { this.resetPreCode(); hljs.initHighlightingOnLoad(); // 重新渲染,添加语法高亮 hljs.initLineNumbersOnLoad(); // 为代码加上行号 } // ...