forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCut Off Trees for Golf Event.py
41 lines (39 loc) · 1.23 KB
/
Cut Off Trees for Golf Event.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
from collections import deque
class Solution:
def cutOffTree(self, forest: List[List[int]]) -> int:
a = []
n = len(forest)
m = len(forest[0])
for i in range(n):
for j in range(m):
if forest[i][j] > 1:
a.append(forest[i][j])
a.sort()
s = 0
ux = 0
uy = 0
for h in a:
dist = [[None] * m for i in range(n)]
q = deque()
q.append((ux, uy))
dist[ux][uy] = 0
while q:
ux, uy = q.popleft()
if forest[ux][uy] == h:
break
d = [(-1, 0), (0, 1), (1, 0), (0, -1)]
for dx, dy in d:
vx = ux + dx
vy = uy + dy
if vx < 0 or vx >= n or vy < 0 or vy >= m:
continue
if forest[vx][vy] == 0:
continue
if dist[vx][vy] is None:
q.append((vx, vy))
dist[vx][vy] = dist[ux][uy] + 1
if forest[ux][uy] == h:
s += dist[ux][uy]
else:
return -1
return s