UVA - 11205 The broken pedometer(子集枚举+增量构造法)

 The Broken Pedometer 

The Problem

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.

The 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.

The 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


题目大意:
有p个LED灯,可以组成一个灯牌。
灯牌上可以显示一些有意义的符号,比如显示数字啥的。
现在有n个灯牌,显示的符号各不相同。
问你最少用几个LED灯,就可以区别这些符号。
输入n和p。
其中n表示灯牌数,p表示灯数。
输出最少LED灯数。


解析:这题我是参考了刘汝佳书上7.3子集生成,中的增量构造法来写的。
先用增量构造法,纵向枚举出所有的子集,然后再讲这些子集,横向两两比较。
如果有相同的就不符合题意,求出全不相同而且最小的子集并输出就好了。


#include <stdio.h>
#include <string.h>

const int ROW = 101;
const int COL = 16;
const int INF = 0x3f3f3f;
int grid[ROW][COL];
int p,n;
int min;
bool compare(int A[],int cur) { //如果全部都不相等返回真
	bool flag;
	for(int i = 0; i < n; i++) {
		for(int j = i+1; j < n; j++) {
			flag = true;
			for(int k = 0; k < cur; k++) {
				if(grid[i][A[k]] != grid[j][A[k]]) {
					flag = false;
				}
			}
			if(flag) {
				return false;
			}
		}
	}
	return true;
}
void print_subset(int p,int A[],int cur) {
	if( compare(A,cur) && cur < min ) {
		min = cur;
	}
	int s = cur ? A[cur - 1] + 1 : 0;
	for(int i = s; i < p; i++) {
		A[cur] = i;
		print_subset(p,A,cur + 1);
	}
}

int main() {
	int A[COL];
	int t;
	scanf("%d",&t);
	while(t--) {	
		memset(grid,0,sizeof(grid));
		scanf("%d%d",&p,&n);
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < p; j++) {
				scanf("%d",&grid[i][j]);		
			}
		}
		min = INF;
		print_subset(p,A,0);
		printf("%d\n",min);
	}
	return 0;
}


你可能感兴趣的:(uva,the,Broken,pedometer)