Skip to content

Commit 5fef83c

Browse files
committed
N과 M(12) 풀이
1 parent 60e16b6 commit 5fef83c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

BAEKJOON/2Silver/N과 M(12).py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# https://www.acmicpc.net/problem/15666
3+
4+
"""
5+
N개의 자연수와 자연수 M이 주어졌을 때, 아래 조건을 만족하는 길이가 M인 수열을 모두 구하는 프로그램을 작성하시오.
6+
7+
N개의 자연수 중에서 M개를 고른 수열
8+
- 같은 수를 여러 번 골라도 된다.
9+
- 고른 수열은 비내림차순이어야 한다.
10+
- 길이가 K인 수열 A가 A1 ≤ A2 ≤ ... ≤ AK-1 ≤ AK를 만족하면, 비내림차순이라고 한다.
11+
"""
12+
13+
def bfs(N, M, array):
14+
visit = {}
15+
queue = [(0, 0, "")]
16+
17+
while queue:
18+
idx, dept, string = queue.pop(0)
19+
20+
if dept == M:
21+
visit[string] = 0
22+
print(string.lstrip())
23+
continue
24+
25+
for i in range(idx, N):
26+
res = f"{string} {array[i]}"
27+
if visit.get(res) is None:
28+
visit[res] = 0
29+
queue.append((i, dept+1, res))
30+
31+
if __name__ == "__main__":
32+
N, M = map(int, input().split())
33+
array = sorted(list(map(int, input().split())))
34+
bfs(N, M, array)

0 commit comments

Comments
 (0)