Skip to content

Commit 751ea44

Browse files
authored
Merge pull request #139 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 2월 2주차 / 2문제
2 parents 10620ef + 0013817 commit 751ea44

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-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)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 입력
6+
N, M, K = map(int, input().split())
7+
grid = [[deque([]) for _ in range(N)] for _ in range(N)]
8+
for _ in range(M):
9+
r, c, m, s, d = map(int, input().split())
10+
grid[r-1][c-1].append([m, s, d])
11+
12+
# 방향벡터
13+
directions = [(-1, 0), (-1, 1), (0, 1), (1, 1),
14+
(1, 0), (1, -1), (0, -1), (-1, -1)]
15+
16+
# K번만큼 반복
17+
temp_grid = [[deque() for _ in range(N)] for _ in range(N)]
18+
19+
for _ in range(K):
20+
# 1. 이동하기
21+
temp_grid[i][j].clear() # 기존 구조 초기화
22+
23+
for i in range(N):
24+
for j in range(N):
25+
while grid[i][j]:
26+
m, s, d = grid[i][j].popleft()
27+
dx, dy = directions[d]
28+
nx, ny = (i + dx * s) % N, (j + dy * s) % N
29+
temp_grid[nx][ny].append([m, s, d])
30+
grid = temp_grid
31+
32+
# 2. 2개 이상의 파이어볼 칸은 합치고 나누기
33+
for i in range(N):
34+
for j in range(N):
35+
# 한 칸에 두 개 이상 있을 경우
36+
if len(grid[i][j]) > 1:
37+
sum_m, sum_s, cnt_odd, cnt_even = 0, 0, 0, 0
38+
cnt_total = len(grid[i][j])
39+
40+
# 합치기
41+
while grid[i][j]:
42+
m, s, d = grid[i][j].popleft()
43+
sum_m += m
44+
sum_s += s
45+
if d % 2 == 0:
46+
cnt_even += 1
47+
else:
48+
cnt_odd += 1
49+
50+
# 방향 결정
51+
if cnt_odd == cnt_total or cnt_even == cnt_total:
52+
d_list = [0, 2, 4, 6]
53+
else:
54+
d_list = [1, 3, 5, 7]
55+
56+
# 나누기
57+
new_m = sum_m // 5 # 질량
58+
new_s = sum_s // cnt_total # 속력
59+
60+
if new_m > 0: # 질량이 0 이면 소멸
61+
for d in d_list: # 방향
62+
grid[i][j].append([new_m, new_s, d])
63+
64+
# 정답(파이어볼 질량의 합) 구하기
65+
answer = 0
66+
for i in range(N):
67+
for j in range(N):
68+
for m, s, d in grid[i][j]:
69+
answer += m
70+
71+
print(answer)

0 commit comments

Comments
 (0)