首页 > 其他分享 >Disconnect Path in a Binary Matrix by at Most One Flip

Disconnect Path in a Binary Matrix by at Most One Flip

时间:2023-02-05 18:33:13浏览次数:62  
标签:Binary Disconnect Matrix text dfs grid return 轮廓 true

Disconnect Path in a Binary Matrix by at Most One Flip

You are given a 0-indexed $m \times n$ binary matrix grid . You can move from a cell (row, col) to any of the cells (row + 1, col) or (row, col + 1) that has the value $1$. The matrix is disconnected if there is no path from (0, 0) to (m - 1, n - 1) .

You can flip the value of at most one (possibly none) cell. You cannot flip the cells (0, 0) and (m - 1, n - 1) .

Return true if it is possible to make the matrix disconnect or false otherwise.

Note that flipping a cell changes its value from $0$ to $1$ or from $1$ to $0$.

Example 1:

Input: grid = [[1,1,1],[1,0,0],[1,1,1]]
Output: true
Explanation: We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid.

Example 2:

Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: false
Explanation: It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2).

 

Constraints:

  • $m \ \mathrm{==} \ \text{grid.length}$
  • $n \ \mathrm{==} \ \text{grid}[i]\text{.length}$
  • $1 \leq m, n \leq 1000$
  • $1 \leq m \times n \leq {10}^5$
  • $\text{grid}[i][j]$ is either $0$ or $1$.
  • $\text{grid}[0][0] \ \mathrm{==} \ \text{grid}[m - 1][n - 1] \ \mathrm{==} \ 1$

 

解题思路

  考虑从$(0,0)$到$(n-1,m-1)$的所有路径所构成的轮廓。如下图(无法使得图不连通的情况),彩色代表该格子可以走,黑色的部分是轮廓。

  如果通过修改一个格子使得图不连通,那么本质就是存在唯一一个格子使得除去这个格子后整个轮廓变得不连通。因此可以先dfs找出轮廓的其中一个部分。注意,在dfs的过程中要保证向下和向右搜索的顺序是不变的,例如每次先向下搜再向左搜,这样才能保证搜出的路径是轮廓的一部分。然后第一次dfs搜出的这条路径全部改成$0$(当然如果不存在路径那么图本身就不连通了),再dfs(一样是搜索轮廓,搜的是另外一部分轮廓)一次。如果第二次dfs依然存在$(0,0)$到$(n-1,m-1)$的路径,说明无法通过修改一个格子使得轮廓不连通(参考上图)。

  AC代码如下:

class Solution {
public:
    bool isPossibleToCutPath(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> vis(n, vector<bool>(m));
        function<bool(int, int)> dfs = [&](int x, int y) {
            if (x >= n || y >= m || !grid[x][y] || vis[x][y]) return false;
            if (x == n - 1 && y == m - 1) return true;
            vis[x][y] = true;
            grid[x][y] = 0;
            if (dfs(x + 1, y) || dfs(x, y + 1)) return true;
            grid[x][y] = 1;
            return false;
        };
        if (!dfs(0, 0)) return true;
        grid[0][0] = grid[n - 1][m - 1] = 1;
        vis = vector<vector<bool>>(n, vector<bool>(m));
        return !dfs(0, 0);
    }
};

 

参考资料

  c++ 30行超简单两次dfs:https://leetcode.cn/problems/disconnect-path-in-a-binary-matrix-by-at-most-one-flip/solution/c-32xing-chao-jian-dan-liang-ci-dfs-by-x-amcj/

标签:Binary,Disconnect,Matrix,text,dfs,grid,return,轮廓,true
From: https://www.cnblogs.com/onlyblues/p/17093753.html

相关文章

  • Educational Codeforces Round 141:B. Matrix of Differences
    一、来源:Problem-B-Codeforces二、题面三、思路我们先从一维思考如何构造尽可能多的数值差。以n=2为例,此时有1,2,3,4数,其中构成差值为3的方案有一个1,4,构成差值......
  • matrix-tree 的一个推论
    本文作者的线性代数水平还很低,如果有什么更简单的方法请告诉TA。结论:对于一张无向图\(G\),设其Laplace矩阵为\(A\),而\(A\)的特征值分别为\(\lambda_1,\lambda_2,\l......
  • 104. Maximum Depth of Binary Tree[Easy]
    217.ContainsDuplicateGiventherootofabinarytree,returnitsmaximumdepth.Abinarytree'smaximumdepthisthenumberofnodesalongthelongestpath......
  • Codeforces 1316 D. Nash Matrix(dfs)
    题意:给出一个的棋盘和每个棋盘位置最后能走到的位置,如果一直走不停下来就是,可以停下来就是走到的最后位置,让你输出每个位置的操作字符,上下左右和,停在此位置。我们先找......
  • Codeforces Round #596 C. p-binary
    给定N和p,让你找到满足2^x+p最少有多少不同的项。就把N转成二进制然后枚举P的个数就是答案,昨天特判没写好,今天早上起来发现被卡掉了。rank又出1000了。AC代码:#include<......
  • [LeetCode] 1145. Binary Tree Coloring Game
    Twoplayersplayaturnbasedgameonabinarytree.Wearegiventhe root ofthisbinarytree,andthenumberofnodes n inthetree. n isodd,andeach......
  • Luogu5435 基于值域预处理的快速 GCD & Leetcode2543 - binary GCD -
    题目链接:https://www.luogu.com.cn/problem/P5435请忽略题目名称学到一个科技:binaryGCD,能够快速求出两个数GCD(从这道题来看已经接近\(O(1)\)了)代码://bySkyRainW......
  • [LeetCode]Spiral Matrix
    QuestionGivenamatrixofmxnelements(mrows,ncolumns),returnallelementsofthematrixinspiralorder.Forexample,Giventhefollowingmatrix:[[1......
  • vulnhub_matrix-breakout-2-morpheus
    前言靶机地址:matrix-breakout-2-morpheus攻击机:kali2022.3靶机:matrix-breakout-2-morpheus题目描述:这是《黑客帝国突围》系列的第二部,副标题为墨菲斯:1。它的主题是对......
  • LeetCode dynamic binary tree generator component All In One
    LeetCodedynamicbinarytreegeneratorcomponentAllInOnedemosgif//破解,钻牛角尖https://leetcode.com/problems/maximum-depth-of-binary-tree/descrip......