灯泡3

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;
}

posted @ 2024-03-19 08:56  Jeanny  阅读(1)  评论(0编辑  收藏  举报