-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path2814-minimum-time-takes-to-reach-destination-without-drowning.py
58 lines (50 loc) · 2.03 KB
/
2814-minimum-time-takes-to-reach-destination-without-drowning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# time complexity: O(m*n)
# space complecity: O(m*n)
from collections import deque
from typing import List
class Solution:
def minimumSeconds(self, land: List[List[str]]) -> int:
ROW = len(land)
COL = len(land[0])
flood = deque()
moves = deque()
DIRS = [(1, 0), (0, 1), (-1, 0), (0, -1)]
seconds = 0
for r in range(ROW):
for c in range(COL):
if land[r][c] == 'S':
moves.append((r, c))
if land[r][c] == '*':
flood.append((r, c))
while moves:
spread = len(flood)
for _ in range(spread):
floodR, floodC = flood.popleft()
for dR, dC in DIRS:
nextR = floodR + dR
nextC = floodC + dC
if 0 <= nextR < ROW and 0 <= nextC < COL and land[nextR][nextC] == '.':
land[nextR][nextC] = '*'
flood.append((nextR, nextC))
move = len(moves)
for _ in range(move):
moveR, moveC = moves.popleft()
if land[moveR][moveC] == 'D':
return seconds
for dR, dC in DIRS:
nextR = moveR + dR
nextC = moveC + dC
if 0 <= nextR < ROW and 0 <= nextC < COL:
if land[nextR][nextC] == '.' or land[nextR][nextC] == 'D':
if land[nextR][nextC] != 'D':
land[nextR][nextC] = '*'
moves.append((nextR, nextC))
seconds += 1
return -1
land = [["D", ".", "*"], [".", ".", "."], [".", "S", "."]]
print(Solution().minimumSeconds(land))
land = [["D", "X", "*"], [".", ".", "."], [".", ".", "S"]]
print(Solution().minimumSeconds(land))
land = [["D", ".", ".", ".", "*", "."], [".", "X", ".",
"X", ".", "."], [".", ".", ".", ".", "S", "."]]
print(Solution().minimumSeconds(land))