题:
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:
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.
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.
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:
思路:
记录哪些灯亮 ,遍历到第几个灯,将相应LED失效 ,如果能全部区分,再灭下一轮 ,否则返回当前灭灯数 。
Debug:
1.memset函数按字节对内存块进行初始化,所以不能用它将int数组初始化为0和-1之外的其他值(除非该值高字节和低字节相同)
2.循环中用的变量,不能为了存储结果随意修改
代码:
/*
2
3
1
1 0 1
5
2
1 1 1 1 1
1 1 1 1 0
*/
#include<stdio.h>
#include<string.h>
//#define LOCAL
int N,p,n;//问题数,LED数量 ,符号数
int cod[103][20];//符号编码
void draw(int cod[]){
for(int j=0;j<p;j++){
if(j==0||j==3||j==6){
if(cod[j])printf(" 一");
printf("\n");
}else if(j==1||j==4){
if(cod[j])printf("|");
else printf(" ");
printf(" ");
}else if(j==2||j==5){
if(cod[j])printf("|");
printf("\n");
}
}
}
void drawAct(int cod[20]){
for(int j=0;j<p;j++){
if(j==0||j==3||j==6){
if(cod[j])printf(" 一");
printf("\n");
}else if(j==1||j==4){
if(cod[j])printf("|");
else printf(" ");
printf(" ");
}else if(j==2||j==5){
if(cod[j])printf("|");
printf("\n");
}
}
printf("\n");
}
int search(int a[],int cnt,int spr){
//判断在LED范围
// printf("begin-->cnt:%d p-1:%d\n",cnt,p-1);
if(cnt>p-1){
// printf("return cnt:%d\n",cnt);
return spr;
}
int act[20];
int temp[103][20];
int max_spr;
memcpy(act,a,sizeof(act)) ;
memcpy(temp,cod,sizeof(cod));
act[cnt++]=0;
// drawAct(act);
//将字符相应LED失效
for(int i=0;i<p;i++) {
if(!act[i]){
for(int j=0;j<n;j++){
temp[j][i]=0;
}
}
}
//如果能全部辨别,再灭下一轮 ,否则返回当前数字
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(memcmp(temp[i],temp[j],sizeof(temp[j]))==0){
// printf("cnt:%d act:%d=7-%d\n",cnt-1,p-spr,spr);
// drawAct(act);
// printf("%d %d:\n",i,j);
// draw(temp[i]);
// draw(temp[j]);
return spr;
}
}
}
spr++;
max_spr=spr;
for(int i=cnt;i<p;i++){
int temp_spr;
// printf("search:%d spr:%d\n",i,spr) ;
temp_spr=search(act,i,spr);//不能为了找最大spr改动固定参数
if(temp_spr>max_spr){
max_spr=temp_spr;
}
}
spr=max_spr;
//返回灭灯个数
return spr;
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
scanf("%d",&N) ;
while(N--){
scanf("%d",&p);
scanf("%d",&n);
memset(cod,0,sizeof(cod));
for(int i=0;i<n;i++){
for(int j=0;j<p;j++){
scanf("%d",&cod[i][j]);
}
}
int act[20];//记录哪些灯亮
for(int i=0;i<p;i++)act[i]=1;
int spr=0;//灭灯数
for(int i=0;i<p;i++){
int temp_spr;
temp_spr=search(act,i,0);
if(temp_spr>spr)spr=temp_spr;
}
// printf("%d-%d=\n",p,spr);
printf("%d\n",p-spr);
}
return 0;
}