Baekjoon/백트래킹

[백준] 1476 날짜 계산 (실버5) / 백트래킹

hellosonic 2023. 4. 27. 13:17

문제요약

나의 코드 및 설명 01 - DFS

import sys
sys.setrecursionlimit(10**6)

def dfs(x,y,z, count):
    global ans
    if x == 16:
        x = 1
    if y == 29:
        y = 1
    if z == 20:
        z = 1 
    if x == e and y == s and m == z:
        ans = count
        return
    dfs(x+1, y+1, z+1, count+1)

e,s,m = map(int, input().split())
ans = 0
dfs(1,1,1,1)
print(ans)

나의 코드 및 설명 02- while문

e,s,m = map(int, input().split())

a,b,c = 1,1,1
ans = 1
while True:
    if a == e and b == s and c == m:
        break
    a += 1
    b += 1
    c += 1
    if a == 16:
        a = 1
    if b == 29:
        b = 1
    if c == 20:
        c = 1
    ans += 1

print(ans)

피드백

백트래킹 연습하다가 만난 문제. 단순히 반복문을 사용해서 풀 수도 있었지만 연습하는 것이 목적이니까 DFS로 풀어보았다.