-
Notifications
You must be signed in to change notification settings - Fork 0
Shortest Path In A Grid With Obstacles Elimination #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination/memo.md
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # 1293. Shortest Path in a Grid with Obstacles Elimination | ||
|
|
||
| ## step1 | ||
| 24mほどかかった。 | ||
|
|
||
| BFSを思いついた。 | ||
|
|
||
| visited を setにするとTLEするので、remainingの最大値を保持しておく。 | ||
|
|
||
| 計算量はどちらも O(mnk) で実行時間に直すと 40**4 / 10**7 = 0.256s | ||
|
|
||
| DPを少し考えたが無理ではないか?下右だけの遷移ならば一方通行なので可能だが。 | ||
|
|
||
| ## 他の人の解法 | ||
|
|
||
|
|
||
| ```python | ||
| class Solution: | ||
| def shortestPath(self, grid: list[list[int]], k: int) -> int: | ||
|
|
||
| m, n = len(grid), len(grid[0]) | ||
| # [1] this check significantly improves runtime, i.e., | ||
| # we can use path (0,0) -> (0,n-1) -> (m-1,n-1) | ||
| if k >= m + n - 2: return m + n - 2 | ||
|
|
||
| # [2] we use deque to store and update a BFS state that is | ||
| # (x, y, obstacles we can destroy, steps done so far) | ||
| dq = deque([(0, 0, k, 0)]) | ||
| # [3] we also keep track of visited cells | ||
| seen = set() | ||
|
|
||
| while dq: | ||
| i, j, k, s = dq.popleft() | ||
| # [4] successfully reached lower right corner | ||
| if (i,j) == (m-1,n-1) : return s | ||
|
|
||
| # [5] scan all possible directions | ||
| for ii, jj in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]: | ||
| # [6] check boundaries and obstacles | ||
| if 0 <= ii < m and 0 <= jj < n and k >= grid[ii][jj]: | ||
| # [7] make (and remember) a step | ||
| step = (ii, jj, k-grid[ii][jj], s+1) | ||
| if step[0:3] not in seen: | ||
| seen.add(step[0:3]) | ||
| dq.append(step) | ||
|
|
||
| # [8] failed to reach lower right corner | ||
| return -1 | ||
| ``` | ||
|
|
||
| set() を使っているがAC。`if k >= m + n - 2: return m + n - 2`が効いている | ||
|
|
||
| 自分のに入れても 104ms -> 3ms と高速化した | ||
|
|
||
| --- | ||
|
|
||
| 他の解法はなさそうだが、LLMによるとA-starが使えるかもしれない | ||
|
|
||
44 changes: 44 additions & 0 deletions
44
1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination/step1.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| EMPTY = 0 | ||
| OBSTACLE = 1 | ||
|
|
||
| class Solution: | ||
|
|
||
| def shortestPath(self, grid: List[List[int]], k: int) -> int: | ||
| if not grid or not grid[0]: | ||
| return -1 | ||
|
|
||
| num_rows = len(grid) | ||
| num_cols = len(grid[0]) | ||
|
|
||
| max_remaining_k = [[-1] * num_cols for _ in range(num_rows)] | ||
| max_remaining_k[0][0] = k | ||
|
|
||
| frontier = [(0, 0, k)] | ||
| steps = 0 | ||
|
|
||
| while frontier: | ||
| next_frontier = [] | ||
| for row, col, remaining_k in frontier: | ||
| if row == num_rows - 1 and col == num_cols - 1: | ||
| return steps | ||
|
|
||
| for next_row, next_col in ((row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)): | ||
| if not (0 <= next_row < num_rows and 0 <= next_col < num_cols): | ||
| continue | ||
|
|
||
| is_obstacle = (grid[next_row][next_col] == OBSTACLE) | ||
| next_remaining_k = remaining_k - (1 if is_obstacle else 0) | ||
|
|
||
| if next_remaining_k < 0: | ||
| continue | ||
|
|
||
| if max_remaining_k[next_row][next_col] >= next_remaining_k: | ||
| continue | ||
|
|
||
| max_remaining_k[next_row][next_col] = next_remaining_k | ||
| next_frontier.append((next_row, next_col, next_remaining_k)) | ||
|
|
||
| frontier = next_frontier | ||
| steps += 1 | ||
|
|
||
| return -1 |
47 changes: 47 additions & 0 deletions
47
1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination/step2.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| EMPTY = 0 | ||
| OBSTACLE = 1 | ||
|
|
||
| class Solution: | ||
|
|
||
| def shortestPath(self, grid: List[List[int]], k: int) -> int: | ||
| if not grid or not grid[0]: | ||
| return -1 | ||
|
|
||
| num_rows = len(grid) | ||
| num_cols = len(grid[0]) | ||
|
|
||
| if k >= num_rows + num_cols - 2: | ||
| return num_rows + num_cols - 2 | ||
|
|
||
| max_remaining_k = [[-1] * num_cols for _ in range(num_rows)] | ||
| max_remaining_k[0][0] = k | ||
|
|
||
| frontier = [(0, 0, k)] | ||
| steps = 0 | ||
|
|
||
| while frontier: | ||
| next_frontier = [] | ||
| for row, col, remaining_k in frontier: | ||
| if row == num_rows - 1 and col == num_cols - 1: | ||
| return steps | ||
|
|
||
| for next_row, next_col in ((row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)): | ||
| if not (0 <= next_row < num_rows and 0 <= next_col < num_cols): | ||
| continue | ||
|
|
||
| is_obstacle = (grid[next_row][next_col] == OBSTACLE) | ||
| next_remaining_k = remaining_k - (1 if is_obstacle else 0) | ||
|
|
||
| if next_remaining_k < 0: | ||
| continue | ||
|
|
||
| if max_remaining_k[next_row][next_col] >= next_remaining_k: | ||
| continue | ||
|
|
||
| max_remaining_k[next_row][next_col] = next_remaining_k | ||
| next_frontier.append((next_row, next_col, next_remaining_k)) | ||
|
|
||
| frontier = next_frontier | ||
| steps += 1 | ||
|
|
||
| return -1 |
43 changes: 43 additions & 0 deletions
43
1293.Shortest-Path-in-a-Grid-with-Obstacles-Elimination/step2_deque.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import collections | ||
|
|
||
| EMPTY = 0 | ||
| OBSTACLE = 1 | ||
|
|
||
| class Solution: | ||
|
|
||
| def shortestPath(self, grid: List[List[int]], k: int) -> int: | ||
| if not grid or not grid[0]: | ||
| return -1 | ||
|
|
||
| num_rows = len(grid) | ||
| num_cols = len(grid[0]) | ||
|
|
||
| if k >= num_rows + num_cols - 2: | ||
| return num_rows + num_cols - 2 | ||
|
|
||
| max_remaining_k = [[-1] * num_cols for _ in range(num_rows)] | ||
| max_remaining_k[0][0] = k | ||
|
|
||
| dq = collections.deque([(0, 0, k, 0)]) | ||
|
|
||
| while dq: | ||
| row, col, remaining_k, steps = dq.popleft() | ||
| if row == num_rows - 1 and col == num_cols - 1: | ||
| return steps | ||
|
|
||
| for next_row, next_col in ((row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)): | ||
| if not (0 <= next_row < num_rows and 0 <= next_col < num_cols): | ||
| continue | ||
|
|
||
| next_remaining_k = remaining_k - 1 if (grid[next_row][next_col] == OBSTACLE) else remaining_k | ||
|
|
||
| if next_remaining_k < 0: | ||
| continue | ||
|
|
||
| if max_remaining_k[next_row][next_col] >= next_remaining_k: | ||
| continue | ||
|
|
||
| max_remaining_k[next_row][next_col] = next_remaining_k | ||
| dq.append((next_row, next_col, next_remaining_k, steps + 1)) | ||
|
|
||
| return -1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これ、ここまでの破壊回数が1で到達できたならば、破壊回数が2では調べる必要はないのではないでしょうか。
私はこれはダイクストラに破壊回数がついていて、到達時間と破壊回数でパレート最適なのだけ残すというイメージです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dequeを使ったBFS実装も書いてみました。辺コスト1なのでダイクストラと同等です。破壊回数最大だけを残すのはパレート最適といえますね。