diff --git a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.cpp b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.cpp index f87742ec..e53d677c 100644 --- a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.cpp +++ b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.cpp @@ -1,27 +1,32 @@ +typedef pair PII; class Solution { public: bool hasValidPath(vector>& grid) { - r = grid.size(), c = grid[0].size(); - return dfs(grid, 0, 0, 0) || dfs(grid, 0, 0, 1) || dfs(grid, 0, 0, 2) || dfs(grid, 0, 0, 3); - } -private: - int r, c; - int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; - int p[6][4] = {{2, -1, 0, -1}, - {-1, 3, -1, 1}, - {-1, 2, 1, -1}, - {1, 0, -1, -1}, - {-1, -1, 3, 2}, - {3, -1, -1, 0}}; - - bool dfs(vector>& grid, int x, int y, int di) { - if (p[grid[x][y] - 1][di] == -1) return false; - if (x == r - 1 && y == c - 1) return true; - - int dx = d[p[grid[x][y] - 1][di]][0], dy = d[p[grid[x][y] - 1][di]][1]; - int nx = x + dx, ny = y + dy; - if (0 <= nx && nx < r && 0 <= ny && ny < c) { - if (dfs(grid, nx, ny, (p[grid[x][y] - 1][di] + 2) % 4)) return true; + int r = grid.size(), c = grid[0].size(); + int d[5] = {0, 1, 0, -1, 0}; + int p[6][4] = {{1, 0, 1, 0}, + {0, 1, 0, 1}, + {0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 1, 1}, + {1, 0, 0, 1}}; + + queue q; + q.push({0, 0}); + while (!q.empty()) { + auto pre = q.front(); q.pop(); + int x = pre.first, y = pre.second; + if (x == r - 1 && y == c - 1) return true; + + int k = grid[x][y]; + grid[x][y] = 0; + for (int i = 0; i < 4; i++) { + int nx = x + d[i], ny = y + d[i + 1]; + if (nx >= 0 && nx < r && ny >= 0 && ny < c && grid[nx][ny] && + p[k - 1][i] && p[grid[nx][ny] - 1][i ^ 2]) { + q.push({nx, ny}); + } + } } return false; } diff --git a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.go b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.go index 754e43c6..a7f12101 100644 --- a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.go +++ b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.go @@ -1,30 +1,30 @@ func hasValidPath(grid [][]int) bool { r, c := len(grid), len(grid[0]) - d := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}} - p := [][]int{{2, -1, 0, -1}, - {-1, 3, -1, 1}, - {-1, 2, 1, -1}, - {1, 0, -1, -1}, - {-1, -1, 3, 2}, - {3, -1, -1, 0}} + d := []int{0, 1, 0, -1, 0}; + p := [][]int{{1, 0, 1, 0}, + {0, 1, 0, 1}, + {0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 1, 1}, + {1, 0, 0, 1}} - q := [][]int{{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 0, 3}} + q := [][]int{{0, 0}} for len(q) > 0 { pre := q[0] q = q[1:] - x, y, di := pre[0], pre[1], pre[2] - - if p[grid[x][y] - 1][di] == -1 { - continue - } + x, y := pre[0], pre[1] + if x == r - 1 && y == c - 1 { return true } - dx, dy := d[p[grid[x][y] - 1][di]][0], d[p[grid[x][y] - 1][di]][1]; - nx, ny := x + dx, y + dy; - if 0 <= nx && nx < r && 0 <= ny && ny < c { - q = append(q, []int{nx, ny, (p[grid[x][y] - 1][di] + 2) % 4}) + k := grid[x][y] + grid[x][y] = 0 + for i := 0; i < 4; i++ { + nx, ny := x + d[i], y + d[i + 1] + if 0 <= nx && nx < r && 0 <= ny && ny < c && grid[nx][ny] > 0 && p[k - 1][i] == 1 && p[grid[nx][ny] - 1][i ^ 2] == 1 { + q = append(q, []int{nx, ny}) + } } } return false diff --git a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.java b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.java index b5d9cbb8..20fa9187 100644 --- a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.java +++ b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.java @@ -1,26 +1,30 @@ class Solution { public boolean hasValidPath(int[][] grid) { - r = grid.length; c = grid[0].length; - return dfs(grid, 0, 0, 0) || dfs(grid, 0, 0, 1) || dfs(grid, 0, 0, 2) || dfs(grid, 0, 0, 3); - } - - private int r, c; - private int[][] d = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; - private int[][] p = {{2, -1, 0, -1}, - {-1, 3, -1, 1}, - {-1, 2, 1, -1}, - {1, 0, -1, -1}, - {-1, -1, 3, 2}, - {3, -1, -1, 0}}; - - private boolean dfs(int[][] grid, int x, int y, int di) { - if (p[grid[x][y] - 1][di] == -1) return false; - if (x == r - 1 && y == c - 1) return true; - - int dx = d[p[grid[x][y] - 1][di]][0], dy = d[p[grid[x][y] - 1][di]][1]; - int nx = x + dx, ny = y + dy; - if (0 <= nx && nx < r && 0 <= ny && ny < c) { - if (dfs(grid, nx, ny, (p[grid[x][y] - 1][di] + 2) % 4)) return true; + int r = grid.length, c = grid[0].length; + int[] d = {0, 1, 0, -1, 0}; + int[][] p = {{1, 0, 1, 0}, + {0, 1, 0, 1}, + {0, 1, 1, 0}, + {1, 1, 0, 0}, + {0, 0, 1, 1}, + {1, 0, 0, 1}}; + + Queue q = new ArrayDeque(); + q.add(new int[]{0, 0}); + while (!q.isEmpty()) { + int[] it = q.poll(); + int x = it[0], y = it[1]; + if (x == r - 1 && y == c - 1) return true; + + int k = grid[x][y]; + grid[x][y] = 0; + for (int i = 0; i < 4; i++) { + int nx = x + d[i], ny = y + d[i + 1]; + if (nx >= 0 && nx < r && ny >= 0 && ny < c && grid[nx][ny] > 0 && + p[k - 1][i] == 1 && p[grid[nx][ny] - 1][i ^ 2] == 1) { + q.add(new int[]{nx, ny}); + } + } } return false; } diff --git a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.js b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.js index 07848bd2..b517bde9 100644 --- a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.js +++ b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.js @@ -1,54 +1,28 @@ -/********************************************** -error: Maximum call stack size exceeded -************************************************ var hasValidPath = function(grid) { - let r = grid.length; c = grid[0].length; - let d = [[0, 1], [1, 0], [0, -1], [-1, 0]]; - let p = [[2, -1, 0, -1], - [-1, 3, -1, 1], - [-1, 2, 1, -1], - [1, 0, -1, -1], - [-1, -1, 3, 2], - [3, -1, -1, 0]]; - - let dfs = function(grid, x, y, di) { - if (p[grid[x][y] - 1][di] == -1) return false; - if (x == r - 1 && y == c - 1) return true; - - let dx = d[p[grid[x][y] - 1][di]][0], dy = d[p[grid[x][y] - 1][di]][1]; - let nx = x + dx, ny = y + dy; - if (0 <= nx && nx < r && 0 <= ny && ny < c) { - if (dfs(grid, nx, ny, (p[grid[x][y] - 1][di] + 2) % 4)) return true; - } - return false; - } + let r = grid.length, c = grid[0].length; + let d = [0, 1, 0, -1, 0]; + let p = [[1, 0, 1, 0], + [0, 1, 0, 1], + [0, 1, 1, 0], + [1, 1, 0, 0], + [0, 0, 1, 1], + [1, 0, 0, 1]]; - return dfs(grid, 0, 0, 0) || dfs(grid, 0, 0, 1) || dfs(grid, 0, 0, 2) || dfs(grid, 0, 0, 3); -}; -*/ - -var hasValidPath = function(grid) { - let r = grid.length; c = grid[0].length; - let d = [[0, 1], [1, 0], [0, -1], [-1, 0]]; - let p = [[2, -1, 0, -1], - [-1, 3, -1, 1], - [-1, 2, 1, -1], - [1, 0, -1, -1], - [-1, -1, 3, 2], - [3, -1, -1, 0]]; - - let q = [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 0, 3]]; + let q = [[0, 0]]; while (q.length) { - let pre = q.shift(); - let x = pre[0], y = pre[1], di = pre[2]; - if (p[grid[x][y] - 1][di] == -1) continue; + let it = q.shift(); + let x = it[0], y = it[1]; if (x == r - 1 && y == c - 1) return true; - - let dx = d[p[grid[x][y] - 1][di]][0], dy = d[p[grid[x][y] - 1][di]][1]; - let nx = x + dx, ny = y + dy; - if (0 <= nx && nx < r && 0 <= ny && ny < c) { - q.push([nx, ny, (p[grid[x][y] - 1][di] + 2) % 4]); + + let k = grid[x][y]; + grid[x][y] = 0; + for (let i = 0; i < 4; i++) { + let nx = x + d[i], ny = y + d[i + 1]; + if (nx >= 0 && nx < r && ny >= 0 && ny < c && grid[nx][ny] > 0 && + p[k - 1][i] == 1 && p[grid[nx][ny] - 1][i ^ 2] == 1) { + q.push([nx, ny]); + } } } - return false + return false; }; \ No newline at end of file diff --git a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.py b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.py index fc363c2e..70c5bac9 100644 --- a/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.py +++ b/src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.py @@ -1,27 +1,23 @@ class Solution: def hasValidPath(self, grid: List[List[int]]) -> bool: - d = [[0, 1], [1, 0], [0, -1], [-1, 0]] - p = [[2, -1, 0, -1], - [-1, 3, -1, 1], - [-1, 2, 1, -1], - [1, 0, -1, -1], - [-1, -1, 3, 2], - [3, -1, -1, 0]] - r, c = len(grid), len(grid[0]) - def dfs(x, y, di): - ''' - x: 横坐标 - y: 纵坐标 - di: 前一次方向 - ''' - if p[grid[x][y] - 1][di] == -1: return False + d = [0, 1, 0, -1, 0] + p = [[1, 0, 1, 0], + [0, 1, 0, 1], + [0, 1, 1, 0], + [1, 1, 0, 0], + [0, 0, 1, 1], + [1, 0, 0, 1]] + + q = [[0, 0]] + while q: + x, y = q.pop(0) if x == r - 1 and y == c - 1: return True - dx, dy = d[p[grid[x][y] - 1][di]] - nx, ny = x + dx, y + dy - if 0 <= nx < r and 0 <= ny < c: - if dfs(nx, ny, (p[grid[x][y] - 1][di] + 2) % 4): return True - return False - - return dfs(0, 0, 0) or dfs(0, 0, 1) or dfs(0, 0, 2) or dfs(0, 0, 3) \ No newline at end of file + k, grid[x][y] = grid[x][y], 0 + for i in range(4): + nx, ny = x + d[i], y + d[i + 1] + if 0 <= nx < r and 0 <= ny < c and grid[nx][ny] and \ + p[k - 1][i] and p[grid[nx][ny] - 1][i ^ 2]: # 判断前一个街道和后一个街道是否联通 + q.append([nx, ny]) + return False \ No newline at end of file