1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/转化为全零矩阵的最少反转次数 [minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix].html
2022-03-29 12:43:11 +08:00

48 lines
1.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个&nbsp;<code>m x n</code>&nbsp;的二进制矩阵&nbsp;<code>mat</code>。每一步,你可以选择一个单元格并将它反转(反转表示 <code>0</code><code>1</code> <code>1</code><code>0</code> )。如果存在和它相邻的单元格,那么这些相邻的单元格也会被反转。相邻的两个单元格共享同一条边。</p>
<p>请你返回将矩阵&nbsp;<code>mat</code> 转化为全零矩阵的<em>最少反转次数</em>,如果无法转化为全零矩阵,请返回&nbsp;<code>-1</code>&nbsp;</p>
<p><strong>二进制矩阵</strong>&nbsp;的每一个格子要么是 <code>0</code> 要么是 <code>1</code></p>
<p><strong>全零矩阵</strong>&nbsp;是所有格子都为 <code>0</code> 的矩阵。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1</strong></p>
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/12/13/matrix.png" /></p>
<pre>
<strong>输入:</strong>mat = [[0,0],[0,1]]
<strong>输出:</strong>3
<strong>解释:</strong>一个可能的解是反转 (1, 0),然后 (0, 1) ,最后是 (1, 1) 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>mat = [[0]]
<strong>输出:</strong>0
<strong>解释:</strong>给出的矩阵是全零矩阵,所以你不需要改变它。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>mat = [[1,0,0],[1,0,0]]
<strong>输出:</strong>-1
<strong>解释:</strong>该矩阵无法转变成全零矩阵
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m ==&nbsp;mat.length</code></li>
<li><code>n ==&nbsp;mat[0].length</code></li>
<li><code>1 &lt;= m&nbsp;&lt;= 3</code></li>
<li><code>1 &lt;= n&nbsp;&lt;= 3</code></li>
<li><code>mat[i][j]</code>&nbsp;是 0 或 1 。</li>
</ul>