Skip to content

Commit 387a593

Browse files
authored
Merge pull request #199 from AlgorithmStudy-Allumbus/learntosurf
Learntosurf / 4월 3주차 / 3문제
2 parents afbdb51 + 035af90 commit 387a593

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
N, M = map(int, input().split())
2+
visited = [False] * (N + 1) # 1부터 N까지 사용 여부
3+
result = []
4+
5+
def backtrack():
6+
if len(result) == M:
7+
print(' '.join(map(str, result)))
8+
return
9+
10+
for i in range(1, N + 1):
11+
if not visited[i]:
12+
visited[i] = True
13+
result.append(i)
14+
backtrack()
15+
result.pop() # 상태 복원
16+
visited[i] = False # 방문 초기화
17+
18+
backtrack()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
def backtrack(S, path, start):
4+
if len(path) == 6:
5+
print(' '.join(map(str, path)))
6+
return
7+
for i in range(start, len(S)):
8+
path.append(S[i])
9+
backtrack(S, path, i + 1)
10+
path.pop()
11+
12+
lines = sys.stdin.read().splitlines()
13+
first_case = True
14+
15+
for line in lines:
16+
if line == '0':
17+
break
18+
19+
parts = list(map(int, line.strip().split()))
20+
S = parts[1:]
21+
22+
if not first_case:
23+
print()
24+
first_case = False
25+
26+
backtrack(S, [], 0)

0 commit comments

Comments
 (0)