Skip to content
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

16-janghw0126 #66

Merged
merged 6 commits into from
Feb 21, 2024
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
1 change: 1 addition & 0 deletions janghw0126/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
| 13μ°¨μ‹œ | 2024.2.7 | DFS | <a href= "https://www.acmicpc.net/problem/2606">λ°”μ΄λŸ¬μŠ€</a> |[#57](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/57) |
| 14μ°¨μ‹œ | 2024.2.12 | Backtracking | <a href= "https://www.acmicpc.net/problem/15649">Nκ³Ό M (1)</a> |[#61](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/61) |
| 15μ°¨μ‹œ | 2024.2.15 | Backtracking | <a href= "https://www.acmicpc.net/problem/15650">Nκ³Ό M (2)</a> |[#65](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/65) |
| 16μ°¨μ‹œ | 2024.2.18 | Backtracking | <a href= "https://www.acmicpc.net/problem/15654">Nκ³Ό M (5)</a> |[#66](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/66) |
---
2 changes: 1 addition & 1 deletion janghw0126/λ°±νŠΈλž˜ν‚Ή/Backtracking_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def solution(start):
#ν•¨μˆ˜ 호좜 ν›„μ—λŠ” ν•΄λ‹Ή μžλ¦Ώμˆ˜μ—μ„œμ˜ 선택이 λλ‚¬μœΌλ―€λ‘œ, result λ¦¬μŠ€νŠΈμ—μ„œ λ§ˆμ§€λ§‰μ— μΆ”κ°€λœ 숫자λ₯Ό μ œκ±°ν•œλ‹€.
result.pop()
#μœ„μ˜ 과정을 λ°˜λ³΅ν•˜μ—¬ λͺ¨λ“  경우의 수λ₯Ό μ°ΎλŠ”λ‹€.
solution(0)
solution(0)
28 changes: 28 additions & 0 deletions janghw0126/λ°±νŠΈλž˜ν‚Ή/Backtracking_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#μžμ—°μˆ˜ Nκ³Ό M을 μž…λ ₯λ°›λŠ”λ‹€.
N, M = map(int, input().split())
#μˆ«μžλ“€μ„ μž…λ ₯λ°›μ•„ λ¦¬μŠ€νŠΈμ— μ €μž₯ν•œλ‹€.
numbers = [int(x) for x in input().split()]
#μˆ«μžλ“€μ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
numbers.sort()

def backtracking(depth):
#ν˜„μž¬κΉŒμ§€ μ„ νƒν•œ 숫자의 κ°œμˆ˜κ°€ Mκ³Ό κ°™λ‹€λ©΄ μˆ˜ν–‰ν•  문이닀.
if depth == M:
#λ°•μŠ€μ— μ €μž₯된 μˆ«μžλ“€μ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 곡백을 κΈ°μ€€μœΌλ‘œ κ²°ν•©ν•˜μ—¬ 좜λ ₯ν•œλ‹€.
print(' '.join(map(str,box)))
return
#0λΆ€ν„° N-1κΉŒμ§€μ˜ μΈλ±μŠ€μ— λŒ€ν•΄ λ°˜λ³΅ν•œλ‹€.
for i in range(N):
#이미 μ„ νƒλœ 숫자라면 κ±΄λ„ˆλ›΄λ‹€.
if numbers[i] in box:
continue
#숫자λ₯Ό λ°•μŠ€μ— μΆ”κ°€ν•œλ‹€.
box.append(numbers[i])
#λ‹€μŒ 자릿수의 숫자λ₯Ό μ„ νƒν•˜κΈ° μœ„ν•΄ μž¬κ·€μ μœΌλ‘œ backtracking ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.
backtracking(depth + 1)
#ν•¨μˆ˜ 호좜 ν›„μ—λŠ” ν•΄λ‹Ή μžλ¦Ώμˆ˜μ—μ„œμ˜ 선택이 λλ‚¬μœΌλ―€λ‘œ, λ°•μŠ€μ—μ„œ λ§ˆμ§€λ§‰μ— μΆ”κ°€λœ 숫자λ₯Ό μ œκ±°ν•œλ‹€.
box.pop()
Comment on lines +8 to +24
Copy link
Collaborator

Choose a reason for hiding this comment

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

저도 같은 λ°©λ²•μœΌλ‘œ ν’€μ—ˆμ—ˆλ„€μš” γ…Žγ…Ž

input = open(0).readline
n,m = map(int,input().split())
nlist = sorted(list(map(int,input().split())))
result = []

def dfs(depth):

    if len(result) == m:
        return print(*result)

    for i in nlist:
        if i not in result:
            result.append(i)
            dfs(depth + 1)
            result.pop()
dfs(0)

μ΄λ ‡κ²Œ 리슀트 μš”μ†Œμ— λ°”λ‘œ μ ‘κ·Όν•΄μ„œ 숫자λ₯Ό μ €μž₯ν•˜κ³  좜λ ₯을 ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€ !

μ—¬ν–‰ μ€€λΉ„λ‘œ λ°”μ˜μ…¨μ„ν…λ° μ •μ„±μŠ€λŸ¬μš΄ PR λ‚¨κ²¨μ£Όμ‹œλŠλΌ κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€ !! β˜˜οΈπŸ‘

#μ„ νƒν•œ μˆ«μžλ“€μ„ μ €μž₯ν•  리슀트λ₯Ό μ„ μ–Έν•œλ‹€.
box = []
#λ°±νŠΈλž˜ν‚Ή ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•œλ‹€.
backtracking(0)