正解是 2,我的是 3。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
namespace Mashiro {
char buf[1<<18],*p1=buf,*p2=buf;
inline int getc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline void read(T& x) {
x=0;int f=1;
char ch=getc();
while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
x*=f;
}
template <typename T,typename... Args> inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
template<typename T>inline void write(T x) {
static int buf[40],top=0;
if(x<0)putc('-'),x=~x+1;
while(x)buf[++top]=x%10,x/=10;
if(top==0)buf[++top]=0;
while (top) putc(buf[top--]^48);
putc(' ');
flush();
}
template <typename T,typename... Args> inline void write(T x, Args... args) {
write(x);
write(args...);
}
}
using namespace Mashiro;
const int maxst=(1<<10)+2;
int dp[maxst];
int nxt[105][maxst],vis[maxst];
int a[105][12];
int n,m;
int main() {
read(n,m);
for(int i(1);i<=m;++i){
for(int j(1);j<=n;++j){
read(a[i][j]);
}
}
int St=(1<<n)-1;
for(int i(0);i<=St;++i){
for(int j(1);j<=m;++j){
int temp=i;
for(int k(0);k<n;++k){
if(a[j][k+1]==1&&(temp&(1<<k)))temp^=(1<<k);
else if(a[j][k+1]==-1&&!(temp&(1<<k)))temp^=(1<<k);
}
nxt[j][i]=temp;
//cout<<(bitset<4>)i<<" "<<(bitset<4>)temp<<" "<<j;
//cout<<endl;
}
}
memset(dp,0x3f,sizeof dp);
dp[St]=0;
queue<int>q;
q.push(St);
while(!q.empty()){
int u=q.front();
q.pop();
if(u==0)break;
for(int i(1);i<=m;++i){
if(vis[nxt[i][u]])continue;
dp[nxt[i][u]]=dp[u]+1;
q.push(nxt[i][u]);
vis[nxt[i][u]]=1;
}
}
if(!vis[0])write(-1);
else write(dp[0]);
#ifndef ONLINE_JUDGE
cout<<endl;
system("pause");
#endif
return 0;
}