1+ """
2+ #20057. 마법사 상어와 토네이도
3+ https://www.acmicpc.net/problem/20057
4+ """
5+ # 허리케인 이동방향
6+ delta = [(0 , - 1 ), (1 , 0 ), (0 , 1 ), (- 1 , 0 )]
7+
8+ # 2. 허리케인 방향에 맞춰 비율 배열 바꾸기 -> arr를 반시계방향으로 90도 회전하는 함수
9+ def rotate_90 (proportion ):
10+ new_proportion = list (reversed (list (zip (* proportion ))))
11+ return new_proportion
12+ p = [[0 , 0 , 0.02 , 0 , 0 ], [0 , 0.1 , 0.07 , 0.01 , 0 ], [0.05 , 0 , 0 , 0 , 0 ], [0 , 0.1 , 0.07 , 0.01 , 0 ], [0 , 0 , 0.02 , 0 , 0 ]]
13+ p1 = rotate_90 (p )
14+ p2 = rotate_90 (p1 )
15+ p3 = rotate_90 (p2 )
16+ proportions = [p , p1 , p2 , p3 ]
17+ alphas = [(2 , 1 ), (3 , 2 ), (2 , 3 ), (1 , 2 )] # 허리케인 이동 방향에 따른 알파 위치
18+
19+ # 3. 허리케인 이동 + 모래 이동
20+ def solution ():
21+ # 3-1. 초기 조건 맞추기
22+ outer_sand = 0 # 배열 밖으로 나간 sand
23+ tr = sr # 토네이도 위치 인덱스 -> 시작 지점 부터 시작
24+ tc = sc
25+ curl = 0 # 현재 토네이도 방향
26+ turn = 2 # 토네이도 방향 바꾸는 지표 -> 좌-하 : turn = 2 /우-상: turn = 4, 2씩 늘어난다.
27+ now = 0 # 토네이도 직선 길이
28+ proportion = proportions [0 ] # 토네이도 방향에 따른 비율 배열
29+
30+ # 3-2 토네이도가 종료될때까지 토네이도 이동 -> 모래 이동 -> 토네이도 방향 바꾸기
31+ while not (tr == 0 and tc == 0 ): # 토네이도가 종료될 때까지 반복한다.
32+
33+ # 3-2-1. 토네이도 이동
34+ tr += delta [curl ][0 ] # 현재 토네이도 위치
35+ tc += delta [curl ][1 ]
36+ now += 1 # 토네이도 길이 갱신
37+ sand = data [tr ][tc ]
38+ data [tr ][tc ] = 0 # 토네이도 위치에 있는 모래는 모두 proportion에 따라 이동한다.
39+ left = sand # proportion으로 이동하고 남은 모래
40+
41+ # 3-2-2. proportion의 y위치와 data의 토네이도 위치를 일치 시켜 모래 정보 갱신하기
42+ for r in range (5 ):
43+ for c in range (5 ):
44+ now_sand = int (sand * proportion [r ][c ])
45+ left -= now_sand
46+ if 0 <= tr + r - 2 < N and 0 <= tc + c - 2 < N : # data 배열 안에 가능하다면 data 갱신
47+ data [tr + r - 2 ][tc + c - 2 ] += now_sand
48+ else : # 불가능 하다면 밖으로 나간 모래
49+ outer_sand += now_sand
50+
51+ # alpha 위치에 남은 모래 두기
52+ if 0 <= tr + alphas [curl ][0 ]- 2 < N and 0 <= tc + alphas [curl ][1 ] - 2 < N :
53+ data [tr + alphas [curl ][0 ] - 2 ][tc + alphas [curl ][1 ] - 2 ] += left
54+ else :
55+ outer_sand += left
56+
57+ # 3-2-3. 토네이도 방향 바꾸기
58+ if now == turn // 2 or now == turn :
59+ curl = (curl + 1 ) % 4
60+ proportion = proportions [curl ]
61+ if now == turn :
62+ now = 0
63+ turn += 2
64+
65+ # 3-3. 토네이도 종료 후 바깥의 모래
66+ print (outer_sand )
67+ return
68+
69+
70+ # 1. input 받기
71+ N = int (input ())
72+ sr = sc = N // 2 # 허리케인 시작지점
73+ data = [list (map (int , input ().split ())) for _ in range (N )]
74+ solution ()
0 commit comments