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