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
, 。
Xor , ...... 100, 。
+ .p 2^P-1 ,1,10,101,... 1-2^p-1 ,
1 LED , 100 , 1 , 1 1 。 , 。
#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{int num,min,t,f,i,j,k,p,n,m,a[101],b[101],exp[16],visit[65536];
exp[0]=1;
for (i=1;i<16;i++)
exp[i]=exp[i-1]*2;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&p,&n);
for (i=1;i<=n;i++)
{a[i]=0;
for (j=p-1;j>=0;j--)
{scanf("%d",&k);
a[i]+=exp[j]*k;
}
}
min=15; memset(visit,0,sizeof(visit));
for (i=1;i<=pow(2,p);i++)
{f=1; num=n;
for (j=1;j<=n;j++)
{m=a[j]&i;
b[j]=m;
if (visit[m]==1) {f=0;num=j; break;}
else visit[m]=1;
}
if (f)
{f=i; k=0;
while (f) {k+=f%2; f=f/2;}
if (k<min) min=k;
}
for (j=1;j<=num;j++) // 100 1, memset 1.67s 0.024s
visit[b[j]]=0;
}
printf("%d
",min);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
java에서 InputStream, String, File 간의 상호 전환 비교InputStream, String, File 상호 전환 1. String --> InputStream 2. InputStream --> String 오늘 인터넷에서 또 다른 방법을 보았는데, 특별히 가지고 와서 공...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.