์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step15] 1149 : RGB๊ฑฐ๋ฆฌ (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 25. 16:23

www.acmicpc.net/problem/1149

 

1149๋ฒˆ: RGB๊ฑฐ๋ฆฌ

์ฒซ์งธ ์ค„์— ์ง‘์˜ ์ˆ˜ N(2 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜

www.acmicpc.net


์ฒ˜์Œ์—๋Š” RGB ๊ฐ๊ฐ ์‹œ์ž‘ํ•ด์„œ 3๊ฐ€์ง€ ๋ฃจํŠธ๋ฅผ ๊ณ„์‚ฐํ•ด์„œ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ์Œ

๊ฐ๊ฐ ์ถœ๋ฐœํ•ด์„œ RGB์ค‘

์ด์ „ ์œ„์น˜ ์ œ์™ธํ•˜๊ณ  ๋‚จ์€ ๋‘˜ ์ค‘ ์ž‘์€๊ฐ’์ด๋ž‘ ์ž‘์€๊ฐ’ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ™์ด ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ

์ตœ์ข… ๊ฐ’ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ฒŒ ํ’€์—ˆ์Œ

 

ํ•˜ ์˜ˆ์ œ๋„ ๋ญ๋„ ๋‹ค ์ž˜ ๋‚˜์˜ค๋Š”๋ฐ ํ‹€๋ ธ๋‹ค๊ณ  ํ•ด์„œ ๋Œ€์ฒด ๋ญ๊ฐ€ ํ‹€๋ฆฐ๊ฑด์ง€

๋ฐ˜๋ก€ ๋‹ค ์ฐพ์•„์„œ ์ž…๋ ฅํ•ด๋ดค๋Š”๋ฐ ๋‹ค ๋งž๋Š”๊ฑฐ์•ผ ์  ์žฅ

๊ทธ๋ž˜์„œ ๋’ค์ง€๊ณ  ๋’ค์ ธ์„œ ๋ฐ˜๋ก€ ์ฐพ์•˜๋Š”๋ฐ ๊ฐ™์€ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด ๋‘˜ ์ค‘ ๋ญ๊ฐ€ ์ž‘์€์ง€ ๊ตฌ๋ถ„์„ ๋ชปํ•œ๋‹ค๋Š”๊ฒŒ ๋ฌธ์ œ์˜€์Œ

 

๊ทธ๋ž˜์„œ ์ด๊ฑธ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋‚˜ ํ•œ์ฐธ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๊ตฌ๊ธ€๋ง ํ–ˆ๋Š”๋ฐ

์‚ฌ๋žŒ๋“ค์ด ํ•˜๋‚˜๊ฐ™์ด ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ๊ณ  ๋‚˜๋Š” ๊ทธ ๋ฐฉ๋ฒ•์„ ์ดํ•ด ๋ชปํ•จ...

 

1. ๊ฐ ๋น„์šฉ์„ ๋ฆฌ์ŠคํŠธ house์— ์ €์žฅ

2. ๋‘๋ฒˆ์งธ R = (์ฒซ๋ฒˆ์งธ G, ์ฒซ๋ฒˆ์งธ B ์ค‘ ์ž‘์€ ๊ฐ’) + ๋‘๋ฒˆ์งธ R ๋น„์šฉ

    ๋‘๋ฒˆ์งธ G = (์ฒซ๋ฒˆ์งธ R, ์ฒซ๋ฒˆ์งธ B ์ค‘ ์ž‘์€ ๊ฐ’) + ๋‘๋ฒˆ์งธ G ๋น„์šฉ

    ๋‘๋ฒˆ์งธ B = (์ฒซ๋ฒˆ์งธ R, ์ฒซ๋ฒˆ์งธ G ์ค‘ ์ž‘์€ ๊ฐ’) + ๋‘๋ฒˆ์งธ B ๋น„์šฉ

3. n๋ฒˆ์งธ R = (n-1๋ฒˆ์งธ ์ตœ์ข… G, n-1๋ฒˆ์งธ ์ตœ์ข… B ์ค‘ ์ž‘์€ ๊ฐ’) + n๋ฒˆ์งธ R ๋น„์šฉ

    n๋ฒˆ์งธ G = (n-1๋ฒˆ์งธ ์ตœ์ข… R, n-1๋ฒˆ์งธ ์ตœ์ข… B ์ค‘ ์ž‘์€ ๊ฐ’) + n๋ฒˆ์งธ G ๋น„์šฉ

    n๋ฒˆ์งธ B = (n-1๋ฒˆ์งธ ์ตœ์ข… R, n-1๋ฒˆ์งธ ์ตœ์ข… G ์ค‘ ์ž‘์€ ๊ฐ’) + n๋ฒˆ์งธ B ๋น„์šฉ

 

2๋ฒˆ์€ ์ดํ•ด๋ฅผ ํ–ˆ๋‹ค ์ด๊ฑฐ์•ผ n๋ฒˆ์งธ ๊ฐ’ ๊ตฌํ•˜๋ ค๋ฉด n-1๋ฒˆ์งธ ๋‘๊ฐœ ๋น„๊ตํ•ด์„œ ์ž‘์€ ๊ฐ’ ๋”ํ•˜๋Š”๊ฑฐ

๊ทผ๋ฐ ๋งŒ์•ฝ์— n๋ฒˆ์งธ R ๊ตฌํ•˜๋Š”๋ฐ n-1๋ฒˆ์งธ G๊ฐ€ B๋ณด๋‹ค ์ž‘์•„ ๊ทธ๋Ÿผ n-1๋ฒˆ์งธ ์ตœ์ข… G๋ฅผ ๋”ํ•ด์•ผ ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€???

์™œ ์ตœ์ข… ๊ฐ’์„ ๋น„๊ตํ•˜์ง€??? ๋Œ€์ฒด ์™œ??

 

์ตœ์ข… ๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด ์•ˆ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€

์ตœ์ข…์€ ํฌ๋”๋ผ๋„ ๋‹จ์ผ ๊ฐ’์€ ์ž‘์„ ์ˆ˜ ์žˆ์ž–์•„ ใ…œ

์ œ์ผ ์ž‘์€๊ฑฐ ๊ณ ๋ฅด๋Š”๊ฑฐ ์•„๋‹ˆ์•ผ?ใ…œใ…œ ์ตœ

์ข…๋งŒ ์ž‘์œผ๋ฉด ๋˜์„œ ์ƒ๊ด€์—†๋Š”๊ฑฐ์•ผ??

์ง„์งœ ์•„๋ฌด๋ฆฌ ์ณ๋‹ค๋ด๋„ ์™œ์ธ์ง€ ๋ชจ๋ฅด๊ฒ ์Œ ํ•˜ ์„ค๋ช…ํ•ด์ค„์‚ฌ๋žŒ?

 

# 1149.py

n = int(input())

house = [[0] * 3 for i in range(n)]
for i in range(n):
    house[i][0], house[i][1], house[i][2] = map(int, input().split())

for i in range(1, n):
    house[i][0] = house[i][0] + min(house[i-1][1], house[i-1][2])
    house[i][1] = house[i][1] + min(house[i-1][0], house[i-1][2])
    house[i][2] = house[i][2] + min(house[i-1][0], house[i-1][1])

print(min(house[n-1]))

 

 

+ ๋’ค์— ๋ฌธ์ œ ๊ณ„์† ํ’€๋‹ค๋ณด๋‹ˆ ์ดํ•ดํ•จ.. ๊ฑ ๋‚ด๊ฐ€ DP์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•œ์ฐธ ๋ชจ์ž๋ž€๋“ฏ ใ…œ