ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1676๋ฒ / ํฉํ ๋ฆฌ์ผ 0์ ๊ฐ์ [Go][Python3] (0) | 2020.11.04 |
---|---|
BOJ / 1932๋ฒ / ์ ์ ์ผ๊ฐํ [Python3] (0) | 2020.11.03 |
BOJ / 9461๋ฒ / ํ๋๋ฐ ์์ด [Go] [Python3] (0) | 2020.08.03 |
BOJ / 1904๋ฒ / 01ํ์ผ [Go] [Python3] (0) | 2020.08.02 |
BOJ / 1003๋ฒ / ํผ๋ณด๋์น ํจ์ [Go] (0) | 2020.08.01 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์คํ
- ์๊ฐ๊ต์ฒด
- ํ๋ก์ด๋์์ฌ
- Macbook pro 2012 mid 13
- dp
- python3
- ์๋ฐ
- ๋ธ๋ฃจํธํฌ์ค
- ๋ฐฑ์ค
- ํ
- ๋ชฝ๊ณ ๋๋น
- go
- ๋งฅ๋ถํ๋ก
- baekjoon
- ๋งฅ๋ถ
- ๋ฐ๋
- MongoDB
- BFS
- ballet
- ๋ถํ ์ ๋ณต
- AWS
- java
- Golang
- ํด์๋งต
- ์๊ณ ๋ฆฌ์ฆ
- BOJ
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์ด๋ถํ์
- Algorithm
- dfs
- Total
- Today
- Yesterday