1091:二进制矩阵中的最短路径
This commit is contained in:
parent
a14d0ae471
commit
b4860bc108
120
src/main/java/leetcode/editor/cn/ShortestPathInBinaryMatrix.java
Normal file
120
src/main/java/leetcode/editor/cn/ShortestPathInBinaryMatrix.java
Normal file
@ -0,0 +1,120 @@
|
||||
//给你一个 n x n 的二进制矩阵 grid 中,返回矩阵中最短 畅通路径 的长度。如果不存在这样的路径,返回 -1 。
|
||||
//
|
||||
// 二进制矩阵中的 畅通路径 是一条从 左上角 单元格(即,(0, 0))到 右下角 单元格(即,(n - 1, n - 1))的路径,该路径同时满足下述要求
|
||||
//:
|
||||
//
|
||||
//
|
||||
// 路径途经的所有单元格都的值都是 0 。
|
||||
// 路径中所有相邻的单元格应当在 8 个方向之一 上连通(即,相邻两单元之间彼此不同且共享一条边或者一个角)。
|
||||
//
|
||||
//
|
||||
// 畅通路径的长度 是该路径途经的单元格总数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:grid = [[0,1],[1,0]]
|
||||
//输出:2
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:grid = [[0,0,0],[1,1,0],[1,1,0]]
|
||||
//输出:4
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:grid = [[1,0,0],[1,1,0],[1,1,0]]
|
||||
//输出:-1
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// n == grid.length
|
||||
// n == grid[i].length
|
||||
// 1 <= n <= 100
|
||||
// grid[i][j] 为 0 或 1
|
||||
//
|
||||
// Related Topics 广度优先搜索 数组 矩阵
|
||||
// 👍 116 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
//1091:二进制矩阵中的最短路径
|
||||
class ShortestPathInBinaryMatrix {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ShortestPathInBinaryMatrix().new Solution();
|
||||
// TwoArray twoArray = new TwoArray("[[0,1],[1,0]]", true);
|
||||
// TwoArray twoArray = new TwoArray("[[0,0,0],[1,1,0],[1,1,0]]",true);
|
||||
TwoArray twoArray = new TwoArray("[[1,0,0],[1,1,0],[1,1,0]]", true);
|
||||
System.out.println(solution.shortestPathBinaryMatrix(twoArray.getArr()));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int shortestPathBinaryMatrix(int[][] grid) {
|
||||
if (grid[grid.length - 1][grid[0].length - 1] == 1 || grid[0][0] == 1) {
|
||||
return -1;
|
||||
}
|
||||
if (grid.length == 1) {
|
||||
return 1;
|
||||
}
|
||||
boolean[][] use = new boolean[grid.length][grid[0].length];
|
||||
List<Pair<Integer, Integer>> list = new ArrayList<>();
|
||||
list.add(new Pair<>(-1, -1));
|
||||
list.add(new Pair<>(-1, 0));
|
||||
list.add(new Pair<>(-1, 1));
|
||||
list.add(new Pair<>(0, -1));
|
||||
list.add(new Pair<>(0, 1));
|
||||
list.add(new Pair<>(1, -1));
|
||||
list.add(new Pair<>(1, 0));
|
||||
list.add(new Pair<>(1, 1));
|
||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||
queue.add(new Pair<>(0, 0));
|
||||
use[0][0] = true;
|
||||
int count = 1;
|
||||
while (!queue.isEmpty()) {
|
||||
count++;
|
||||
int size = queue.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Pair<Integer, Integer> pair = queue.poll();
|
||||
for (Pair<Integer, Integer> p : list) {
|
||||
int x = pair.getKey() + p.getKey();
|
||||
int y = pair.getValue() + p.getValue();
|
||||
if (x == grid.length - 1 && y == grid[0].length - 1) {
|
||||
return count;
|
||||
}
|
||||
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || use[x][y]) {
|
||||
continue;
|
||||
}
|
||||
if (grid[x][y] == 0) {
|
||||
queue.add(new Pair<>(x, y));
|
||||
}
|
||||
use[x][y] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<p>给你一个 <code>n x n</code> 的二进制矩阵 <code>grid</code> 中,返回矩阵中最短 <strong>畅通路径</strong> 的长度。如果不存在这样的路径,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p>二进制矩阵中的 畅通路径 是一条从 <strong>左上角</strong> 单元格(即,<code>(0, 0)</code>)到 右下角 单元格(即,<code>(n - 1, n - 1)</code>)的路径,该路径同时满足下述要求:</p>
|
||||
|
||||
<ul>
|
||||
<li>路径途经的所有单元格都的值都是 <code>0</code> 。</li>
|
||||
<li>路径中所有相邻的单元格应当在 <strong>8 个方向之一</strong> 上连通(即,相邻两单元之间彼此不同且共享一条边或者一个角)。</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>畅通路径的长度</strong> 是该路径途经的单元格总数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,1],[1,0]]
|
||||
<strong>输出:</strong>2
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[0,0,0],[1,1,0],[1,1,0]]
|
||||
<strong>输出:</strong>4
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,0,0],[1,1,0],[1,1,0]]
|
||||
<strong>输出:</strong>-1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>grid[i][j]</code> 为 <code>0</code> 或 <code>1</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>广度优先搜索</li><li>数组</li><li>矩阵</li></div></div>\n<div><li>👍 116</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user