forked from shunr/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree2.py
More file actions
53 lines (47 loc) · 1.21 KB
/
tree2.py
File metadata and controls
53 lines (47 loc) · 1.21 KB
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
from heapq import *
import math
import sys
grid = {}
r, c = (int(i) for i in input().split())
bval = 0
bset = []
x, y = 0, 0;
def dist(x1, x2, y1, y2):
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
for i in range(r):
kek = [int(x) if x not in 'X.' else x for x in input().split()]
for j in range(c):
grid[(j,i)] = kek[j]
if kek[j] != '.' and kek[j] != 'X':
if kek[j] > bval:
bval = kek[j]
bset.clear()
bset.append((j, i))
elif kek[j] == bval:
bset.append((j, i))
elif kek[j] == 'X':
x = j
y = i
succ = 999999
wheresmygoddamntree = tuple()
for b in bset:
d = dist(x, b[0], y, b[1])
if d < succ:
wheresmygoddamntree = b
succ = d
q = [(0, 0, (x, y))]
visited = set()
while q:
heights, dist, v1 = heappop(q)
if v1 in visited:
continue
visited.add(v1)
pts = [(v1[0] + 1, v1[1]), (v1[0] - 1, v1[1]), (v1[0], v1[1] + 1), (v1[0], v1[1] - 1)]
for p in pts:
if p in grid:
if p == wheresmygoddamntree:
print(dist)
sys.exit(0)
k = 0 if grid[p] == "." else 1
h = 0 if (grid[p] == "." or grid[p] == "X") else grid[p]
heappush(q, (heights + h, dist + k, p))