Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions learntosurf/Backtracking/2025-04-19-[BOJ]-#15649-N과M(1).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
N, M = map(int, input().split())
visited = [False] * (N + 1) # 1부터 N까지 사용 여부
result = []

def backtrack():
if len(result) == M:
print(' '.join(map(str, result)))
return

for i in range(1, N + 1):
if not visited[i]:
visited[i] = True
result.append(i)
backtrack()
result.pop() # 상태 복원
visited[i] = False # 방문 초기화

backtrack()
26 changes: 26 additions & 0 deletions learntosurf/Backtracking/2025-04-19-[BOJ]-#6603-로또.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys

def backtrack(S, path, start):
if len(path) == 6:
print(' '.join(map(str, path)))
return
for i in range(start, len(S)):
path.append(S[i])
backtrack(S, path, i + 1)
path.pop()

lines = sys.stdin.read().splitlines()
first_case = True

for line in lines:
if line == '0':
break
Comment on lines +12 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.stdin.read().splitlines()는 파이썬에서 표준 입력(stdin)으로부터 전체 내용을 읽고, 줄바꿈 문자를 기준으로 텍스트를 분리하여 리스트 형태로 반환합니다.

여러 줄 읽을때 항상 반복문안에 readline 함수써서 구현했는데 sys.stdin.read().splitlines()한번 쓰면 알아서 줄바꿈 기준으로 문자열 리스트를 만들어 주네요.
오늘 소소한 꿀팁 배워갑니다!


parts = list(map(int, line.strip().split()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sys.stdin.read().splitlines() 으로 여러 줄의 입력을 리스트 변수로 한번에 받을 수 있는건 처음 알았네요.
저도 다음에 꼭 사용해보겠습니다.

S = parts[1:]

if not first_case:
print()
first_case = False

backtrack(S, [], 0)