forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55f65b4
commit 1f27abf
Showing
5 changed files
with
107 additions
and
128 deletions.
There are no files selected for viewing
47 changes: 26 additions & 21 deletions
47
src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 17 additions & 17 deletions
34
src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 25 additions & 21 deletions
46
src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 21 additions & 47 deletions
68
src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
40 changes: 18 additions & 22 deletions
40
src/1391-Check-if-There-is-a-Valid-Path-in-a-Grid/1391.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
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 |