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)/变为棋盘 [transform-to-chessboard].html
2022-03-29 12:43:11 +08:00

51 lines
1.9 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>n x n</code>&nbsp;的二维网络&nbsp;<code>board</code>&nbsp;仅由&nbsp;<code>0</code>&nbsp;&nbsp;<code>1</code>&nbsp;组成&nbsp;。每次移动,你能任意交换两列或是两行的位置。</p>
<p>返回 <em>将这个矩阵变为<strong>&nbsp; “棋盘”&nbsp;&nbsp;</strong>所需的最小移动次数&nbsp;</em>。如果不存在可行的变换,输出 <code>-1</code></p>
<p><strong>“棋盘”</strong> 是指任意一格的上下左右四个方向的值均与本身不同的矩阵。</p>
<p>&nbsp;</p>
<p><strong>示例 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/29/chessboard1-grid.jpg" style="height: 145px; width: 500px;" /></p>
<pre>
<strong>输入:</strong> board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
<strong>输出:</strong> 2
<strong>解释:</strong>一种可行的变换方式如下,从左到右:
第一次移动交换了第一列和第二列。
第二次移动交换了第二行和第三行。
</pre>
<p><strong>示例 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/29/chessboard2-grid.jpg" /></p>
<pre>
<strong>输入:</strong> board = [[0, 1], [1, 0]]
<strong>输出:</strong> 0
<strong>解释: </strong>注意左上角的格值为0时也是合法的棋盘也是合法的棋盘.
</pre>
<p><strong>示例 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/29/chessboard3-grid.jpg" /></p>
<pre>
<strong>输入:</strong> board = [[1, 0], [1, 0]]
<strong>输出:</strong> -1
<strong>解释: </strong>任意的变换都不能使这个输入变为合法的棋盘。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == board.length</code></li>
<li><code>n == board[i].length</code></li>
<li><code>2 &lt;= n &lt;= 30</code></li>
<li><code>board[i][j]</code>&nbsp;将只包含&nbsp;<code>0</code>&nbsp;<code>1</code></li>
</ul>