auto commit
This commit is contained in:
parent
63b4122c46
commit
cac408908f
|
@ -61,6 +61,8 @@
|
||||||
|
|
||||||
## 1. 计算在网格中从原点到特定点的最短路径长度
|
## 1. 计算在网格中从原点到特定点的最短路径长度
|
||||||
|
|
||||||
|
[1091. Shortest Path in Binary Matrix(Medium)](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
|
||||||
|
|
||||||
```html
|
```html
|
||||||
[[1,1,0,1],
|
[[1,1,0,1],
|
||||||
[1,0,1,0],
|
[1,0,1,0],
|
||||||
|
@ -68,12 +70,12 @@
|
||||||
[1,0,1,1]]
|
[1,0,1,1]]
|
||||||
```
|
```
|
||||||
|
|
||||||
题目描述:1 表示可以经过某个位置,求解从 (0, 0) 位置到 (tr, tc) 位置的最短路径长度。
|
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int minPathLength(int[][] grids, int tr, int tc) {
|
public int shortestPathBinaryMatrix(int[][] grids) {
|
||||||
final int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
||||||
final int m = grids.length, n = grids[0].length;
|
int m = grids.length, n = grids[0].length;
|
||||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
queue.add(new Pair<>(0, 0));
|
queue.add(new Pair<>(0, 0));
|
||||||
int pathLength = 0;
|
int pathLength = 0;
|
||||||
|
@ -83,14 +85,14 @@ public int minPathLength(int[][] grids, int tr, int tc) {
|
||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Pair<Integer, Integer> cur = queue.poll();
|
Pair<Integer, Integer> cur = queue.poll();
|
||||||
int cr = cur.getKey(), cc = cur.getValue();
|
int cr = cur.getKey(), cc = cur.getValue();
|
||||||
grids[cr][cc] = 0; // 标记
|
grids[cr][cc] = 1; // 标记
|
||||||
for (int[] d : direction) {
|
for (int[] d : direction) {
|
||||||
int nr = cr + d[0], nc = cc + d[1];
|
int nr = cr + d[0], nc = cc + d[1];
|
||||||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 0) {
|
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (nr == tr && nc == tc) {
|
if (nr == m - 1 && nc == n - 1) {
|
||||||
return pathLength;
|
return pathLength + 1;
|
||||||
}
|
}
|
||||||
queue.add(new Pair<>(nr, nc));
|
queue.add(new Pair<>(nr, nc));
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,6 +61,8 @@
|
||||||
|
|
||||||
## 1. 计算在网格中从原点到特定点的最短路径长度
|
## 1. 计算在网格中从原点到特定点的最短路径长度
|
||||||
|
|
||||||
|
[1091. Shortest Path in Binary Matrix(Medium)](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
|
||||||
|
|
||||||
```html
|
```html
|
||||||
[[1,1,0,1],
|
[[1,1,0,1],
|
||||||
[1,0,1,0],
|
[1,0,1,0],
|
||||||
|
@ -68,12 +70,12 @@
|
||||||
[1,0,1,1]]
|
[1,0,1,1]]
|
||||||
```
|
```
|
||||||
|
|
||||||
题目描述:1 表示可以经过某个位置,求解从 (0, 0) 位置到 (tr, tc) 位置的最短路径长度。
|
题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int minPathLength(int[][] grids, int tr, int tc) {
|
public int shortestPathBinaryMatrix(int[][] grids) {
|
||||||
final int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
|
int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}};
|
||||||
final int m = grids.length, n = grids[0].length;
|
int m = grids.length, n = grids[0].length;
|
||||||
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
|
||||||
queue.add(new Pair<>(0, 0));
|
queue.add(new Pair<>(0, 0));
|
||||||
int pathLength = 0;
|
int pathLength = 0;
|
||||||
|
@ -83,14 +85,14 @@ public int minPathLength(int[][] grids, int tr, int tc) {
|
||||||
while (size-- > 0) {
|
while (size-- > 0) {
|
||||||
Pair<Integer, Integer> cur = queue.poll();
|
Pair<Integer, Integer> cur = queue.poll();
|
||||||
int cr = cur.getKey(), cc = cur.getValue();
|
int cr = cur.getKey(), cc = cur.getValue();
|
||||||
grids[cr][cc] = 0; // 标记
|
grids[cr][cc] = 1; // 标记
|
||||||
for (int[] d : direction) {
|
for (int[] d : direction) {
|
||||||
int nr = cr + d[0], nc = cc + d[1];
|
int nr = cr + d[0], nc = cc + d[1];
|
||||||
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 0) {
|
if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 1) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (nr == tr && nc == tc) {
|
if (nr == m - 1 && nc == n - 1) {
|
||||||
return pathLength;
|
return pathLength + 1;
|
||||||
}
|
}
|
||||||
queue.add(new Pair<>(nr, nc));
|
queue.add(new Pair<>(nr, nc));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user