分析
使用一些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):
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 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:
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