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 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
对不起,题目我是没用太明白,我是直接看别人的解题才明白大概意思的,就是找出能够表示所有数字而且用的bit位数最少的结果,比如上面的两个测试用例,第一个用例用前面五位便能够表示十个数字,
第二个用例用前面4位变能表示十个数字(很明显后面两列都是0,一眼就能看出来),ps:不懂英文真可怕:(,代码别人的,注释我加上去的
#include <cstring>
 #include <cstdlib>
 #include <cstdio>
 #include <map>
 using namespace std;
 
 int rec[105];
 
 int main()
 {
     int T;
     scanf( "%d", &T );
     while( T-- )
     {
         int P, N;
         map<int,bool>mp;
         memset( rec, 0, sizeof( rec ) );
         scanf( "%d %d", &P, &N );
         for( int i = 0; i < N; ++i )
         {
             for( int j = 0; j < P; ++j )
             {
                 int c;
                 scanf( "%d", &c );
                 if( c )
                     rec[i] += ( c << j );
             }
         }
      /*使用多少个位,bound给出上限*/ int bound = ( 1 << P ) - 1, flag, min = 20, bit; for( int i = 0; i <= bound; ++i ) { mp.clear(); for( int j = 0; j < N; ++j ) { int t = i & rec[j]; if( mp.count( t ) )//是否与前面表示的数字重复了,重复了说明位数的组合或者位数的个数不符合要求 { break; } else { flag = j; mp[t] = true; } } if( flag == N - 1 ) { bit = i; int ans = 0; while( bit > 0 )//理解这里就好办了,计算i中bit是1的个数,也就是要使用多少位 { if( bit & 1 ) ans++; bit >>= 1; } min = min < ans ? min : ans; } } printf( "%d\n", min ); } return 0; }

 

posted @ 2012-12-01 22:48  godjob  Views(424)  Comments(0Edit  收藏  举报