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

题目大意:第一个数据代表有几组数据,接下来的两个数据分别是可以亮的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;
}
时间: 2024-08-28 17:09:39

uva 11205 The broken pedometer (暴力)的相关文章

uva 11205 The broken pedometer(暴力枚举+子集生成)

我终于可以说这是我自己独立完成的题目了,没看题解,没看注释,虽然用的时间成了写,总归有成就感的,昨天晚上就写了个大概,有点bug,由于太晚了,而且有点困了,就去睡了,当时真是自己认真想了的,,很深入的想了,用的书上刚学会的位向量自己生成来判断的.以后都要努力自己想,自己解决,专注...深入.... 思路: 就是先算出最少用m个灯才能表示n个数字,然后找第一个数字(由许多灯组成的0,1序列)的个数为m的子 集,把这n个子集作为n个数字的下标,判断一下有没有玩去一样的,如果有的话证明这两个数字不能通

UVa 11205 - The broken pedometer

题目:给你p个LED组成的相同的显示器n个,每个显示器上显示一个符号(LED的p长度的01串) 问最少使用p位中的几个位,就能区分这n个不同符号,均不相同即可(其他位当做置0处理) 分析:搜索.枚举.从保留1位开始,一直搜索到p为,出现满足题意的解就退出,即可. 枚举采用位运算,提高效率. 说明:寻找相同的时候,先排序,再判断相邻的即可(n lg(n)):也可以使用hash提高效率. #include <algorithm> #include <iostream> #include

UVA 11205 The broken pedometer(子集枚举)

B - The broken pedometer Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-18) Description  The Broken Pedometer  The Problem A marathon runner uses a pedometer with wh

UVa 11025 The broken pedometer【枚举子集】

题意:给出一个矩阵,这个矩阵由n个数的二进制表示,p表示用p位二进制来表示的一个数 问最少用多少列就能将这n个数区分开 枚举子集,然后统计每一种子集用了多少列,维护一个最小值 b[i]==1代表的是选择了这一列 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector&g

【UVA】12169-Disgruntled Judge(暴力or欧几里得)

可能由于后台数据的原因,这道题直接暴力枚举a,b进行判断也能过,不过跑的时间长,效率太差了. 14021006 12169 Disgruntled Judge Accepted C++ 0.876 2014-08-11 08:46:28 不说了,比较无脑. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #incl

The broken pedometer-纯暴力枚举

The broken pedometer Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  The Broken Pedometer  The Problem A marathon runner uses a pedometer with which he is having problems. In the pedometer the symb

uva 10825 - Anagram and Multiplication(暴力)

题目链接:uva 10825 - Anagram and Multiplication 题目大意:给出m和n,要求找一个m位的n进制数,要求说该数乘以2~m中的任意一个数的结果是原先数各个位上数值的一个排序. 解题思路:枚举最后一位数,然后用这个数去乘以2~m并对n取模,然后得到的数一定就是这个数的组成,暴力搜索一下并判断. #include <cstdio> #include <cstring> #include <algorithm> using namespace

uva 11256 - Repetitive Multiple(gcd+暴力)

题目链接:uva 11256 - Repetitive Multiple 题目大意:给定一个数n,要求找到最小的k,使得k?n为题目中定义的重复数字. 解题思路:枚举k?n的循环节长度,比如当前枚举为2,那么一次判断u=1001,1001001,1001001001 ...,取d = gcd(n,u), 那么k = u / d, a = n / d (因为n?k=u?a)并且保证a的长度为2,所以k和a要同时扩大相应倍数.枚举过程中为何k. #include <cstdio> #include

uva 646 - The Gourmet Club(暴力)

题目链接:uva 646 - The Gourmet Club 题目大意:有16个人参加聚会,聚会一共5天,每天有4桌,每桌4个人,一起吃饭的4个人会互相认识.现在要安排座位使得16个任意两个人都互相认识.给出前三天的安排,求后两天的安排. 解题思路:任意两个人之间肯定只能同桌一次.所以根据这个条件,只要枚举出第4天的第1桌的情况,就可推导出所有的,或者是矛盾. 在Poj和Zoj上都过了,uva上过不了,求大神指教. #include <stdio.h> #include <string