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 5 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) |
---
37 changes: 21 additions & 16 deletions janghw0126/๋ฐฑํŠธ๋ž˜ํ‚น/Backtracking_2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import sys
input=sys.stdin.readline
#์ž์—ฐ์ˆ˜ N๊ณผ M์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
N,M=map(int,input().split())

def dfs(c):
if len(s) == m:
for j in s:
print(j, end=" ")
print()
return
for i in range(c, n + 1):
if i not in s:
s.append(i)
dfs(i)
s.pop()
#๋‹ต์„ ์ถœ๋ ฅํ•  result ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•œ๋‹ค.
result=[]

input = sys.stdin.readline
n, m = map(int, input().split())
s = []
a = []
dfs(1)
#๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•œ ํ•จ์ˆ˜ solution์„ ์ •์˜ํ•œ๋‹ค.
def solution(start):
#๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ๊ฐ€ M์ด๋ผ๋ฉด ๋ชจ๋“  ๊ณ ๋ฅธ ๊ฒƒ์ด ๋˜๋ฏ€๋กœ ํ•ด๋‹ต์ด ๋œ๋‹ค.
if len(result)==M:
#๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ํ›„ ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฒฐํ•ฉํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
return print(' '.join(map(str,result)))
#์ž๋ฆฟ์ˆ˜๊ฐ€ ์•„์ง ์•ˆ์ฐผ์œผ๋ฉด ์ˆ˜ํ–‰ํ•  ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.
for i in range(start,N):
#result๋ฆฌ์ŠคํŠธ์— ํ•ด๋‹น ์ˆซ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
result.append(i+1)
#๋‹ค์Œ ์ž๋ฆฟ์ˆ˜์˜ ์ˆซ์ž๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•ด ์žฌ๊ท€์ ์œผ๋กœ solutionํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
solution(i+1)
#ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„์—๋Š” ํ•ด๋‹น ์ž๋ฆฟ์ˆ˜์—์„œ์˜ ์„ ํƒ์ด ๋๋‚ฌ์œผ๋ฏ€๋กœ, result ๋ฆฌ์ŠคํŠธ์—์„œ ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€๋œ ์ˆซ์ž๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
result.pop()
#์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ๋Š”๋‹ค.
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)