<๋ฌธ์ ๋งํฌ>
https://www.acmicpc.net/problem/20056
20056๋ฒ: ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ
์ฒซ์งธ ์ค์ N, M, K๊ฐ ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ M๊ฐ์ ์ค์ ํ์ด์ด๋ณผ์ ์ ๋ณด๊ฐ ํ ์ค์ ํ๋์ฉ ์ฃผ์ด์ง๋ค. ํ์ด์ด๋ณผ์ ์ ๋ณด๋ ๋ค์ฏ ์ ์ ri, ci, mi, si, di๋ก ์ด๋ฃจ์ด์ ธ ์๋ค. ์๋ก ๋ค๋ฅธ ๋ ํ์ด์ด๋ณผ์ ์์น
www.acmicpc.net
<ํ์ด ์ ๋ต>
- ํน๋ณํ ์๊ณ ๋ฆฌ์ฆ์ ํ์๋ก ํ์ง ์๋ ๊ตฌํ ๋ฌธ์ ์ด๋ค.
- ์ด๋ค ์๋ฃ๊ตฌ์กฐํ์ ์ด๋ป๊ฒ ์ฌ์ฉํด์ ํ์ด์ด๋ณผ์ ์ ๋ณด์ ์์น ๋ฑ์ ๋ํ๋ผ ๊ฒ์ธ๊ฐ๊ฐ ์๊ฐํ๊ธฐ ์ข ๊น๋ค๋กญ๋ค
- ๊ตฌํํด์ผํ ์์๋ ํฌ๊ฒ ํ์ด์ด๋ณผ์ ์ด๋ / ํ์ด์ด๋ณผ์ ๋ณํฉ๊ณผ ๋ถ์ฐ์ด๋ค.
- ๋จผ์ ํ์ด์ด๋ณผ์ ์ด๋์ด๋ค.
- ํ์ด์ด์ด๋ณผ์ ์ด๋์ ๋ฆฌ์คํธ์ ๋ด์ ํ์ด์ด๋ณผ์ ์ ๋ณด๋ฅผ ํ๋์ฉ ๊บผ๋ด์ ๋น ๋ฆฌ์คํธ๋ฅผ ์์๋ก ๊ฐ์ง N x N ๋ฐฐ์ด(๋ฆฌ์คํธ๋ฅผ ์์๋ก ๊ฐ์ง๋ฏ๋ก3์ฐจ์ ๋ฐฐ์ด์ด๋ค)์ ํ์ํด์ฃผ์๋ค.
- 1๋ฒ ํ๊ณผ N๋ฒ ํ์ด, 1๋ฒ ์ด๊ณผ N๋ฒ ์ด์ด ์ฐ๊ฒฐ๋์ด ์์ผ๋ฏ๋ก N์ผ๋ก ๋๋ ๋๋จธ์ง๋ก ์ขํ๋ฅผ ํ์ํด์ผํ๋ค.
- ํ์ด์ด๋ณผ์ ๋ณํฉ๊ณผ ๋ถ์ฐ์ด๋ค.
- ํ์ด์ด๋ณผ์ ์ด๋์์ ํ๋ ๊ฑฐ์ ์ญ์์ผ๋ก ์ด๋ฒ์๋ ๋ฐฐ์ด์์ ํ์ด์ด๋ณผ์ ๊บผ๋ด์ ๋ค์ ๋ฆฌ์คํธ์ ๋ด์์ค๋ค.
- N x N ๋ฐฐ์ด์ ์์๊ฐ 2๊ฐ ์ด์ ์์ผ๋ฉด ๋ฌธ์ ์ ์ฃผ์ด์ง ์กฐ๊ฑด๋๋ก ๋ณํฉ๊ณผ ๋ถ์ฐ์ ๊ตฌํํด ์ํํด์ค๋ค.
- 1๊ฐ์ผ ๊ฒฝ์ฐ์๋ ๋ฐ๋ก ํ ๊ฒ ์์ผ๋ฏ๋ก ๊ทธ๋๋ก ๋ค์ ๋ฆฌ์คํธ์ ๋ด์์ค๋ค.
- ์ ๊ณผ์ ์ K ๋ฒ ์ํํ๊ณ ๋จ์์๋ ํ์ด์ด๋ณผ์ ์ง๋์ ํฉ์ ๊ตฌํด์ฃผ๋ฉด ๋๋ค.
- ๊ฐ์ธ์ ์ผ๋ก๋ ์๊ฐ๋ณด๋ค ๊ตฌํ์ด ๊น๋ค๋กญ๊ณ ์ ๊ฒฝ ์ธ ๊ฒ ๋ง์์ผ ํ๋ ๋ฌธ์ ์๋ค.
<์ ๋ต ์ฝ๋>
import sys, os, io, atexit
input = lambda: sys.stdin.readline().rstrip('\r\n')
stdout = io.BytesIO()
sys.stdout.write = lambda s: stdout.write(s.encode("ascii"))
atexit.register(lambda: os.write(1, stdout.getvalue()))
# 220921 20056 ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ
# ์ ๋ต์ฝ๋
# N: ๋ฐฐ์ด ํฌ๊ธฐ, M: ํ์ด์ด๋ณผ ๊ฐ์, K: ์ด๋ ํ์
N, M, K = map(int, input().split())
# fireball: ํ์ด์ด๋ณผ ๋ฆฌ์คํธ
fireball = []
for _ in range(M):
temp = list(map(int, input().split()))
# 0_index๋ก ์ ์ฅ
temp[0] -= 1
temp[1] -= 1
fireball.append(temp)
# arr: ํ์ด์ด๋ณผ์ด ์์นํ๋ ๋ฐฐ์ด
arr = [[[] for _ in range(N)] for _ in range(N)]
# direction: ์์น ์ ๋ณด
direction = {
0 : (-1, 0),
1 : (-1, 1),
2 : (0, 1),
3 : (1, 1),
4 : (1, 0),
5 : (1, -1),
6 : (0, -1),
7 : (-1, -1),
}
# move: ํ์ด์ด๋ณผ์ ์ด๋
def move():
# ๋ฆฌ์คํธ์์ ํ์ด์ด๋ณผ์ ํ๋์ฉ ๊บผ๋ด์ ๋ฐฐ์ด์ ์ด๋ ์์น๋ฅผ ํ์ํด์ค๋ค.
while fireball:
fireball_info = fireball.pop()
# ํ์ด์ด๋ณผ ์ด๋
fireball_info[0] = (fireball_info[0] + (fireball_info[3] * direction[fireball_info[4]][0])) % N
fireball_info[1] = (fireball_info[1] + (fireball_info[3] * direction[fireball_info[4]][1])) % N
# ๋ฒ ์ด์ ํ์ด์ด๋ณผ ํ์
arr[fireball_info[0]][fireball_info[1]].append([fireball_info[2], fireball_info[3], fireball_info[4]])
return
# merge_split: ํ์ด์ด๋ณผ์ด ํฉ์ณ์ก๋ค๊ฐ ๋๋์ด์ง๋ ํจ์
def merge_split():
for r in range(N):
for c in range(N):
# ๋ฐฐ์ด์ ๋์ผํ ์์น์ ํ์ด์ด๋ณผ์ด ๋ ๊ฐ ์ด์์ธ ๊ฒฝ์ฐ
if len(arr[r][c]) > 1:
# nm, ns: ๋๋์ด์ง ํ์ด์ด๋ณผ์ ์ง๋๊ณผ ์๋
nm = 0
ns = 0
# even, odd: ํฉ์ณ์ง๋ ํ์ด์ด๋ณผ์ ๋ฐฉํฅ์ ์ธ๋ ๋ณ์
even = 0
odd = 0
# cnt: ํฉ์ณ์ง๋ ํ์ด์ด๋ณผ์ ๊ฐ์
cnt = len(arr[r][c])
while arr[r][c]:
# ํ์ด์ด๋ณผ์ ์ง๋๊ณผ ์๋๋ฅผ ๋ํด์ค๋ค.
m, s, d = arr[r][c].pop()
nm += m
ns += s
# ํ์ด์ด ๋ณผ์ ๋ฐฉํฅ์ ์ธ์ค๋ค.
if d % 2:
odd += 1
else:
even += 1
# ํ์ด์ด๋ณผ์ ์ง๋์ด ๋จ์์๋ ๊ฒฝ์ฐ
if nm//5:
# ๋ชจ๋ ํ์์ด๊ฑฐ๋ ์ง์์ธ ๊ฒฝ์ฐ
if cnt == odd or cnt == even:
fireball.append([r, c, nm//5, ns//cnt, 0])
fireball.append([r, c, nm//5, ns//cnt, 2])
fireball.append([r, c, nm//5, ns//cnt, 4])
fireball.append([r, c, nm//5, ns//cnt, 6])
# ๋ค๋ฅธ ๋ฐฉํฅ์ด ์๋ ๊ฒฝ์ฐ
else:
fireball.append([r, c, nm//5, ns//cnt, 1])
fireball.append([r, c, nm//5, ns//cnt, 3])
fireball.append([r, c, nm//5, ns//cnt, 5])
fireball.append([r, c, nm//5, ns//cnt, 7])
# ๋ฐฐ์ด์ ์์น์ ํ์ด์ด๋ณผ์ด ํ๋๋ง ์กด์ฌํ๋ ๊ฒฝ์ฐ ๊ทธ๋๋ก ํ์ด์ด๋ณผ ๋ฆฌ์คํธ์ ๋ค์ ๋ฃ์ด์ค๋ค.
elif len(arr[r][c]) == 1:
fireball.append([r, c] + arr[r][c].pop())
# ์ํ์ K๋ฒ ๋ฐ๋ณตํ๋ค.
for _ in range(K):
move()
merge_split()
# ans: ๋จ์์๋ ํ์ด์ด๋ณผ ์ง๋์ ํฉ
ans = 0
# ๋จ์์๋ ํ์ด์ด๋ณผ ์ง๋์ ํฉ์ ๋ํด์ค๋ค.
for i in range(len(fireball)):
ans += fireball[i][2]
# ์ ๋ต ์ถ๋ ฅ
print(ans)
'โญ Problem_Solving > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 20057 ๋ง๋ฒ์ฌ ์์ด์ ํ ๋ค์ด๋ (Python/ํ์ด์ฌ) (0) | 2022.10.04 |
---|---|
[๋ฐฑ์ค] 2437 ์ ์ธ (Python/ํ์ด์ฌ) (1) | 2022.10.03 |
[๋ฐฑ์ค] 14621 ๋๋ง ์๋๋ ์ฐ์ (Python/ํ์ด์ฌ) (1) | 2022.10.02 |
[๋ฐฑ์ค] 16724 ํผ๋ฆฌ ๋ถ๋ ์ฌ๋์ด (Python/ํ์ด์ฌ) (0) | 2022.10.02 |
[๋ฐฑ์ค] 2146 ๋ค๋ฆฌ ๋ง๋ค๊ธฐ (Python/ํ์ด์ฌ) (0) | 2022.10.01 |
๋๊ธ