백준_N과 M(1~4)_(백트래킹)

맹민재·2023년 4월 2일
0

알고리즘

목록 보기
18/134
def dfs1(n, m):
    if len(s) == m:
        print(" ".join(map(str, s)))
        return
    for i in range(1, n+1):
        if i not in s:
            s.append(i)
            dfs1(n, m)
            s.pop()
def dfs2(n, m, x):
    if len(s) == m:
        print(" ".join(map(str, s)))
        return
    for i in range(x, n+1):
        if i not in s:
            s.append(i)
            dfs2(n, m, i+1)
            s.pop()

def dfs3(n, m):
    if len(s) == m:
        print(" ".join(map(str, s)))
        return
    for i in range(1, n+1):
        s.append(i)
        dfs3(n, m)
        s.pop()

def dfs4(n, m, x):
    if len(s) == m:
        print(" ".join(map(str, s)))
        return
    for i in range(x, n+1):
        s.append(i)
        dfs4(n, m, i)
        s.pop()
n, m = [int(v) for v in input().split()]
s = []
# dfs1(n, m)
# dfs2(n, m, 1)
# dfs3(n,m)
dfs4(n,m, 1)

백트래킹 맛보기 문제들!!

profile
ㄱH ㅂrㄹ ㅈr

0개의 댓글