Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
BOJ #6603. 로또 (실버2)
https://www.acmicpc.net/problem/6603
유형: Graph, Backtracking
"""

# 토요일에 업로드 예정
21 changes: 21 additions & 0 deletions _WeeklyChallenges/W19-[Graph-Backtracking]/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 🚀4월 2주차 (4/14) 스터디 발제 주제: Graph (Backgracking)
> 발제자: 김민정 (@Mingguriguri)

> [!NOTE]
> 주제: Graph (Backgracking)

### 🗂️ 스터디 자료
- PDF: [바로가기
](Study_BOJ_1759.pdf)

<img width="500" alt="스터디문제" src="https://github.com/user-attachments/assets/80d0415b-034e-484e-849f-34457b8c543b" />
<img width="500" alt="발제문제" src="https://github.com/user-attachments/assets/e7136d73-179c-4540-8380-88181c5c3fd7" />


### 📖 문제
- [백준 #1759. 암호만들기](https://www.acmicpc.net/problem/1759): Graph (Backgracking) / 골드1
- 정답 코드: [Study_BOJ_1759_암호만들기.py](Study_BOJ_1759_암호만들기.py)

### 💻 과제
- [백준 #6603. 로또](https://www.acmicpc.net/problem/6603): Graph (Backgracking) / 실버2
- 정답 코드: [Assignment_BOJ_6603_로또.py](Assignment_BOJ_6603_로또.py)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
BOJ #1759. 암호 만들기 (골드5)
https://www.acmicpc.net/problem/1759
유형: Graph, Backtracking
"""

"""
풀이1: 백트래킹
"""
import sys
input = sys.stdin.readline

L, C = map(int, input().split()) # L: 암호 길이, C: 문자 종류
chars = sorted(input().split()) # 사전순 정렬
vowels = {'a', 'e', 'i', 'o', 'u'}


def is_valid(word):
# 최소 한 개의 모음과 최소 두 개의 자음으로 구성되어있는지 확인
vowel_cnt, consonant_cnt = 0, 0 # 모음 개수, 자음 개수
for w in word:
if w in vowels:
vowel_cnt += 1
else:
consonant_cnt += 1

return vowel_cnt >= 1 and consonant_cnt >= 2


def backtrack(word, start):
if len(word) == L: # 종료 조건
if is_valid(word):
print(''.join(word))
return

for i in range(start, C):
word.append(chars[i])
backtrack(word, i+1)
word.pop()

backtrack([], 0)


"""
풀이2: 조합
출처: https://velog.io/@dlgosla/%EB%B0%B1%EC%A4%80-BOJ-%EC%95%94%ED%98%B8-%EB%A7%8C%EB%93%A4%EA%B8%B01759-python
"""
from itertools import combinations

L, C = map(int, input().split())

alphabets = input().split()

# 길이가 L인 모든 조합, 증가하는 순서로 배열해야되기 때문에 sort 후 comb
alpha_combs = combinations(sorted(alphabets), L)

answer = []

for alpha_comb in alpha_combs: # 가능한 조합 중에서
consonant_count = 0
vowel_count = 0

# 자음 모음 개수 세기
for alpha in alpha_comb:
if alpha in "aeiou":
consonant_count += 1
else:
vowel_count += 1

# 모음이 1개 이상, 자음이 2 개 이상이면 출력
if consonant_count >= 1 and vowel_count >= 2:
print("".join(alpha_comb))

"""
풀이3: DFS 재귀 방식
출처: https://velog.io/@dlgosla/%EB%B0%B1%EC%A4%80-BOJ-%EC%95%94%ED%98%B8-%EB%A7%8C%EB%93%A4%EA%B8%B01759-python
"""
L, C = map(int, input().split())

alphabets = sorted(input().split())


def dfs(idx, codes):
if L == idx:
vowel_count = 0
consonant_count = 0

# 자음 모음 개수 세기
for code in codes:
if code in "aeiou":
consonant_count += 1
else:
vowel_count += 1

# 자음 2개 이상, 모음 한개 이상이면 암호가 될 수 있으므로 출력
if consonant_count >= 1 and vowel_count >= 2:
print("".join(codes))

else:
for i in range(idx, C):
if codes and alphabets[i] <= codes[-1]: # 오름차순 아니면 버림
continue
dfs(idx + 1, codes + [alphabets[i]])


dfs(0, [])