1091:二进制矩阵中的最短路径

This commit is contained in:
huangge1199@hotmail.com 2021-08-19 23:03:17 +08:00
parent a14d0ae471
commit b4860bc108
2 changed files with 165 additions and 0 deletions

View 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)
}

View File

@ -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>