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

 

 

 

第一种解法(超时)

 

 

#define RUN
#ifdef RUN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype> 
#include <algorithm>
#include <utility>
#include <math.h>
#include <ctime>

using namespace std;

#define MAXN 110
int P;		// Number of LEDs, eg:7 列数
int N;		// Number of symbols, eg:10 行数
char buf[101][16];	// 存储输入序列
char tmp[101][16];	// buf的复制

int myset[16];	// 排列
bool b[2^16+1];

int minOnes;

void printout(){
	for(int i=0; i<N; i++){
		for(int j=0; j<P; j++){
			cout << buf[i][j];
		}
		printf("\n");
	}
}

void printout2(){
	for(int i=0; i<N; i++){
		for(int j=0; j<P; j++){
			cout << tmp[i][j];
		}
		printf("\n");
	}
	cout << "===================" << endl;
}

int cmp(const void *a, const void *b){
	return strcmp((char*)a, (char*)b);
}

void copyBuf(){
	for(int i=0; i<101; i++){
		for(int j=0; j<16; j++){
			tmp[i][j] = buf[i][j];
		}
	}
}

bool isSame(char* a, char* b){
	for(int i=0; i<P; i++){
		if(a[i] != b[i]){
			return false;
		}
	}
	return true;
}


void decide(){
	
	int ones = 0;
	
	for(int col=0; col<P; col++){
		//printf("%d ", myset[i]);
		if(myset[col] == 1){
			ones++;
		}
		//第i列为0,即第i列不被选中,因此可以把tmp中的第i列全部赋值为0
		else{
			//memcpy(tmp, buf, sizeof(buf));
			copyBuf();
			//printout2();

			for(int row=0; row<N; row++){
				tmp[row][col] = '0';
			}

			//先排列一遍
			qsort(tmp, N, sizeof(tmp[0]), cmp);

			//检验是否有重复
			for(int i=0; i<N-1; i++){

				if(isSame(tmp[i], tmp[i+1])){
					return;
				}

			}

			if(ones < minOnes){
				minOnes = ones;
			}
		}
	}
	//printf("\n");

}

//产生00..0 到11..1的全部组合,共2^7种组合
void subset(int cur, int n){
	if(cur == n){
		decide();
		return;
	}
	else{
		myset[cur] = 1;
		subset(cur+1, n);
		myset[cur] = 0;
		subset(cur+1, n);
	}
}


