n*n(n<1000)的棋盤有一些亮著的燈泡,和滅著的燈泡,按行或者列可以將行或者列燈泡翻轉。但是沒有花費,按某一個格子也可以使燈泡的狀態翻轉,花費一元,問不超過k元,能否是棋盤的燈泡全亮,k<n。問是否可行及可行方案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
using namespace std;
int res = INT_MAX, ans, n, k, x;
bitset<1005> c[1005], b[1005];
/*
4 3
0 1 1 0
1 0 1 0
0 0 1 1
1 1 1 0
*/
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin>>x;
(b[j] <<= 1) |= x;
(c[j] <<= 1) |= (x^1);
}
}
for(int i = 1; i <= n; i++){
int ans = 0;
for(int j = 1; j < n; j++){
if(j != i){
int t1 = (b[i]^b[j]).count();
int t2 = (c[i]^b[j]).count();
int t = min(t1, t2);
ans += t;
}
}
// cout<<"ans: "<<ans<<endl;
res = min(res, ans);
}
if(res <= k) cout<<"yes"<<endl;
return 0;
}