|
| 1 | +''' |
| 2 | +BOJ #20057. 마법사 상어와 토네이도(골드3) |
| 3 | +https://www.acmicpc.net/problem/20057 |
| 4 | +유형: Implementation, Simulation |
| 5 | +''' |
| 6 | + |
| 7 | +# 모래 계산하는 함수 |
| 8 | +def recount(time, dx, dy, direction): |
| 9 | + global ans, s_x, s_y |
| 10 | + |
| 11 | + # y좌표 계산 & x좌표 갱신 |
| 12 | + for _ in range(time): |
| 13 | + s_x += dx |
| 14 | + s_y += dy |
| 15 | + if s_y < 0: # 범위 밖이면 stop |
| 16 | + break |
| 17 | + |
| 18 | + # 3. a, out_sand |
| 19 | + total = 0 # a 구하기 위한 변수 |
| 20 | + for dx, dy, z in direction: |
| 21 | + nx = s_x + dx |
| 22 | + ny = s_y + dy |
| 23 | + if z == 0: # a(나머지) |
| 24 | + new_sand = sand[s_x][s_y] - total |
| 25 | + else: # 비율 |
| 26 | + new_sand = int(sand[s_x][s_y] * z) |
| 27 | + total += new_sand |
| 28 | + |
| 29 | + if 0 <= nx < N and 0 <= ny < N: # 인덱스 범위이면 값 갱신 |
| 30 | + sand[nx][ny] += new_sand |
| 31 | + else: # 범위 밖이면 ans 카운트 |
| 32 | + ans += new_sand |
| 33 | + |
| 34 | + |
| 35 | +N = int(input()) |
| 36 | +sand = [list(map(int, input().split())) for _ in range(N)] |
| 37 | + |
| 38 | +# 2. 방향별 모래 비율 위치 |
| 39 | +left = [(1, 1, 0.01), (-1, 1, 0.01), (1, 0, 0.07), (-1, 0, 0.07), (1, -1, 0.1), |
| 40 | + (-1, -1, 0.1), (2, 0, 0.02), (-2, 0, 0.02), (0, -2, 0.05), (0, -1, 0)] |
| 41 | +right = [(x, -y, z) for x, y, z in left] |
| 42 | +down = [(-y, x, z) for x, y, z in left] |
| 43 | +up = [(y, x, z) for x, y, z in left] |
| 44 | + |
| 45 | +s_x, s_y = N//2, N//2 # 시작좌표(x좌표) |
| 46 | +ans = 0 # out_sand |
| 47 | + |
| 48 | +# 1.토네이도 회전 방향(y위치) |
| 49 | +for i in range(1, N + 1): |
| 50 | + if i % 2: |
| 51 | + recount(i, 0, -1, left) |
| 52 | + recount(i, 1, 0, down) |
| 53 | + else: |
| 54 | + recount(i, 0, 1, right) |
| 55 | + recount(i, -1, 0, up) |
| 56 | + |
| 57 | +print(ans) |
0 commit comments