int main(){

#ifndef ONLINE_JUDGE
	freopen("11205.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int cnt;
	cin >> cnt;

	memset(b, false, sizeof(b));

	while(cnt--){
		cin >> P >> N;
		for(int j=0; j<N; j++){
			for(int k=0; k<P; k++){
				cin >> buf[j][k];
			}
		}

		minOnes = P;
		//printout();
		subset(0, P);
		printf("%d\n", minOnes);
	}

	
}


#endif

 

第二种解法:

参考思想 http://www.questtosolve.com/browse.php?pid=11205

There are up to 15 different LEDs, so there are 2^15 different ways in which they might be active/broken. We can easily test all 100 * 2^15 possibilities.

Represent each symbol as an integer in binary. So, 1 1 0 1 0 is stored as 26 (or 11 if you start from the left; doesn't matter which way you do it). For i from 1 to 2^p, AND together each symbol with i. Then, check if every symbol is unique under this bitmask. We can't afford to do an O(N^2) comparison, so make a boolean array of 2^p cells, b[], and set b[j] to true if j is some symbol under the bitmask i. If the cell is already set to true, then you don't have a unique representation.

Take the minimum number of LEDs needed across all unique representations, and output it.

 

 有许多点创新:

1 读入时顺便把二进制转为了十进制

2 (1<<P) 等效于 2^P

3 避免O(N^2)的比较,事先建立bool空间来记录已访问的值

4 用位于运算和右移来计算一个十进制转为二进制后,该数含有多少位1

5 相比于第一种解法,无需特别的用subset()方法产生全排列,直接增量遍历就行了

事实证明,多次使用memset,memcpy,strcpy,strcmp等方法均会严重使运行速度降低

 

#define RUN
#ifdef RUN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype> 
#include <algorithm>
#include <utility>
#include <math.h>
#include <ctime>

using namespace std;

#define MAXN 110
int P;		// Number of LEDs, eg:7 列数,最大15
int N;		// Number of symbols, eg:10 行数,最大100
int buf[105];	// 存储输入序列,转为了十进制的形式
int tmp[105];	// buf的复制

int myset[16];	// 排列
bool b[(1<<16)];


int minOnes;


void play(){
	// 2^P-1
	int bound = (1 << P) - 1;

	int i, j;

	for(i=0; i<=bound; ++i){
		memset(b, false, sizeof(b));
		for(j=0; j<N; ++j){
			int val = buf[j] & i;
			//cout << "val:" << val << endl;
			if(b[val]){
				break;
			}
			else{
				b[val] = true;
			}
		}

		//cout << "j:" << j << endl;
		if(j == N){
			//cout << "j==N" << endl;
			//计算i的二进制数里含有1的个数
			int ones = 0;
			int bit = i;

			while(bit > 0){
				if(bit & 1){
					++ones;
				}
				bit = bit >> 1;
			}

			//cout << "ones:" << ones << endl;
			if(ones < minOnes){
				minOnes = ones;
			}
		}
	}
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("11205.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int cnt;
	cin >> cnt;

	while(cnt--){
		memset(b, false, sizeof(b));
		memset(buf, 0, sizeof(buf));
		cin >> P >> N;
		for(int i=0; i<N; i++){
			for(int j=0; j<P; j++){
				int input;
				cin >> input;

				//从左至右存储为十进制
				if(input)
					buf[i] += (input << j);
				//cout << input << " ";
			}
			//cout << endl;
			//cout << "---" << buf[i] << endl;
		}

		minOnes = P;

		play();
		
		printf("%d\n", minOnes);
	}

	
}


#endif

 

在 ABAP Cloud 里优雅读取与解析 XML:用 sXML Reader 把接口数据落成结构化 ABAP ABAP Cloud中XML解析实践 摘要:本文介绍了在ABAP Cloud环境下解析XML数据的最佳实践。针对企业系统中常见的XML格式数据,文章推荐使用sXML库中的CL_SXML_STRING_READER进行流式解析,而非传统的DOM方式。重点解析了命名空间(Namespace)、属性(Attribute)和CDATA等关键概念的处理方法,并比较了两种读取属性的策略:通扫所有属性(NEXT_ATTRIBUTE)和精准取值(GET_ATTRIBUTE_VALUE)。文章提供了完整的ABAP Cloud 阅读详情

相关推荐

从零开始搭建GD32F103CBT6开发环境:VSCode配置与实战技巧

本文详细介绍了如何从零开始为国产MCU GD32F103CBT6搭建高效的VSCode开发环境。内容涵盖工具链安装、VSCode核心插件配置、工程目录结构规划、关键配置文件编写以及Makefile实战,旨在帮助开发者摆脱传统IDE束缚,利用现代化工具链提升嵌入式开发效率。

weixin_29158413的博客 448

【UVA11205】The broken pedometer,思路+代码,可能是最不装逼最朴素最易懂效率也最差的代码。

#include #include #include #include #include using namespace std; /** Problem : UVA11205 - The broken pedometer Begin Time: 28th/Mar/2012 11:30 a.m. Finish Time: 29th/Mar/2012 2:14 a.

c0de4fun的完美算(nei)法(he)教室-我们仍不知道,那天调用的驱动的名字。 3460

用rsl_rl训练四足机器人走梅花桩:PPO算法调参实战与避坑指南

本文详细介绍了如何使用rsl_rl框架和PPO算法训练四足机器人在梅花桩上行走的实战经验。通过分析梅花桩场景的独特挑战,提供PPO算法关键参数配置策略、奖励函数设计方法以及常见问题的解决方案,帮助开发者高效实现机器人平衡控制。文章特别强调了仿真到实机的过渡技巧,最终实现98%的过桩成功率。

weixin_29103191的博客 220

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): Bu

总有那么一群人喜欢盯着我的博客看,估计这是他们见过最长的博客名,我必须得告诉他们博客名最长可以写多长 962

用Matlab搞定Delta并联机器人运动学:从建模到正逆解完整流程(附代码)

本文详细介绍了如何使用Matlab实现Delta并联机器人的运动学分析,包括几何建模、正逆解求解及可视化验证。通过符号计算和数值迭代法,解决非线性方程组问题,并提供完整代码示例,帮助读者快速掌握Delta机器人的运动学计算与应用。

weixin_30955617的博客 176

R语言clusterProfiler实战:GO与KEGG富集分析从入门到精通

本文提供了一份详尽的R语言clusterProfiler实战指南,从环境配置、数据导入、基因ID转换到GO与KEGG富集分析及可视化,手把手教你解读生物学意义。文章重点介绍了如何利用clusterProfiler这一强大工具,将差异基因列表转化为清晰的生物学通路与功能故事,并分享了结果解读、可视化以及处理非模式生物等进阶技巧与避坑指南。

weixin_29044713的博客 162

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

hexiecs的技术专栏 904

Cursor 使用教程(2025年更新版)

官方Cursor详细使用教程在“扩展学习”

我是赛博AI Lewis 7082

UVa 11205 The broken pedometer

子集枚举,很黄很暴力/* coder: ACboy date: 2010-3-18 result: AC description: UVa 11205 The broken pedometer*/#include using namespace std;char data[110][20];char temp[110][20];int a[40

个人独立博客http://www.acb0y.cn/ 1229

【运动规划算法项目实战】全覆盖分区算法

全覆盖规划在机器人学领域具有重要意义,其应用场景涵盖了自动农场、扫地机器人、扫描监视无人机等多个领域。该问题的核心目标是使机器人能够从指定起点出发,规划一条避开障碍物、同时路径尽可能短的轨迹,从而实现对特定区域的全面覆盖。在实际应用中,全覆盖规划的成功实现对于提高机器人在各类环境中的工作效率至关重要。这一问题的解决涉及到高效的路径规划和规划区域分解技术。其中,梯形单元分解算法是一种常用分区的技术,过将地图上需要规划的区域进行分区,采用梯形分解的方式,可以更好地应对复杂环境。全覆盖算法规划流程。

xiewf的博客 1万+

从毫K级温控到分子级洁净:光刻机环境控制系统的技术演进与专利格局

摘要 光刻机环境控制系统是半导体制造中的"隐形精密工程",通过毫K级温度控制(±0.002℃)、超高洁净度(ISO Class 1)等核心技术为纳米级光刻提供环境保障。本文系统分析了温度、湿度、洁净度、流场和压力控制五大技术方向的发展趋势,揭示了从单参数控制向多参数协同智能控制的转型路径。研究显示:1)温度控制正从PID向神经网络智能控制演进;2)EUV光刻将湿度控制推至ppbv级;3)洁净度控制扩展到分子级污染物管理。头部厂商中,ASML侧重系统集成,Nikon专注浸没流场,Canon

