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

16-janghw0126 #66

merged 6 commits into from
Feb 21, 2024

Conversation

janghw0126
Copy link
Member

@janghw0126 janghw0126 commented Feb 15, 2024

πŸ”— 문제 링크

λ°±μ€€ | Nκ³Ό M (5)

문제 μ„€λͺ…

N개의 μžμ—°μˆ˜μ™€ μžμ—°μˆ˜ M이 μ£Όμ–΄μ‘Œμ„ λ•Œ, μ•„λž˜ 쑰건을 λ§Œμ‘±ν•˜λŠ” 길이가 M인 μˆ˜μ—΄μ„ λͺ¨λ‘ κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

μ œν•œ 쑰건

  • N개의 μžμ—°μˆ˜λŠ” λͺ¨λ‘ λ‹€λ₯Έ μˆ˜μ΄λ‹€.
  • μˆ˜μ—΄μ€ N개의 μžμ—°μˆ˜ μ€‘μ—μ„œ M개λ₯Ό κ³ λ₯Έ μˆ˜μ—΄μ΄λ‹€.

μž…λ ₯

  • 첫째 쀄에 μžμ—°μˆ˜ Nκ³Ό M이 주어진닀. (1 ≀ M ≀ N ≀ 8)
  • λ‘˜μ§Έ 쀄에 N개의 μˆ˜κ°€ 주어진닀. μž…λ ₯으둜 μ£Όμ–΄μ§€λŠ” μˆ˜λŠ” 10,000보닀 μž‘κ±°λ‚˜ 같은 μžμ—°μˆ˜μ΄λ‹€.

좜λ ₯

  • ν•œ 쀄에 ν•˜λ‚˜μ”© 문제의 쑰건을 λ§Œμ‘±ν•˜λŠ” μˆ˜μ—΄μ„ 좜λ ₯ν•œλ‹€. μ€‘λ³΅λ˜λŠ” μˆ˜μ—΄μ„ μ—¬λŸ¬ 번 좜λ ₯ν•˜λ©΄ μ•ˆλ˜λ©°, 각 μˆ˜μ—΄μ€ 곡백으둜 κ΅¬λΆ„ν•΄μ„œ 좜λ ₯ν•΄μ•Ό ν•œλ‹€
  • μˆ˜μ—΄μ€ 사전 순으둜 μ¦κ°€ν•˜λŠ” μˆœμ„œλ‘œ 좜λ ₯ν•΄μ•Ό ν•œλ‹€.

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

βœ…μ΄ 문제의 μ•„μ΄λ””μ–΄λŠ” 주어진 μˆ«μžλ“€μ„ λ°°μ—΄λ‘œ 받은 λ’€ μ •λ ¬ν•˜κ³ , μˆ˜μ—΄λ‘œ λ§Œλ“ λ‹€λŠ” 것이닀. κ·Έλž˜μ„œ λ°±νŠΈλž˜ν‚Ή μ•Œκ³ λ¦¬μ¦˜μ˜ μœ ν˜•μ— 맞게, λͺ¨λ“  경우의 μˆ˜μ—΄μ„ μž¬κ·€μ μœΌλ‘œ νƒμƒ‰ν•˜μ—¬ 좜λ ₯ν•˜μ˜€λ‹€. μ΄λ•Œ, 사전 μˆœμ„œλŒ€λ‘œ 결과값을 좜λ ₯ν•˜λŠ” 것이기 λ•Œλ¬Έμ— DFS λ°©μ‹μœΌλ‘œ 경우λ₯Ό νƒμƒ‰ν•˜λŠ” 것이 더 μ μ ˆν•œ 것 κ°™μ•˜λ‹€.

πŸ“–ν’€μ΄ 방법

  1. μžμ—°μˆ˜ Nκ³Ό M을 μž…λ ₯λ°›λŠ”λ‹€.
  2. μˆ«μžλ“€μ„ μž…λ ₯λ°›μ•„ λ¦¬μŠ€νŠΈμ— μ €μž₯ν•œλ‹€.
  3. μˆ«μžλ“€μ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.
  4. μž¬κ·€ ν•¨μˆ˜λ‘œ μ‚¬μš©ν•  backtracking(depth) ν•¨μˆ˜λ₯Ό μ„ μ–Έν•œλ‹€.
  5. ν˜„μž¬κΉŒμ§€ μ„ νƒν•œ 숫자의 κ°œμˆ˜κ°€ Mκ³Ό κ°™λ‹€λ©΄ λ°•μŠ€μ— μ €μž₯된 μˆ«μžλ“€μ„ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•˜μ—¬ 곡백을 κΈ°μ€€μœΌλ‘œ κ²°ν•©ν•˜μ—¬ 좜λ ₯ν•œλ‹€.
  6. 0λΆ€ν„° N-1κΉŒμ§€μ˜ μΈλ±μŠ€μ— λŒ€ν•΄ λ°˜λ³΅ν•œλ‹€.
  7. 이미 μ„ νƒλœ 숫자라면 κ±΄λ„ˆλ›΄λ‹€.
  8. 숫자λ₯Ό λ°•μŠ€μ— μΆ”κ°€ν•˜κ³  λ‹€μŒ 자릿수의 숫자λ₯Ό μ„ νƒν•˜κΈ° μœ„ν•΄ μž¬κ·€μ μœΌλ‘œ backtracking ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.
  9. ν•¨μˆ˜ 호좜 ν›„μ—λŠ” ν•΄λ‹Ή μžλ¦Ώμˆ˜μ—μ„œμ˜ 선택이 λλ‚¬μœΌλ―€λ‘œ, λ°•μŠ€μ—μ„œ λ§ˆμ§€λ§‰μ— μΆ”κ°€λœ 숫자λ₯Ό μ œκ±°ν•œλ‹€.
  10. ν•¨μˆ˜λ₯Ό λΉ μ Έλ‚˜μ™€ μ„ νƒν•œ μˆ«μžλ“€μ„ μ €μž₯ν•  리슀트λ₯Ό μ„ μ–Έν•œλ‹€.
  11. λ°±νŠΈλž˜ν‚Ή ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν•œλ‹€.

⬇️전체 μ½”λ“œ

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

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

.

Comment on lines +8 to +24
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()
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 λ‚¨κ²¨μ£Όμ‹œλŠλΌ κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€ !! β˜˜οΈπŸ‘

@tgyuuAn tgyuuAn removed the request for review from Hwangyerin February 20, 2024 10:22
Copy link
Member

@avocado-13 avocado-13 left a comment

Choose a reason for hiding this comment

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

ν˜œμ›λ‹˜κ³Ό μ •μ€λ‹˜μ˜ μ½”λ“œλ₯Ό 보며 λ‹€μ–‘ν•˜κ²Œ μ½”λ“œλ₯Ό 지 수 μžˆλ‹€λŠ” 것을 λ‹€μ‹œκΈˆ λŠλ‚λ‹ˆλ‹€πŸ€“
이번 μ°¨μ‹œ PR μž‘μ„±ν•˜μ‹œλŠλΌ 수고 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€ πŸ‘πŸ»πŸ€

@janghw0126 janghw0126 merged commit ce60728 into main Feb 21, 2024
@janghw0126 janghw0126 deleted the 16-janghw0126 branch July 17, 2024 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants