[백준] 21317 징검다리 건너기
징검다리 건너기
코드
import sys
sys.setrecursionlimit(10 ** 6)
def dfs(stone, bcount, cost):
if stone == n - 1:
dp[stone] = min(dp[stone], cost)
return
# 작은 점프, 큰 점프
for gap in range(1, 3):
next_stone = stone + gap
new_cost = cost + jdata[stone][gap - 1]
if 0 <= next_stone < n:
dfs(next_stone, bcount, new_cost)
# 매우 큰 점프
if bcount == 1:
next_stone = stone + 3
new_cost = cost + k
if 0 <= next_stone < n:
dfs(next_stone, 0, new_cost)
n = int(input())
jdata = [list(map(int, input().split())) for _ in range(n - 1)]
k = int(input())
inf = float('inf')
dp = [inf for _ in range(n)]
dp[0] = 0
dfs(0, 1, 0)
print(dp[-1])
틀린 코드
import sys
sys.setrecursionlimit(10 ** 6)
def dfs(stone, bcount, cost):
if stone == n - 1:
dp[stone] = min(dp[stone], cost)
return
# 작은 점프, 큰 점프
for gap in range(1, 3):
next_stone = stone + gap
new_cost = cost + jdata[stone][gap - 1]
if 0 <= next_stone < n and dp[next_stone] > new_cost:
dp[next_stone] = new_cost
dfs(next_stone, bcount, new_cost)
# 매우 큰 점프
if bcount == 1:
next_stone = stone + 3
new_cost = cost + k
if 0 <= next_stone < n and dp[next_stone] > new_cost:
dp[next_stone] = new_cost
dfs(next_stone, 0, new_cost)
n = int(input())
jdata = [list(map(int, input().split())) for _ in range(n - 1)]
k = int(input())
inf = float('inf')
dp = [inf for _ in range(n)]
dp[0] = 0
dfs(0, 1, 0)
print(dp[-1])
틀린 이유
# 틀린 코드
if 0 <= next_stone < n and dp[next_stone] > new_cost:
dp[next_stone] = new_cost
dfs(next_stone, bcount, new_cost)
# 맞은 코드
if 0 <= next_stone < n:
dfs(next_stone, bcount, new_cost)
import sys
sys.setrecursionlimit(10 ** 6)
def dfs(stone, bcount, cost):
if stone == n - 1:
dp[stone] = min(dp[stone], cost)
return
# 작은 점프, 큰 점프
for gap in range(1, 3):
next_stone = stone + gap
new_cost = cost + jdata[stone][gap - 1]
if 0 <= next_stone < n:
dfs(next_stone, bcount, new_cost)
# 매우 큰 점프
if bcount == 1:
next_stone = stone + 3
new_cost = cost + k
if 0 <= next_stone < n:
dfs(next_stone, 0, new_cost)
n = int(input())
jdata = [list(map(int, input().split())) for _ in range(n - 1)]
k = int(input())
inf = float('inf')
dp = [inf for _ in range(n)]
dp[0] = 0
dfs(0, 1, 0)
print(dp[-1])
import sys
sys.setrecursionlimit(10 ** 6)
def dfs(stone, bcount, cost):
if stone == n - 1:
dp[stone] = min(dp[stone], cost)
return
# 작은 점프, 큰 점프
for gap in range(1, 3):
next_stone = stone + gap
new_cost = cost + jdata[stone][gap - 1]
if 0 <= next_stone < n and dp[next_stone] > new_cost:
dp[next_stone] = new_cost
dfs(next_stone, bcount, new_cost)
# 매우 큰 점프
if bcount == 1:
next_stone = stone + 3
new_cost = cost + k
if 0 <= next_stone < n and dp[next_stone] > new_cost:
dp[next_stone] = new_cost
dfs(next_stone, 0, new_cost)
n = int(input())
jdata = [list(map(int, input().split())) for _ in range(n - 1)]
k = int(input())
inf = float('inf')
dp = [inf for _ in range(n)]
dp[0] = 0
dfs(0, 1, 0)
print(dp[-1])
틀린 이유
# 틀린 코드
if 0 <= next_stone < n and dp[next_stone] > new_cost:
dp[next_stone] = new_cost
dfs(next_stone, bcount, new_cost)
# 맞은 코드
if 0 <= next_stone < n:
dfs(next_stone, bcount, new_cost)
왜???
Author And Source
이 문제에 관하여([백준] 21317 징검다리 건너기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@guswns3371/백준-21317-징검다리-건너기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)