huanghm88的专栏 312

UVA11205 The broken pedometer【位运算+暴力】

A marathon runner uses a pedometer with which he is having problems. In the pedometer the symbols are represented by seven segments (or LEDs): 问题链接:UVA11205 The broken pedometer 问题简述:(略) 问题分析: &amp;amp;amp;amp;nbsp;&amp;amp;amp;amp;...

海岛Blog 671

手机相机Tuning专项调优(一):基础流程与图像主观评价标准建立实战解析

本篇为手机相机系统“Tuning专项调优”系列的开篇,深入剖析了当前手机相机Tuning的基本流程与图像主观评价标准的构建方法。结合实际工程经验与最新业界案例,详细介绍ISP调优和3A控制中涉及的主观评价标准,如锐度、色彩饱和度、噪声感知等,并提出一套可工程化落地的标准制定与评测实践体系,帮助工程师有效提升图像品质。

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 1933

UVA 11205 The broken pedometer

UVA 11205 The broken pedometer题目大意:给出几行数字组合,求出至少取几列可以分辨出不同行解题思路:刚刚开始学子集生成,方法比较笨,位向量法生成子集,去二进制数,然后排序,求相邻的数是否都不相同#include <stdio.h> #include <iostream> #include <string.h> #include <math.h> using namespa

ZeroLH00的博客 425

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

UVA 11205 The broken pedometer

UVA-11205题意:给出n个p位的2进制串,求最少保留多少位可以区分这些2进制串。 解题思路:枚举子集。1 -> 2^p 中的每个数字用二进制表示都是一种子集的获取方式。把2进制串转成10进制,用 & 运算的取保留下的值,判断是否都不一样,都不一样计算位数,取最小的一个。/*************************************************************

QzNarsh的博客 389

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

UVa-11205-The broken pedometer

AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 3. Brute Force :: Elementary Skills

侘寂 399

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

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

不慌不忙、不急不躁 1341

UVA 11205 The broken pedometer 果断还是暴力吧。。。

UVA 11205 The broken pedometer 这题题意一开始还理解错了。后来才知道是输出最少开几个灯,能表示所有数字。。 一开始的想法是深搜。。把每一种情况找出来求出最少的。。结果很悲剧啊 超时了无数次。。。 后来换思路。。用暴力。。大概思路 灯可能是开着或者关着的, 题目中灯为P盏,一盏灯有2种状态,按这样来考虑的话 一共只要枚举 2^p次 而p最多为15,

Remilia's 2465

uva 11205 The broken pedometer

dfs递归枚举所有的子集,检验每一个子集是否能够使得每一个数字的编码不同,如果发现这样的子集,就统计其中1的个数,将结果更新为当前结果和前面统计出的1的个数的较小值,递归枚举结束后,得到的全局变量result就是最后的结果。 #include #include #include #include using namespace std; vector original_code;

皓首不倦 不负少年 618

UVa 11205 - The broken pedometer

求可区分出所有行的最小列数,用位向量法生成子集,然后枚举最小子集。最需要注意的是其0与1之间可能不止一个空格,开始用char型数组 scanf("%c%c", &a[i][j], &c); 读取的,因为这个WA了好几次,如果直接用int型数组求则可以省去很多麻烦。 代码如下: #include #include #include #include #include #includ

GooMaple 1263

Uva OJ 11205 - The broken pedometer

描述: 输入t,p,n,n个数的显示法              t 表示有几组测试数据,p表示有多少条显示条,n表示需要显示的数据有多少    (0                                                                                        题意: p 条显示条中能否找到最少的条数便可表示 n 个数(即 找

852

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

uva code 1732

uva11205 The broken pedometer

计时器坏了 判断至少几列数字可以判断出这几个数字 子集 位向量法 #include #include int mat[105][20], b[20]; int min, p, n; void subset(int cur){ if (cur == p) { for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n;

katydid3018的博客 459
上一篇: qsort()应用大全
下一篇: 夜宵频频胃黏膜难修复
chiiis
博客等级 码龄14年 422粉丝 429原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值