UVA 11205 The broken pedometer

本文探讨了如何通过改变部分LED灯的状态来区分不同的数字,并通过算法找到最少需要激活的LED灯数量,实现数字的唯一标识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析

使用一些LED灯的亮与灭来拼出一些数字,问如果拿去一些LED灯,是否还能区别这些数字呢?输出需要的最小的LED灯的数量。

这里,需要生成排列,即使用哪一些LED灯的排列,这里使用0,1表示使用或不使用,标记于flag数组中。

对于每一种排列,尝试把数字用这种方式表达(转换为十进制),再与其他的数字表达比较是否重复,如果重复说明这种排列不能区分,如果不重复则更新最小数量M

代码

#include <cstdio>
#define MAX_N 105
#define MAX_P 20

int P, N, M;
int led[MAX_N][MAX_P];
int a[MAX_N];
int flag[MAX_P];

void judge()
{
    int set_size = 0, idx = 0;
    for (int i = 0; i < P; i++)
        if (flag[i]) set_size++;
    if (set_size >= M) return;

    for (int i = 0; i < N; i++) {
        int cur_num = 0;
        for (int j = 0; j < P; j++)
            if (flag[j] && led[i][j])
                cur_num += (1<<j);
        for (int j = 0; j < idx; j++)
            if (a[j] == cur_num) return;
        a[idx++] = cur_num;
    }
    if (set_size < M) M = set_size;
}

void sub_set(int cur)
{
    if (cur == P) { judge(); return; }
    flag[cur] = 0; sub_set(cur + 1);
    flag[cur] = 1; sub_set(cur + 1);
}

void solve()
{
    M = P;
    sub_set(0);
    printf("%d\n", M);
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &P, &N);
        for (int i = 0; i < N; i++)
            for (int j = 0; j < P; j++)
                scanf("%d", &led[i][j]);
        solve();
    }
    return 0;
}

题目

Description

A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs):

8

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:

0123456789

can be correctly identified. For example, when the active LEDs are:

5

numbers 2 and 3 are seen as:

23

so they cannot be distinguished. But when the active LEDs are:

A

the numbers are seen as:

0123456789

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 represented 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:

8

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值