-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon21610.py
More file actions
93 lines (70 loc) · 2.18 KB
/
BaekJoon21610.py
File metadata and controls
93 lines (70 loc) · 2.18 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
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
# BaekJoon21610.py
N, M = map(int, input().split())
arr = [[] for _ in range(N)]
for i in range(N):
arr[i] = list(map(int, input().split()))
d = [0 for _ in range(M)]
s = [0 for _ in range(M)]
for i in range(M):
d[i], s[i] = map(int, input().split())
def move_clouds(clouds, d, s, visited):
global N, arr
dx = [0, -1, -1, -1, 0, 1, 1, 1]
dy = [-1, -1, 0, 1, 1, 1, 0, -1]
for cloud in clouds:
cloud[0] = cloud[0] + dx[d] * s
cloud[1] = cloud[1] + dy[d] * s
if cloud[0] < 0:
if (-1 * cloud[0]) % N == 0:
cloud[0] = 0
else:
cloud[0] = N - ((-1 * cloud[0]) % N)
elif cloud[0] >= N:
cloud[0] = cloud[0] % N
if cloud[1] < 0:
if (-1 * cloud[1]) % N == 0:
cloud[1] = 0
else:
cloud[1] = N - ((-1 * cloud[1]) % N)
elif cloud[1] >= N:
cloud[1] = cloud[1] % N
arr[cloud[0]][cloud[1]] += 1
visited[cloud[0]][cloud[1]] = True
return clouds, visited
def magic(clouds):
global arr
dx = [-1, -1, 1, 1]
dy = [-1, 1, 1, -1]
for cloud in clouds:
water_num = 0
for i in range(4):
nx = cloud[0] + dx[i]
ny = cloud[1] + dy[i]
if nx < 0 or ny < 0 or nx >= N or ny >= N:
continue
if arr[nx][ny] > 0:
water_num += 1
arr[cloud[0]][cloud[1]] += water_num
def make_clouds(clouds, visited):
global arr, N
new_clouds = []
for i in range(N):
for j in range(N):
if arr[i][j] >= 2 and not visited[i][j]:
new_clouds.append([i, j])
arr[i][j] -= 2
return new_clouds
def solution():
global M
clouds = [[N - 1, 0], [N - 1, 1], [N - 2, 0], [N - 2, 1]] # 초기 구름 위치
for i in range(M):
visited = [[False for _ in range(N)] for _ in range(N)]
clouds, visited = move_clouds(clouds, d[i] - 1, s[i], visited)
magic(clouds)
clouds = make_clouds(clouds, visited)
result = 0
for i in range(N):
for j in range(N):
result += arr[i][j]
print(result)
solution()