題目描述
現有n盞燈,以及m個按鈕。每個按鈕可以同時控制這n盞燈——按下了第i個按鈕,對於所有的燈都有一個效果。按下i按鈕對於第j盞燈,是下面3中效果之一:如果a[i][j]爲1,那麼當這盞燈開了的時候,把它關上,否則不管;如果爲-1的話,如果這盞燈是關的,那麼把它打開,否則也不管;如果是0,無論這燈是否開,都不管。
現在這些燈都是開的,給出所有開關對所有燈的控制效果,求問最少要按幾下按鈕才能全部關掉。
輸入格式
前兩行兩個數,n m
接下來m行,每行n個數,a[i][j]表示第i個開關對第j個燈的效果。
輸出格式
一個整數,表示最少按按鈕次數。如果沒有任何辦法使其全部關閉,輸出-1
輸入輸出樣例
輸入 #1
3
2
1 0 1
-1 1 0
輸出 #1
2
說明/提示
對於20%數據,輸出無解可以得分。
對於20%數據,n<=5
對於20%數據,m<=20
上面的數據點可能會重疊。
對於100%數據 n<=10,m<=100
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e3 + 10;
int a[maxn][maxn];
bool vis[maxn];
int n, m;
struct node{
int dp, step;
};
void bfs() {
queue<node>que;
node cur;
cur.dp = (1 << n) - 1;
cur.step = 0;
que.push(cur);
vis[cur.dp] = true;
while(!que.empty()) {
cur = que.front();
que.pop();
node tem = cur;
for(int i = 1; i <= m; i++) {
tem = cur;
int now = tem.dp;
for(int j = 1; j <= n; j++) {
if(a[i][j] == 1) {
if( (1 << (j - 1)) & now)
now = (1 << (j - 1)) ^ now;//,cout << now << endl;
}
else if(a[i][j] == -1)
now = (1 << (j - 1)) | now;
}
if(vis[now] == true)
continue;
vis[now] = true;
tem.dp = now;
tem.step++;
if(now == 0) {
cout << tem.step << endl;
return;
}
que.push(tem);
}
}
}
int main() {
cin >> n >> m;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
bfs();
if(vis[0] == 0)
cout << -1 << endl;
return 0;
}