Skip to content

Commit 24bc2c0

Browse files
committed
[BOJ] #20057. 마법사 상어와 토네이도 / 골드3 / 90분 / 실패
1 parent 10620ef commit 24bc2c0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
def move_sand(time, dx, dy, direction):
5+
global answer, sx, sy, grid
6+
for _ in range(time):
7+
sx += dx
8+
sy += dy
9+
10+
if sy < 0: # 범위 밖일 경우 stop
11+
break # 토네이도는 항상 (0, 0)에 멈춘다.
12+
13+
# 모래 이동
14+
total = 0 # α(알파)를 구하기 위한 변수
15+
for ddx, ddy, ratio in direction:
16+
nx, ny = sx + ddx, sy + ddy
17+
18+
if ratio == 0: # α 위치
19+
new_sand = grid[sx][sy] - total
20+
else:
21+
new_sand = int(grid[sx][sy] * ratio)
22+
total += new_sand
23+
24+
if 0 <= nx < N and 0 <= ny < N: # 격자 안
25+
grid[nx][ny] += new_sand
26+
else: # 격자 밖
27+
answer += new_sand
28+
29+
N = int(input())
30+
grid = [list(map(int, input().split())) for _ in range(N)]
31+
32+
# 모래 흩날리는 비율 (기준: 왼쪽 방향)
33+
left = [(1, 1, 0.01), (-1, 1, 0.01), (1, 0, 0.07), (-1, 0, 0.07), (1, -1, 0.1),
34+
(-1, -1, 0.1), (2, 0, 0.02), (-2, 0, 0.02), (0, -2, 0.05), (0, -1, 0)]
35+
36+
right = [(x, -y, z) for x, y, z in left]
37+
down = [(-y, x, z) for x, y, z in left]
38+
up = [(y, x, z) for x, y, z in left]
39+
40+
sx, sy = N // 2, N // 2 # 토네이도 시작 위치
41+
answer = 0 # 격자 밖으로 나간 모래양
42+
43+
# 토네이도 이동 방향
44+
for i in range(1, N+1):
45+
if i % 2 == 1: # 홀수이면 좌 → 하
46+
move_sand(i, 0, -1, left) # 좌
47+
move_sand(i, 1, 0, down) # 하
48+
else: # 짝수이면 우 → 상
49+
move_sand(i, 0, 1, right) # 우
50+
move_sand(i, -1, 0, up) # 상
51+
52+
print(answer)

0 commit comments

Comments
 (0)