https://khanwol./9
(백준 BOJ 15640 / Python / Python) N과 M (1)
즉시 떠오르는 알고리즘은 그 자체로 bfs 역추적입니다.
bf(m)을 정의하고 누락된 값을 bf(m-1)의 목록에 알파벳순으로 추가합니다.
import sys def bf(m): 전역 n if m == 1: return list(range(1, n+1)) r
khanwol.
잘. 처음에는 1부터 N까지입니다.
주어진 값으로 같은 계산을 하라고 합니다.
입력 값을 각각 취하여 목록을 교체하십시오!
끝
import sys
def bf(m):
global n, list_n
if m == 1:
return list_n(:)
result = ()
for i in bf(m-1):
copy_n_list = list_n(:)
if type(i) == int:
i = (i)
if type(i) == list:
copy_n_list = list(set(copy_n_list) - set(i))
copy_n_list.sort()
for j in copy_n_list:
append_list = i.copy()
append_list.append(j)
result.append(append_list)
return result
n, m = map(int, sys.stdin.readline().strip().split(' '))
list_n = list(map(int, sys.stdin.readline().strip().split(' ')))
list_n.sort()
if m == 1:
strs = list(map(str, bf(m)))
print('\n'.join(strs))
else:
strs = bf(m)
for i in strs:
i = list(map(str, i))
print(' '.join(i))