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

我终于可以说这是我自己独立完成的题目了,没看题解,没看注释,虽然用的时间成了写,总归有成就感的,昨天晚上就写了个大概,有点bug,由于太晚了,而且有点困了,就去睡了,当时真是自己认真想了的,,很深入的想了,用的书上刚学会的位向量自己生成来判断的。以后都要努力自己想,自己解决,专注。。。深入。。。。

思路:

就是先算出最少用m个灯才能表示n个数字,然后找第一个数字(由许多灯组成的0,1序列)的个数为m的子

集,把这n个子集作为n个数字的下标,判断一下有没有玩去一样的,如果有的话证明这两个数字不能通过m个灯来判

断,然后就把m加1,找第一个数字个数为m+1的子集,和m的时候一样进行判断

代码:

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

int B[20];
int sum;
int map[105][20];
int ans;
int res[20];
int flag,n;

void find(int m,int *B,int cur)
{
	if(flag)
		return ;
	if(cur == m)
	{
		if(ans!=sum)
			return ;
		int k = 0;
		memset(res,0,sizeof(res));
		for(int i=0; i<cur; i++)
		{
			if(B[i])
			{
				res[k++] = i;
			}
		}
		//puts("");
		//puts("***********************");
		//for(int p=0; p<k; p++)
		//	printf("%d ",res[p]);
		//puts("");
		for(int i=0; i<n; i++)
		{
			for(int j=i+1; j<n; j++)
			{
				int nodeng = 0;
				for(int p=0; p<k; p++)
				{
					//printf("%d ",res[p]);
					if(map[i][res[p]]!=map[j][res[p]])
					{
						//printf("ii = %d jj = %d\n",i,j);
						//printf("k = %d\n",k);
						//printf("p = %d\n",res[p]);
						nodeng = 1;
						break;
					}
				}

				//puts("**************");
				if(nodeng==0)
				{
				//	printf("i = %d j = %d\n",i,j);
				//	printf("map[0][3] = %d map[8][3] = %d\n",map[0][3],map[8][3]);
					//system("pause") ;
					return ;
				}
			}
		}
		flag = 1;
		//puts("***********************");
		return ;
	}
	B[cur] = 1;
	ans++;
	find(m,B,cur+1);
	B[cur] = 0;
	ans--;
	find(m,B,cur+1);
}
int main()
{
	int T,m,i,j;
	scanf("%d",&T);
	while(T--)
	{
		memset(B,0,sizeof(B));
		memset(map,0,sizeof(map));
		scanf("%d%d",&m,&n); //m表示灯的个数,n表示数字的个数
		for(i=0; i<n; i++)
			for(j=0; j<m; j++)
				scanf("%d",&map[i][j]);
		//printf("**map[0][3] = %d map[8][3] = %d\n",map[0][3],map[8][3]);
		if(n==1)
		{
			printf("%d\n",0);
			continue;
		}
		if(m==0||n==0)
		{
			printf("%d\n",0);
			continue;
		}
		int x = 1;
		ans = 0;
		for(i=1; i<=m; i++)
		{
			x*=2;
			if(x >= n)
			{
				x = i;
				break;
			}
		}
		//printf("x = %d\n",x);
		sum = x;
		flag = 0;
		for(sum=x; sum<=m; sum++)
		{
			find(m,B,0);
			if(flag)
			{
				printf("%d\n",sum);
				break;
			}
		}
	}
	return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 00:28:34

uva 11205 The broken pedometer(暴力枚举+子集生成)的相关文章

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

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

HDU 5616 Jam&#39;s balance(暴力枚举子集)

题目链接:点击打开链接 题意:有一个没有游标的天平,和n个秤砣,m个询问, 每次一个k,问可否秤出k这个重量. 秤砣可以放两边. 思路:因为n最大20, 暴力枚举子集. 因为可以放两边, 所以每次再跑一遍, 减去每个的重量, 将答案保存. 比赛的时候忘了限制边界,虽然过了终测数据, 却被人用大数据hack了(RE), 还是自己程序写的不够鲁棒, 思考的不完善. 细节参见代码: #include<cstdio> #include<cstring> #include<algori

【UVA】11464-Even Parity(二进制枚举子集)

枚举第一行的所有可能情况,之后根据上面行计算下面行(判断是否冲突),获得最终结果. 14058243 11464 Even Parity Accepted C++ 0.275 2014-08-18 05:14:15 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<stack> #inc

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 1354 Mobile Computing(天平难题,枚举子集,递归,好题*)

1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 /** 6 思路:在每一个根节点枚举左右子树 7 8 学习: 9 (1)枚举子集的方法 例如 枚举 s = 100101 的子集 10 for(int l = (s-1)&s , l > 0 ; l = (l-1) & s){ 11 int r = s ^ l; 12 (2)思考:如何表示 左边距离 右边距离 . 该点的

uva 1508 Equipment(暴力+枚举子集)

uva 1508 Equipment 题目大意:给出n个5元组,要求从中选取k个,要求5个位置上的数的最大值的和尽量大. 解题思路:一开始没思路,看别人题解过的.5位可以组成32种情况,在DFS前预处理,找出32种情况在n组数据中的最大值,然后将这组数据带入DFS中枚举计算. #include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> using namespace