๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 2096๋ฒ
ํ์ด ๊ณผ์
์ ํ์ ์ธ DP
๋ฌธ์ ์ด์ง๋ง ๋ฉ๋ชจ๋ฆฌ ์ ํ์ด ๋นก์
๋๋ค.์ฌ๋ผ์ด๋ฉ ์๋์ฐ
๋ฅผ ํ์ฉํด์ memo
2์ฐจ์ ๋ฐฐ์ด์ 2 * 3
์ ํฌ๊ธฐ๋ก ์ง์ ํด์ ๊ฐ์ ๊ตฌํ๋ฉด ๋๋๋ฐ
๋ฌธ์ ๋ ์ฒ์์ top-down
๋ฐฉ์์ผ๋ก ์ ๊ทผํ๋ค๊ฐ board
๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ์ค์ผ ๋ฐฉ๋ฒ์ด ์์ด์ ๊ณ์ ๋ฉ๋ชจ๋ฆฌ ์ด๊ณผ๊ฐ ๋ฐ์ํ์ต๋๋ค.
๋์ ์ bottom-up
๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝํ๊ณ board
๋ฅผ ๋ฏธ๋ฆฌ ์
๋ ฅ ๋ฐ๋ ๊ฒ์ด ์๋๋ผ
ํ ์ค์ฉ ์
๋ ฅ ๋ฐ์์ ์ฌ์ฉํ๋ ๊ฒ์ผ๋ก ๋ณ๊ฒฝํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์์ต๋๋ค.
์ฝ๋
import sys
N = int(input())
def solution():
min_memo = [[-1] * 3 for _ in range(2)]
max_memo = [[-1] * 3 for _ in range(2)]
board = list(map(int, sys.stdin.readline().split()))
for i in range(3):
min_memo[0][i] = board[i]
max_memo[0][i] = board[i]
for x in range(1, N):
board = list(map(int, sys.stdin.readline().split()))
min_memo[x % 2][0] = min(min_memo[(x - 1) % 2][0], min_memo[(x - 1) % 2][1]) + board[0]
min_memo[x % 2][1] = min(min_memo[(x - 1) % 2][0], min_memo[(x - 1) % 2][1], min_memo[(x - 1) % 2][2]) + board[1]
min_memo[x % 2][2] = min(min_memo[(x - 1) % 2][1], min_memo[(x - 1) % 2][2]) + board[2]
max_memo[x % 2][0] = max(max_memo[(x - 1) % 2][0], max_memo[(x - 1) % 2][1]) + board[0]
max_memo[x % 2][1] = max(max_memo[(x - 1) % 2][0], max_memo[(x - 1) % 2][1], max_memo[(x - 1) % 2][2]) + board[1]
max_memo[x % 2][2] = max(max_memo[(x - 1) % 2][1], max_memo[(x - 1) % 2][2]) + board[2]
max_value = max(max_memo[(N - 1) % 2])
min_value = min(min_memo[(N - 1) % 2])
return f"{max_value} {min_value}"
print(solution())
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 18352 - ํน์ ๊ฑฐ๋ฆฌ์ ๋์ ์ฐพ๊ธฐ (0) | 2021.03.18 |
---|---|
BOJ 16173 - ์ ํ์ ์ฉฐ๋ฆฌ(small) (0) | 2021.03.18 |
BOJ 14716 - ํ์๋ง (0) | 2021.03.18 |
BOJ 14923 - ๋ฏธ๋ก ํ์ถ (0) | 2021.03.18 |
BOJ 17406 - ๋ฐฐ์ด ๋๋ฆฌ๊ธฐ 4 (0) | 2021.03.18 |
๐ฌ ๋๊ธ