首页 > 代码库 > uva 11205 The broken pedometer (暴力)

uva 11205 The broken pedometer (暴力)

                        uva 11205 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 numberP of LEDs, and N symbols to be represented with these LEDs (along with the codification of each symbol).

For example, in the previous sampleP = 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), andN 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 ofP 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


题目大意:第一个数据代表有几组数据,接下来的两个数据分别是可以亮的LED灯数量n,以及要表达的字符的数量m,接下来是m行n列的数据,要求最少只需要几盏LED灯就可以表示那m个数据。

解题思路:我借鉴了大神的代码,先枚举灯数,然后再由灯数枚举所有组成情况,如果当中有一组符合要求,即是当前灯数,否则灯数+1。


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int A[105][20], T[20], m, n;
int judge(int a, int b, int sum)  
{  
	for(int i = 1; i <= sum ;i++)  
	{  
		if(A[a][T[i]] != A[b][T[i]])  
			return 0;  
	}  
	return 1;  
}  

int find(int k)  
{  
	for(int a = 0; a < n; a++) { 
		for(int b = a + 1; b < n; b++) { 
			if( judge(a, b, k) ) {return 0;}  
		}
	}
	return 1;  
}  

int build(int sum, int k)  
{  
	if(sum != k - 1)  
	{  
		for(T[k] = T[k - 1] + 1; T[k] < m; T[k]++) { 
			if(build(sum, k + 1)) {return 1;}  
		}
	} 
	else  
	{  
		if(find(sum)) {return 1;}  
	}  
	return 0;  
}  

int main() {
	int NUM;
	while (scanf("%d", &NUM) == 1) {
		while (scanf("%d\n%d", &m, &n) == 2) {
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					scanf("%d", &A[i][j]);
				}
			}
			int i;  
			for(i = 0; i <= m; i++)  
			{  
				memset(T, 0, sizeof(T));  
				T[0] = -1;  
				if(build(i,1)) {break;}  
			}  

			printf("%d\n", i);  
		}
	}
	return 0;
}



uva 11205 The broken pedometer (暴力)