题意 :从矩阵中选出最少的列,使得选出来的矩阵可以区别开
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int hang,lie;
int map[110][20];
int vis[20];
int ans;
bool found;
void check()
{
for(int i = 0 ; i < hang-1 ; i++ )//枚举每种情况
{
for(int j = i+1 ; j < hang ; j++)
{
found = false ;
for(int k = 0 ; k < lie ; k++)
if(vis[k] && map[i][k] != map[j][k]) //有选到,并且有不同点
found = true ;
if(!found)
return ;
}
}
int num = 0 ;
for(int i = 0 ; i < lie ; i++)
if(vis[i])
num++;
if(num < ans)
ans = num ;
}
void solve(int n)
{
if(n == lie)
check();
else
{
vis[n] = 1; //选
solve(n+1);
vis[n] = 0 ; //不选
solve(n+1);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&lie,&hang);
for(int i = 0 ; i < hang ; i++)
for(int j = 0 ; j < lie ; j++)
scanf("%d",&map[i][j]);
memset(vis,0,sizeof(vis));
ans = lie ;
solve(0);
printf("%d\n",ans);
}
return 0;
}