UVA 11205 The broken pedometer 状态压缩+搜索
2013-09-04 14:10
330 查看
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
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <math.h>using namespace std;int n,p;int vis[111],res;bool b[1<<16];void solve(){ int i,j; int sum=(1<<p)-1; for(i=0;i<=sum;i++) { memset(b,false,sizeof(b)); for(j=0;j<n;++j) { int val=i&vis[j]; if(b[val]) break; else b[val]=true; } if(j==n) { int ans=0; int a=i; while(a>0) { if(a&1) ans++; a=a>>1; } res=min(ans,res); } }}int main(){ int t; cin>>t; while(t--) { memset(b,false,sizeof(b)); memset(vis,0,sizeof(vis)); int i,j,c; cin>>p>>n; for(i=0;i<n;i++) { for(j=0;j<p;j++) { scanf("%d",&c); if(c) vis[i]+=(c<<j); } } res=p; solve(); cout<<res<<endl; }}
相关文章推荐
- UVA - 11205 The broken pedometer (子集生成)
- uva 11205 The broken pedometer (暴力)
- UVA 11205 The broken pedometer ------二进制枚举
- UVA - 1252 Twenty Questions 记忆化搜索 状态压缩
- UVA 11205 The broken pedometer 果断还是暴力吧。。。
- UVa 1533 - Moving Pegs (隐式图搜索 + 状态压缩)
- uva658 - It's not a Bug, it's a Feature! 状态压缩+隐式图搜索+优先队列的dijkstra
- UVA 11205 - The broken pedometer
- uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)
- uva1508-Equipment 状态压缩 记忆化搜索
- UVA-1604 Cubic Eight-Puzzle (双向BFS+状态压缩+限制搜索层数)
- uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)
- UVA - 10029 Edit Step Ladders 状态压缩和记忆化搜索
- uva 11205 The broken pedometer 枚举子集
- uva 10817 状态压缩DP
- 【九度OJ】题目1204:农夫、羊、菜和狼的故事 -----------状态压缩+搜索
- UVA 1252 - Twenty Questions(状态压缩DP+记忆化搜索)
- uva1076 - Password Suspects AC自动机+状态压缩DP
- uva 10603 状态压缩& 最短路
- uva 10817 - Headmaster's Headache ( 状态压缩dp)