24h購物| | PChome| 登入
2013-06-05 10:26:25| 人氣1,149| 回應0 | 上一篇 | 下一篇

[UVA][窮舉] 11205 - The broken pedometer

推薦 0 收藏 0 轉貼0 訂閱站台

 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 燈顯示,LED 燈的個數可能會有所不同,即不一定是七段顯示器,接著有些燈可能會壞,而造成辨識模稜兩可。問最少用 LED 的個數不影響給定的字符辨識。

題目解法:

基本上窮舉所有可能 LED 可用可不用的情況,因此是 O(2^15) 為上限,接著在每個字符的 LED 表示法壓縮成二進制,那麼就可以直接使用 AND 運算。
如果進行 AND 運算都不重複的話,即可以做為一組解,然後解析有多少的 bit = 1,找最少個數即可。

#include <stdio.h>

int main() {
int testcase;
int n, m, i, j, k;
int x, v;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &n, &m);
int A[105];
for(i = 0; i < m; i++) {
v = 0;
for(j = 0; j < n; j++) {
scanf("%d", &x);
v |= (1&x)<<j;
}
A[i] = v;
}
int ret = 0xffff, rr = 1<<n;
int mark[32768] = {};
for(i = 0; i < rr; i++) {
int flag = 0;
x = 0;
for(j = 0; j < n; j++)
x += (i>>j)&1;
if(x >= ret) continue;
for(j = 0; j < m && !flag; j++) {
v = A[j]&i;
if(mark[v]) {
while(j >= 0) {
v = A[j]&i;
mark[v] = 0;
j--;
}
flag = 1;
break;
}
mark[v] = 1;
}
if(flag == 0) {
x = 0;
for(j = 0; j < n; j++)
x += (i>>j)&1;
if(x < ret)
ret = x;
for(j = 0; j < m; j++) {
v = A[j]&i;
mark[v] = 0;
}
}
}
printf("%d\n", ret);
}
return 0;
}
 

台長: Morris
人氣(1,149) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][窮舉] 12249 - Overlapping Scenes
此分類上一篇:[UVA][dp、大數] 10328 - Coin Toss

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文