蒟蒻随机化乱搞过了,建议加强Qwq
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[24][24];
int st[24],b[24];
int cnt,ans,res,js;
void check(int x)
{
for(int i=1;i<=n;i++)
{
ans=0;
if(i==x)
continue ;
for(int j=1;j<=m;j++)
if(a[x][j]!=a[i][j])
ans=1;
if(!ans)
{
cout<<-1<<' ';
return ;
}
}
cnt=0;
ans=114514;
while(cnt<=1000)//对于每个人只跑了1000次就得到了最佳答案,但一共有 $ 2^20 $ 种可能
{
memset(st,0,sizeof st);
st[0]=1;
random_shuffle(b,b+m);
cnt++;
res=0;
js=n-1;
st[x]=1;
// cout<<999;
int y=0;
while(js)
{
while(st[y]!=0)
y=rand()%n+1;//,cout<<y<<"] ";
//st[y]=1;js--;
//cout<<y<<' '<<js<<"; ";
for(int i=1;i<=n;i++)
if(a[i][b[res]]!=a[x][b[res]]&&st[i]==0)
js--,st[i]=1;//cout<<i<<' ';
res++;
}
ans=min(ans,res);
//cout<<"|"<<ans<<"|\n";
}
cout<<ans<<' ';
}
int Main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>a[i][j];
for(int i=0;i<m;i++)
b[i]=i+1;
if(n==1)
cout<<0;
else
for(int i=1;i<=n;i++)
check(i);
return 0;
}