-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
executable file
·108 lines (85 loc) · 2.19 KB
/
10.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env pypy3
# 17/107
from util import *
if len(sys.argv) == 1:
sys.stdin = open(__file__.replace("py", "in"))
ADJ = {
"|": [Point.of(0, 1), Point.of(0, -1)],
"-": [Point.of(1, 0), Point.of(-1, 0)],
"F": [Point.of(0, 1), Point.of(1, 0)],
"J": [Point.of(0, -1), Point.of(-1, 0)],
"L": [Point.of(1, 0), Point.of(0, -1)],
"7": [Point.of(0, 1), Point.of(-1, 0)]
}
G = [list(l.replace("O", ".").replace("I", ".")) for l in lines()]
for y, l in enumerate(G):
for x, c in enumerate(l):
if c == "S":
start = Point.of(x, y)
break
G[start.y][start.x] = "J"
# G[start.y][start.x] = "F"
H = len(G)
W = len(G[0])
NG = [["."] * (W*3) for _ in range(H*3)]
for y, l in enumerate(G):
for x, c in enumerate(l):
if c == ".":
continue
NG[y*3+1][x*3+1] = c
for diff in ADJ[c]:
n = Point.of(3*x+1, 3*y+1) + diff
NG[n.y][n.x] = "|" if diff.x == 0 else "-"
G = NG
# print("\n".join("".join(l) for l in NG))
# exit()
# for k, aa in ADJ.items():
# for diff in aa:
# n = start + diff
# if G[n.y][n.x] == ".":
# break
# else:
# print(k)
start = start * 3 + Point.of(1, 1)
D = {start: 0}
Q = [start]
for p in Q:
for diff in ADJ[G[p.y][p.x]]:
n = p + diff
assert G[n.y][n.x] != "."
if n not in D:
D[n] = D[p] + 1
Q.append(n)
# prints(max(D.values()))
in_loop = set(D)
H = len(G)
W = len(G[0])
V = set()
Q = []
for x in range(W):
Q.extend([Point.of(x, -1), Point.of(x, H)])
for y in range(H):
Q.extend([Point.of(-1, y), Point.of(W, y)])
V = set(Q)
for p in Q:
assert p not in in_loop
for diff in DIR:
n = p + diff
if 0 <= n.x < W and 0 <= n.y < H and n not in in_loop and n not in V:
V.add(n)
Q.append(n)
V |= in_loop
RG = [[] for _ in range(H//3)]
res = 0
for y in range(1, H, 3):
for x in range(1, W, 3):
p = Point.of(x, y)
if p not in V:
res += 1
RG[y//3].append("I")
elif p not in in_loop:
RG[y//3].append("O")
else:
RG[y//3].append(G[y][x])
print("\n".join("".join(l) for l in RG))
prints(res)