ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

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

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

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	cost := [][]int{}
	for i := 0; i < n; i++ {
		var a, b, c int
		fmt.Fscanln(reader, &a, &b, &c)
		cost = append(cost, []int{a, b, c})
	}
	minVal := calculate(cost, n)
	fmt.Fprintln(writer, minVal)
}

func calculate(cost [][]int, n int) (minVal int) {
	minVal = 0
	if n == 1 {
		minVal = int(math.Min(math.Min(float64(cost[0][0]), float64(cost[0][1])), float64(cost[0][2])))
	}

	minCosts := [][]int{}
	minCosts = append(minCosts, []int{cost[0][0], cost[0][1], cost[0][2]})

	for i := 1; i < n; i++ {
		values := []int{
			int(math.Min(float64(minCosts[i-1][1]), float64(minCosts[i-1][2]))) + cost[i][0],
			int(math.Min(float64(minCosts[i-1][0]), float64(minCosts[i-1][2]))) + cost[i][1],
			int(math.Min(float64(minCosts[i-1][0]), float64(minCosts[i-1][1]))) + cost[i][2],
		}
		minCosts = append(minCosts, values)
	}

	minVal = int(math.Min(math.Min(float64(minCosts[n-1][0]), float64(minCosts[n-1][1])), float64(minCosts[n-1][2])))
	return
}

๐ŸŽจ Python3

import sys

def calculate(n):
    min_val = 0
    if n == 1:
        min_val = min(cost[0][0], cost[0][1], cost[0][2])
    min_costs = [[cost[0][0], cost[0][1], cost[0][2]]]
    for i in range(1, n):
        min_costs.append([min(min_costs[i-1][1], min_costs[i-1][2])+cost[i][0],
                          min(min_costs[i-1][0], min_costs[i-1][2])+cost[i][1],
                          min(min_costs[i-1][0], min_costs[i-1][1])+cost[i][2]])
    min_val = min(min_costs[n-1][0], min_costs[n-1][1], min_costs[n-1][2])
    return min_val

if __name__ == "__main__":
    n = int(sys.stdin.readline())

    cost = []
    for i in range(n):
        a, b, c = map(int, sys.stdin.readline().split())
        cost.append([a, b, c])

    min_val = calculate(n)
    print(min_val)
728x90
๋Œ“๊ธ€