二进制矩阵全零转换问题 | DFS

问题描述

在一个古老的实验室里,两个研究员,小星和小月,获得了一个 m x n 的电路图,表示为二进制矩阵 grid。在这个矩阵中,他们可以对任意一个电路单元进行翻转操作。翻转操作会将所选单元的状态从 0 改为 1,或从 1 改为 0,同时影响与其相邻的上下左右单元。

小星和小月希望通过最少的翻转次数,将整个电路图变成全 0 的状态。如果这个目标无法实现,则返回 -1


测试样例

样例1:

输入:grid = [[0, 1], [1, 0]]

输出:2

样例2:

输入:grid = [[1, 0], [1, 1]]

输出:1

样例3:

输入:grid = [[0]]

输出:0

题解:

很经典的一道题,原题叫棋盘覆盖问题,关键在于一个位置如果翻转两次等于没有翻转

所以直接DFS遍历每个格子,每次都有翻和不翻两个情况,最后判断翻转次数即可。

有一种更方便的方法,但是在时间上似乎不那么占优,先通过DFS生成随机1,0的长度为m*n的字符串,然后根据字符串的10,如果是1就翻转,如果是0就不翻,也不用每一位去异或,直接在对应位置+1代表翻转次数,最后遍历,如果是偶数就是0,奇数就是1即可。

代码:

cpp 复制代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<map>
#include<set>
using namespace std;
typedef long long int ll;

vector<vector<int>> grid1;
vector<vector<int>> vt;


int mincnt=100000;
int xt[5]={1,0,-1,0,0};
int yt[5]={0,1,0,-1,0};

int check(int x,int y){
    int n=vt.size();
    int m=vt[0].size();
    if(x>=0 && x<n && y>=0 && y<m){
        return 1;
    }
    return 0;
}

void f(int x,int y){
    for(int i=0;i<5;i++){
        int xx=x+xt[i];
        int yy=y+yt[i];
        if(check(xx,yy)){
            vt[xx][yy]++;
        }

    }
}

void col(string s){
    //cout << "YES" << "\n";
    //cout << s << "\n";

    int cnt=0;
    for(int i=0;i<vt.size();i++){
        for(int j=0;j<vt[i].size();j++){
            if(s[cnt]=='1'){
                //cout << "In" <<"\n";
                f(i,j);
            }
            cnt++;
        }
    }
    //cout << "pa1" <<"\n";
    /*
    for(int i=0;i<vt.size();i++){
        for(int j=0;j<vt[i].size();j++){
            cout << vt[i][j] << " ";
        }
        cout << "\n";
    }
    */
    bool is=true;
    for(int i=0;i<vt.size();i++){
        for(int j=0;j<vt[i].size();j++){
            if(vt[i][j]%2!=0){
                is=false;
                break;
            }
        }
    }
    //cout << "pa2" << "\n";
    if(is){
        int ct=0;
        for(int i=0;i<s.size();i++){
            if(s[i]=='1'){
                ct++;
            }
        }
        if(ct<mincnt){
            mincnt=ct;
        }
    }
    //cout << "pa3" << "\n";
}

void dfs(string s,int num,int t){
    if(num==t){
        vt=grid1;
        col(s);
        return;
    }
    string a=s;
    a=a+'1';
    dfs(a,num+1,t);
    a=s;
    a=a+'0';
    dfs(a,num+1,t);
}

int solution(std::vector<std::vector<int>> grid) {
    vt=grid;
    grid1=grid;
    string s="";
    mincnt=100000;
    int x=grid.size();
    int y=grid[0].size();
    dfs(s,0,x*y);
    if(mincnt==100000){
        return -1;
    }
    return mincnt;
}

int main() {
    cout << (solution({{1, 0, 1}, {0, 1, 0}, {1, 0, 1}})) << endl;
    cout << (solution({{0, 1}, {1, 0}}) == 2) << endl;
    cout << (solution({{1, 0}, {1, 1}}) == 1) << endl;
    cout << (solution({{0, 0}, {0, 1}}) == 3) << endl;
    //cout << (solution({{0}}) == 0) << endl;
    return 0;
}
相关推荐
哈哈很哈哈2 分钟前
逻辑回归Logistic Regression
算法·机器学习·逻辑回归
甄心爱学习8 分钟前
【极大似然估计/最大化后验】为什么逻辑回归要使用交叉熵损失函数
算法·机器学习·逻辑回归
郝学胜-神的一滴22 分钟前
深度学习入门全解析:从核心概念到实战基础 | 技术研讨会精华总结
人工智能·python·深度学习·算法·cnn
梯度下降中32 分钟前
CNN原理精讲
人工智能·算法·机器学习
Ivanqhz35 分钟前
活跃范围重写(Live Range Rewriting)
开发语言·c++·后端·算法·rust
xiaoye-duck40 分钟前
《算法题讲解指南:优选算法-链表》--51.两数相加,52.两两交换链表中的节点
数据结构·算法·链表
Cosolar44 分钟前
阿里CoPaw进阶使用手册:从新手到高手的完整指南
人工智能·后端·算法
松小白song1 小时前
机器人路径规划算法之Dijkstra算法详解+MATLAB代码实现
前端·javascript·算法
qq_263_tohua1 小时前
第107期 刷算法题
算法
2501_940315261 小时前
98验证二叉搜索树
java·数据结构·算法