-
Notifications
You must be signed in to change notification settings - Fork 5
Learntosurf / 4월 3주차 / 3문제 #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
learntosurf/Backtracking/2025-04-19-[BOJ]-#15649-N과M(1).py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| parts = list(map(int, line.strip().split())) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| S = parts[1:] | ||
|
|
||
| if not first_case: | ||
| print() | ||
| first_case = False | ||
|
|
||
| backtrack(S, [], 0) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()한번 쓰면 알아서 줄바꿈 기준으로 문자열 리스트를 만들어 주네요.
오늘 소소한 꿀팁 배워갑니다!