From 71a8185cbacc95905f2ac7ad59b5dc65c984123d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=ED=98=9C=EC=9B=90?= <127714800+janghw0126@users.noreply.github.com> Date: Thu, 15 Feb 2024 23:43:46 +0900 Subject: [PATCH 1/3] 2024-02-15 --- janghw0126/README.md | 1 + .../Backtracking_2.py" | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 "janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" diff --git a/janghw0126/README.md b/janghw0126/README.md index 8f1a460e..0505a6f5 100644 --- a/janghw0126/README.md +++ b/janghw0126/README.md @@ -16,4 +16,5 @@ | 12차시 | 2024.2.4 | DFS | 빵집 |[#53](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/53) | | 13차시 | 2024.2.7 | DFS | 바이러스 |[#57](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/57) | | 14차시 | 2024.2.12 | Backtracking | N과 M (1) |[#61](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/61) | +| 15차시 | 2024.2.15 | Backtracking | N과 M (2) |[#65](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/65) | --- diff --git "a/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" "b/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" new file mode 100644 index 00000000..6852843b --- /dev/null +++ "b/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" @@ -0,0 +1,16 @@ +N,M=map(int,input().split()) +lst=[] + +def dfs(start): + #탈출 조건에 해당한다. + if len(lst)==M: + print(''.join(map(str,lst))) + return + + for i in range(start,N+1): + if i not in lst: + lst.append(i) + dfs(i+1) + lst.pop() + +dfs(1) \ No newline at end of file From c8b0f04aa16f7a6e7c96678b657cffa07d9c8d92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=ED=98=9C=EC=9B=90?= <127714800+janghw0126@users.noreply.github.com> Date: Thu, 15 Feb 2024 23:49:29 +0900 Subject: [PATCH 2/3] 2024-02-15 --- .../Backtracking_2.py" | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git "a/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" "b/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" index 6852843b..274677f9 100644 --- "a/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" +++ "b/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" @@ -1,16 +1,19 @@ -N,M=map(int,input().split()) -lst=[] +import sys -def dfs(start): - #탈출 조건에 해당한다. - if len(lst)==M: - print(''.join(map(str,lst))) - return - - for i in range(start,N+1): - if i not in lst: - lst.append(i) - dfs(i+1) - lst.pop() +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() +input = sys.stdin.readline +n, m = map(int, input().split()) +s = [] +a = [] dfs(1) \ No newline at end of file From e963a243ea28161e51dcd6414109606a418cecfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=ED=98=9C=EC=9B=90?= <127714800+janghw0126@users.noreply.github.com> Date: Fri, 16 Feb 2024 09:04:26 +0900 Subject: [PATCH 3/3] 2024-02-15 --- .../Backtracking_2.py" | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git "a/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" "b/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" index 274677f9..2e5abbcb 100644 --- "a/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" +++ "b/janghw0126/\353\260\261\355\212\270\353\236\230\355\202\271/Backtracking_2.py" @@ -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) \ No newline at end of file +#문제 해결을 위한 함수 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) \ No newline at end of file