Skip to content

Commit d043f27

Browse files
authored
Merge pull request #192 from AlgorithmStudy-Allumbus/minjeong3
feat: 4์›” 2์ฃผ์ฐจ ๋ฐœ์ œ ์ž๋ฃŒ ์—…๋กœ๋“œ
2 parents 35bf3d2 + bcd4d65 commit d043f27

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
BOJ #6603. ๋กœ๋˜ (์‹ค๋ฒ„2)
3+
https://www.acmicpc.net/problem/6603
4+
์œ ํ˜•: Graph, Backtracking
5+
"""
6+
7+
# ํ† ์š”์ผ์— ์—…๋กœ๋“œ ์˜ˆ์ •
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## ๐Ÿš€4์›” 2์ฃผ์ฐจ (4/14) ์Šคํ„ฐ๋”” ๋ฐœ์ œ ์ฃผ์ œ: Graph (Backgracking)
2+
> ๋ฐœ์ œ์ž: ๊น€๋ฏผ์ • (@Mingguriguri)
3+
4+
> [!NOTE]
5+
> ์ฃผ์ œ: Graph (Backgracking)
6+
7+
### ๐Ÿ—‚๏ธ ์Šคํ„ฐ๋”” ์ž๋ฃŒ
8+
- PDF: [๋ฐ”๋กœ๊ฐ€๊ธฐ
9+
](Study_BOJ_1759.pdf)
10+
11+
<img width="500" alt="์Šคํ„ฐ๋””๋ฌธ์ œ" src="https://github.com/user-attachments/assets/80d0415b-034e-484e-849f-34457b8c543b" />
12+
<img width="500" alt="๋ฐœ์ œ๋ฌธ์ œ" src="https://github.com/user-attachments/assets/e7136d73-179c-4540-8380-88181c5c3fd7" />
13+
14+
15+
### ๐Ÿ“– ๋ฌธ์ œ
16+
- [๋ฐฑ์ค€ #1759. ์•”ํ˜ธ๋งŒ๋“ค๊ธฐ](https://www.acmicpc.net/problem/1759): Graph (Backgracking) / ๊ณจ๋“œ1
17+
- ์ •๋‹ต ์ฝ”๋“œ: [Study_BOJ_1759_์•”ํ˜ธ๋งŒ๋“ค๊ธฐ.py](Study_BOJ_1759_์•”ํ˜ธ๋งŒ๋“ค๊ธฐ.py)
18+
19+
### ๐Ÿ’ป ๊ณผ์ œ
20+
- [๋ฐฑ์ค€ #6603. ๋กœ๋˜](https://www.acmicpc.net/problem/6603): Graph (Backgracking) / ์‹ค๋ฒ„2
21+
- ์ •๋‹ต ์ฝ”๋“œ: [Assignment_BOJ_6603_๋กœ๋˜.py](Assignment_BOJ_6603_๋กœ๋˜.py)
Binary file not shown.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
BOJ #1759. ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ (๊ณจ๋“œ5)
3+
https://www.acmicpc.net/problem/1759
4+
์œ ํ˜•: Graph, Backtracking
5+
"""
6+
7+
"""
8+
ํ’€์ด1: ๋ฐฑํŠธ๋ž˜ํ‚น
9+
"""
10+
import sys
11+
input = sys.stdin.readline
12+
13+
L, C = map(int, input().split()) # L: ์•”ํ˜ธ ๊ธธ์ด, C: ๋ฌธ์ž ์ข…๋ฅ˜
14+
chars = sorted(input().split()) # ์‚ฌ์ „์ˆœ ์ •๋ ฌ
15+
vowels = {'a', 'e', 'i', 'o', 'u'}
16+
17+
18+
def is_valid(word):
19+
# ์ตœ์†Œ ํ•œ ๊ฐœ์˜ ๋ชจ์Œ๊ณผ ์ตœ์†Œ ๋‘ ๊ฐœ์˜ ์ž์Œ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธ
20+
vowel_cnt, consonant_cnt = 0, 0 # ๋ชจ์Œ ๊ฐœ์ˆ˜, ์ž์Œ ๊ฐœ์ˆ˜
21+
for w in word:
22+
if w in vowels:
23+
vowel_cnt += 1
24+
else:
25+
consonant_cnt += 1
26+
27+
return vowel_cnt >= 1 and consonant_cnt >= 2
28+
29+
30+
def backtrack(word, start):
31+
if len(word) == L: # ์ข…๋ฃŒ ์กฐ๊ฑด
32+
if is_valid(word):
33+
print(''.join(word))
34+
return
35+
36+
for i in range(start, C):
37+
word.append(chars[i])
38+
backtrack(word, i+1)
39+
word.pop()
40+
41+
backtrack([], 0)
42+
43+
44+
"""
45+
ํ’€์ด2: ์กฐํ•ฉ
46+
์ถœ์ฒ˜: 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
47+
"""
48+
from itertools import combinations
49+
50+
L, C = map(int, input().split())
51+
52+
alphabets = input().split()
53+
54+
# ๊ธธ์ด๊ฐ€ L์ธ ๋ชจ๋“  ์กฐํ•ฉ, ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ๋ฐฐ์—ดํ•ด์•ผ๋˜๊ธฐ ๋•Œ๋ฌธ์— sort ํ›„ comb
55+
alpha_combs = combinations(sorted(alphabets), L)
56+
57+
answer = []
58+
59+
for alpha_comb in alpha_combs: # ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ ์ค‘์—์„œ
60+
consonant_count = 0
61+
vowel_count = 0
62+
63+
# ์ž์Œ ๋ชจ์Œ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
64+
for alpha in alpha_comb:
65+
if alpha in "aeiou":
66+
consonant_count += 1
67+
else:
68+
vowel_count += 1
69+
70+
# ๋ชจ์Œ์ด 1๊ฐœ ์ด์ƒ, ์ž์Œ์ด 2 ๊ฐœ ์ด์ƒ์ด๋ฉด ์ถœ๋ ฅ
71+
if consonant_count >= 1 and vowel_count >= 2:
72+
print("".join(alpha_comb))
73+
74+
"""
75+
ํ’€์ด3: DFS ์žฌ๊ท€ ๋ฐฉ์‹
76+
์ถœ์ฒ˜: 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
77+
"""
78+
L, C = map(int, input().split())
79+
80+
alphabets = sorted(input().split())
81+
82+
83+
def dfs(idx, codes):
84+
if L == idx:
85+
vowel_count = 0
86+
consonant_count = 0
87+
88+
# ์ž์Œ ๋ชจ์Œ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
89+
for code in codes:
90+
if code in "aeiou":
91+
consonant_count += 1
92+
else:
93+
vowel_count += 1
94+
95+
# ์ž์Œ 2๊ฐœ ์ด์ƒ, ๋ชจ์Œ ํ•œ๊ฐœ ์ด์ƒ์ด๋ฉด ์•”ํ˜ธ๊ฐ€ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ถœ๋ ฅ
96+
if consonant_count >= 1 and vowel_count >= 2:
97+
print("".join(codes))
98+
99+
else:
100+
for i in range(idx, C):
101+
if codes and alphabets[i] <= codes[-1]: # ์˜ค๋ฆ„์ฐจ์ˆœ ์•„๋‹ˆ๋ฉด ๋ฒ„๋ฆผ
102+
continue
103+
dfs(idx + 1, codes + [alphabets[i]])
104+
105+
106+
dfs(0, [])

0 commit comments

Comments
ย (0)