-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12-02.py
80 lines (79 loc) · 3.02 KB
/
12-02.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
from collections import deque
inputval = ""
plot_map = []
while True:
inputval = input()
if not inputval:
break
plot_map.append(list(inputval))
total = 0
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
for i, row in enumerate(plot_map):
for j, cell in enumerate(row):
if plot_map[i][j]:
first_cell = plot_map[i][j]
visited = set()
bfs_queue = deque()
bfs_queue.append((i, j))
minx, miny, maxx, maxy = i, j, i, j
while bfs_queue:
ti, tj = bfs_queue.popleft()
if (ti, tj) not in visited:
visited.add((ti, tj))
minx = min(minx, ti)
maxx = max(maxx, ti)
miny = min(miny, tj)
maxy = max(maxy, tj)
plot_map[ti][tj] = ""
for dx, dy in dirs:
ni, nj = ti + dx, tj + dy
if ni < 0 or ni >= len(plot_map) or nj < 0 or nj >= len(plot_map[0]):
pass
elif plot_map[ni][nj] != first_cell:
pass
else:
bfs_queue.append((ni, nj))
sides = 0
# Top side
for scani in range(minx, maxx + 1):
is_edge = False
for scanj in range(miny, maxy + 1):
if (scani, scanj) in visited and (scani - 1, scanj) not in visited:
if not is_edge:
sides += 1
is_edge = True
else:
is_edge = False
# Bottom side
for scani in range(minx, maxx + 1):
is_edge = False
for scanj in range(miny, maxy + 1):
if (scani, scanj) in visited and (scani + 1, scanj) not in visited:
if not is_edge:
sides += 1
is_edge = True
else:
is_edge = False
# Left side
for scanj in range(miny, maxy + 1):
is_edge = False
for scani in range(minx, maxx + 1):
if (scani, scanj) in visited and (scani, scanj - 1) not in visited:
if not is_edge:
sides += 1
is_edge = True
else:
is_edge = False
# Right side
for scanj in range(miny, maxy + 1):
is_edge = False
for scani in range(minx, maxx + 1):
if (scani, scanj) in visited and (scani, scanj + 1) not in visited:
if not is_edge:
sides += 1
is_edge = True
else:
is_edge = False
print(len(visited) * sides)
total += len(visited) * sides
print(total)