ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ 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
- ๋ฐฑ์ค
- ์๋ฐ
- ๋ถํ ์ ๋ณต
- ๋ฐ๋
- MongoDB
- ๋ธ๋ฃจํธํฌ์ค
- ๋งฅ๋ถ
- dp
- ์คํ
- BOJ
- BFS
- ํ
- Macbook pro 2012 mid 13
- python3
- dfs
- Golang
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์๊ฐ๊ต์ฒด
- java
- ์ด๋ถํ์
- ballet
- ํด์๋งต
- ๋งฅ๋ถํ๋ก
- AWS
- Algorithm
- ์๊ณ ๋ฆฌ์ฆ
- go
- baekjoon
- ํ๋ก์ด๋์์ฌ
- ๋ชฝ๊ณ ๋๋น
- Total
- Today
- Yesterday