문제

 

참고한 코드
n,m = list(map(int,input().split()))
s = []
def dfs(start):
    if len(s)==m:
        print(' '.join(map(str,s)))
        return
    
    for i in range(start,n+1):
        if i not in s:
            s.append(i)
            dfs(i+1)
            s.pop()
dfs(1)

> 참고한 블로그

https://jiwon-coding.tistory.com/22

 

[백준] 15650번 N과 M(2) / 파이썬(python)

# 문제 링크 www.acmicpc.net/problem/15650 15650번: N과 M (2) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야

jiwon-coding.tistory.com

 

 

내가 해석한 코드 설명
n,m = list(map(int,input().split()))
s = []

> 여기느 N과 M (1) 과 동일하다.

 

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

> N과 M(1)과 달라진 점은 start 초기값이 생겼다는 것이다.

> 그 이유는 중복되는 값이 없어야될 뿐만 아니라, 오름차순으로 출력해야 되기 때문이다.

> 그러므로 start 변수가 for문에 range(start, n +1)로 들어감으로써 중복되는 것을 없애줄뿐만 아니라, 자신보다 작은 값은 포함을 시키지않게 된다!!

> 너무 어렵다 ㅜㅜ

+ Recent posts