过了 Hack 但没过 #8 十分奇妙,希望各位奆佬能帮助一下我这个蒟蒻。
#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int N = 10;
vector <int> e[1 << N];
int n,m,a[N],dis[1 << N];
void bfs(int s){
queue <int> q;
q.push(s);
memset(dis , 0x3f , sizeof dis);
dis[s] = 0;
while (!q.empty()){
int x = q.front();
q.pop();
for (int y : e[x]){
if (dis[y] > dis[x] + 1) {
dis[y] = dis[x] + 1;
q.push(y);
}
}
}
}
int main(){
cin >> n >> m;
while (m--){
for (int i = 0;i < n;i++) cin >> a[i];
for (int i = 0;i < (1 << n);i++){
int t = i;
for (int j = 0;j < n;j++){
if (a[j] == 1 && (t >> j) != 0) t ^= (1 << j);
if (a[j] == -1) t |= (1 << j);
}
e[i].push_back(t);
}
}
bfs((1 << n) - 1);
if (dis[0] < 1e9) cout << dis[0];
else cout << -1;
return 0;
}