diff --git a/docs/notes/Leetcode 题解 - 搜索.md b/docs/notes/Leetcode 题解 - 搜索.md index 4fd9504b..2e0729e9 100644 --- a/docs/notes/Leetcode 题解 - 搜索.md +++ b/docs/notes/Leetcode 题解 - 搜索.md @@ -61,6 +61,8 @@ ## 1. 计算在网格中从原点到特定点的最短路径长度 +[1091. Shortest Path in Binary Matrix(Medium)](https://leetcode.com/problems/shortest-path-in-binary-matrix/) + ```html [[1,1,0,1], [1,0,1,0], @@ -68,12 +70,12 @@ [1,0,1,1]] ``` -题目描述:1 表示可以经过某个位置,求解从 (0, 0) 位置到 (tr, tc) 位置的最短路径长度。 +题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。 ```java -public int minPathLength(int[][] grids, int tr, int tc) { - final int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; - final int m = grids.length, n = grids[0].length; +public int shortestPathBinaryMatrix(int[][] grids) { + int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}}; + int m = grids.length, n = grids[0].length; Queue> queue = new LinkedList<>(); queue.add(new Pair<>(0, 0)); int pathLength = 0; @@ -83,14 +85,14 @@ public int minPathLength(int[][] grids, int tr, int tc) { while (size-- > 0) { Pair cur = queue.poll(); int cr = cur.getKey(), cc = cur.getValue(); - grids[cr][cc] = 0; // 标记 + grids[cr][cc] = 1; // 标记 for (int[] d : direction) { 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; } - if (nr == tr && nc == tc) { - return pathLength; + if (nr == m - 1 && nc == n - 1) { + return pathLength + 1; } queue.add(new Pair<>(nr, nc)); } diff --git a/notes/Leetcode 题解 - 搜索.md b/notes/Leetcode 题解 - 搜索.md index 0437af64..38945d90 100644 --- a/notes/Leetcode 题解 - 搜索.md +++ b/notes/Leetcode 题解 - 搜索.md @@ -61,6 +61,8 @@ ## 1. 计算在网格中从原点到特定点的最短路径长度 +[1091. Shortest Path in Binary Matrix(Medium)](https://leetcode.com/problems/shortest-path-in-binary-matrix/) + ```html [[1,1,0,1], [1,0,1,0], @@ -68,12 +70,12 @@ [1,0,1,1]] ``` -题目描述:1 表示可以经过某个位置,求解从 (0, 0) 位置到 (tr, tc) 位置的最短路径长度。 +题目描述:0 表示可以经过某个位置,求解从左上角到右下角的最短路径长度。 ```java -public int minPathLength(int[][] grids, int tr, int tc) { - final int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; - final int m = grids.length, n = grids[0].length; +public int shortestPathBinaryMatrix(int[][] grids) { + int[][] direction = {{1, -1}, {1, 0}, {1, 1}, {0, -1}, {0, 1}, {-1, -1}, {-1, 0}, {-1, 1}}; + int m = grids.length, n = grids[0].length; Queue> queue = new LinkedList<>(); queue.add(new Pair<>(0, 0)); int pathLength = 0; @@ -83,14 +85,14 @@ public int minPathLength(int[][] grids, int tr, int tc) { while (size-- > 0) { Pair cur = queue.poll(); int cr = cur.getKey(), cc = cur.getValue(); - grids[cr][cc] = 0; // 标记 + grids[cr][cc] = 1; // 标记 for (int[] d : direction) { 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; } - if (nr == tr && nc == tc) { - return pathLength; + if (nr == m - 1 && nc == n - 1) { + return pathLength + 1; } queue.add(new Pair<>(nr, nc)); }