|
1 | 1 | import networkx as nx
|
2 | 2 |
|
3 | 3 | def solve(obstacles, S=70):
|
4 |
| - G = nx.DiGraph() |
5 |
| - for c in {x+1j*y for y in range(S+1) for x in range(S+1)}: |
6 |
| - for d in [1, 1j, -1, -1j]: |
7 |
| - if c not in obstacles and c+d not in obstacles: |
8 |
| - G.add_edge(c, c+d) |
| 4 | + G = nx.grid_graph((S+1, S+1)) |
| 5 | + G.remove_nodes_from(obstacles) |
| 6 | + return nx.has_path(G, (0,0), (S,S)) \ |
| 7 | + and len(nx.shortest_path(G, (0,0), (S,S))) |
9 | 8 |
|
10 |
| - return nx.has_path(G, 0, S+S*1j) and len(nx.shortest_path(G, 0, S+S*1j)) |
11 |
| - |
12 |
| -obstacles = [complex(line.replace(",", "+")+"j") for line in open(0)] |
13 |
| -print(solve(obstacles[:1024])) |
| 9 | +obstacles = [tuple(map(int, line.split(","))) for line in open(0)] |
| 10 | +print(solve(obstacles[:1024])-1) |
14 | 11 |
|
15 | 12 | lo, hi = 1024, len(obstacles)-1
|
16 | 13 | while lo < hi:
|
17 | 14 | mid = (lo + hi) // 2
|
18 | 15 | if solve(obstacles[:mid]): lo = mid+1
|
19 | 16 | else: hi = mid-1
|
20 | 17 |
|
21 |
| -print(str(obstacles[mid]).replace("+", ",")[1:-2]) |
| 18 | +print(*obstacles[mid], sep=",") |
0 commit comments