Skip to content

Commit 0af31fd

Browse files
authored
Merge branch 'AlgorithmStudy-Allumbus:main' into main
2 parents e368c31 + 44b9033 commit 0af31fd

6 files changed

+203
-56
lines changed
Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,59 @@
11
from collections import deque
22

3-
# 2์ฐจ์› ๋ฐฐ์—ด์„ ์ด์šฉํ•œ bfs ๊ตฌํ˜„
4-
def bfs(x, y):
3+
dx = [-1, 1, 0, 0]
4+
dy = [0, 0, -1, 1]
55

6-
# 2์ฐจ์› ๋ฐฐ์—ด์—์„œ ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•œ dx์™€ dy
7-
dx = [0, -1, 0, 1]
8-
dy = [-1, 0, 1, 0]
96

10-
# queue๋ฅผ ์ด์šฉํ•œ bfs ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰
11-
queue.append((x, y))
12-
visited[x][y] = 1
7+
def bfs(graph, visited):
8+
border = 0
9+
queue = deque()
1310

11+
# ์˜์—ญ ๊ฐœ์ˆ˜ ๊ณ„์‚ฐ
12+
for i in range(1, N + 1):
13+
for j in range(1, N + 1):
14+
# ํ˜„์žฌ ์นธ์„ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ํ˜„์žฌ ์นธ๋ถ€ํ„ฐ bfs ํƒ์ƒ‰ ์‹œ์ž‘
15+
if visited[i][j] == 0:
16+
# ์˜์—ญ ๊ฐœ์ˆ˜ ํ•œ ๊ฐœ ์ฆ๊ฐ€
17+
border += 1
18+
queue.append((i, j))
1419

15-
while queue:
16-
(x, y) = queue.popleft()
20+
while queue:
21+
(x, y) = queue.popleft()
22+
# ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
23+
visited[x][y] = 1
1724

18-
# queue์—์„œ popํ•œ ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰
19-
for k in range(4):
20-
row = x + dx[k]
21-
col = y + dy[k]
25+
# ์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ ์นธ์œผ๋กœ ์ด๋™
26+
for k in range(4):
27+
nx = x + dx[k]
28+
ny = y + dy[k]
2229

23-
# ํƒ์ƒ‰ ์กฐ๊ฑด
24-
# 1. ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„ ์•ˆ์ธ์ง€ 0 <= x < n and 0 <= y < n
25-
# 2. ๊ฐ™์€ ์ƒ‰์ธ์ง€
26-
# 3. ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋Š”์ง€
27-
if 0 <= row < n and 0 <= col < n:
28-
if arr[x][y] == arr[row][col] and visited[row][col] == 0:
29-
queue.append((row, col))
30-
visited[row][col] = 1
30+
# ์นธ์ด ๋ณด๋“œ ๋ฐ–์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š์•˜๋Š”์ง€,์ธ์ ‘ํ•œ ์นธ์ด ๊ฐ™์€ ์ƒ‰์ธ์ง€,์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธ
31+
if (1 <= nx <= N and 1 <= ny <= N) and graph[x][y] == graph[nx][ny] and visited[nx][ny] == 0:
32+
# ํ•ด๋‹น ์นธ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
33+
queue.append((nx, ny))
34+
visited[nx][ny] = 1
3135

36+
return border
3237

33-
# ์ดˆ๊ธฐ๊ฐ’ ์„ธํŒ…
34-
n = int(input())
35-
visited = [[0] * n for _ in range(n)]
36-
arr = [list(input()) for _ in range(n)]
37-
queue = deque()
3838

39-
# ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ์˜ ๋‹ต
40-
answerForNormal = 0
39+
# ์ž…๋ ฅ๋ฐ›๊ธฐ
40+
N = int(input())
41+
graph = [[0] * (N + 1)]
42+
visited = [[0] * (N + 1) for _ in range(N + 1)]
4143

42-
# ์ ๋ก์ƒ‰์•ฝ์ธ ๊ฒฝ์šฐ์˜ ๋‹ต
43-
answerForColorBlindness = 0
44+
for _ in range(N):
45+
graph.append([0] + list(input()))
4446

47+
# ์ •์ƒ์ธ์ด ๋ณด๋Š” ์˜์—ญ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
48+
num_of_normal = bfs(graph, visited)
4549

46-
# ์ ๋ก์ƒ‰์•ฝ์ด ์•„๋‹Œ ๊ฒฝ์šฐ
47-
for i in range(n):
48-
for j in range(n):
49-
if visited[i][j] == 0:
50-
bfs(i, j)
51-
answerForNormal += 1
50+
# ์ ๋ก์ƒ‰์•ฝ์ด ๋ณด๋Š” ์˜์—ญ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜ ์ ๋ก์ƒ‰์•ฝ์€ R,G๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ๋ชปํ•˜๋ฏ€๋กœ ๋ชจ๋“  R์„ G๋กœ ๋ณ€ํ™˜
51+
for i in range(1, N + 1):
52+
for j in range(1, N + 1):
53+
if graph[i][j] == 'R':
54+
graph[i][j] = 'G'
5255

56+
visited = [[0] * (N + 1) for _ in range(N + 1)]
57+
num_of_abnormal = bfs(graph, visited)
5358

54-
# ์ ๋ก์ƒ‰์•ฝ์ธ ๊ฒฝ์šฐ R๊ณผ G๋Š” ๊ฐ™์œผ๋ฏ€๋กœ
55-
56-
for i in range(n):
57-
for j in range(n):
58-
if arr[i][j] == 'G':
59-
arr[i][j] = 'R'
60-
61-
visited = [[0] * n for _ in range(n)]
62-
for i in range(n):
63-
for j in range(n):
64-
if not visited[i][j]:
65-
bfs(i,j)
66-
answerForColorBlindness += 1
67-
68-
print(answerForNormal, answerForColorBlindness)
59+
print(f"{num_of_normal} {num_of_abnormal}")
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
'''
2+
3+
4+
๋†๋ถ€ ํ•ด๊ฐ•์ด๋Š” N X N์นธ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‚˜๋ฌดํŒ์—์„œ ๋ฒ„์„ฏ ๋†์‚ฌ๋ฅผ ์ง“๋Š”๋‹ค. ๋‚˜๋ฌดํŒ์€ ๋ฒ„์„ฏ์ด ์ž๋ž„ ์ˆ˜ ์žˆ๋Š” ์นธ๊ณผ ์—†๋Š” ์นธ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.
5+
6+
7+
๊ฐ ๋ฒ„์„ฏ ํฌ์ž๋Š” ํฌ์ž๊ฐ€ ์‹ฌ์–ด์ง„ ์นธ์„ ํฌํ•จํ•ด ์ตœ๋Œ€ K๊ฐœ์˜ ์—ฐ๊ฒฐ๋œ (๋ฒ„์„ฏ์ด ์ž๋ž„ ์ˆ˜ ์žˆ๋Š”) ์นธ์— ๋ฒ„์„ฏ์„ ์ž๋ผ๊ฒŒ ํ•œ๋‹ค.
8+
์ด๋•Œ ์—ฐ๊ฒฐ๋œ ์นธ์€ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ ์–ด๋„ ํ•œ ๋ณ€์„ ๊ณต์œ ํ•˜๋Š” ์นธ๋“ค์˜ ์ง‘ํ•ฉ์ด๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค.
9+
10+
๋˜ํ•œ ํ•œ ์นธ์— ๋ฒ„์„ฏ ํฌ์ž๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๊ฒน์ณ์„œ ์‹ฌ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งŒ์•ฝ x๊ฐœ์˜ ๋ฒ„์„ฏ ํฌ์ž๋ฅผ ๊ฒน์ณ ์‹ฌ์œผ๋ฉด ํฌ์ž๊ฐ€ ์‹ฌ์–ด์ง„ ์นธ์„ ํฌํ•จํ•ด ์ตœ๋Œ€
11+
x X K๊ฐœ์˜ ์—ฐ๊ฒฐ๋œ (๋ฒ„์„ฏ์ด ์ž๋ž„ ์ˆ˜ ์žˆ๋Š”) ์นธ์— ๋ฒ„์„ฏ์ด ์ž๋ž€๋‹ค.
12+
13+
14+
ํ•ด๊ฐ•์ด๋Š” ๋ฒ„์„ฏ ํฌ์ž๋ฅผ ์‹ฌ์„ ๋•Œ ์ตœ์†Œ ๊ฐœ์ˆ˜๋กœ๋งŒ ์‹ฌ์œผ๋ ค๊ณ  ํ•œ๋‹ค.
15+
ํ•ด๊ฐ•์ด๊ฐ€ ๋†์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ• ์ง€ ํŒ๋‹จํ•˜๊ณ , ๋†์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ๋‚จ์€ ๋ฒ„์„ฏ ํฌ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
16+
๋ฒ„์„ฏ ํฌ์ž๋ฅผ ํ•˜๋‚˜๋ผ๋„ ์‚ฌ์šฉํ•˜๊ณ  ๋ฒ„์„ฏ์ด ์ž๋ž„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์นธ์— ๋ฒ„์„ฏ์ด ์ „๋ถ€ ์ž๋ž์„ ๋•Œ ๋†์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ์ •์˜ํ•œ๋‹ค.
17+
18+
1. N X N ์นธ์—์„œ ๋ฒ„์„ฏ์„ ์‹ฌ์–ด์•ผ ํ•จ. ์ด๋•Œ ์‹ฌ์€ ๋ฒ„์„ฏ์€ ์ƒํ•˜์ขŒ์šฐ ์ตœ๋Œ€ K์นธ์œผ๋กœ ํ™•์‚ฐ.
19+
(๋ฒ„์„ฏ์„ ์‹ฌ์€ ์นธ์„ ์‹œ์ž‘์œผ๋กœ BFS ํƒ์ƒ‰ ์ง„ํ–‰ํ•˜๋ฉด ํ•ด๋‹น ์นธ์œผ๋กœ๋ถ€ํ„ฐ ๋ฒ„์„ฏ์„ ์‹ฌ์„ ์ˆ˜ ์žˆ๋Š” ์ธ์ ‘ํ•œ ์นธ์ด ๋ช‡๊ฐœ์ธ์ง€ ์•Œ์ˆ˜ ์žˆ์Œ)
20+
21+
2. 2์ฐจ์› ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ์นธ์ด 0์ด๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฑฐ๊ธฐ์„œ๋ถ€ํ„ฐ BFS ํƒ์ƒ‰ ์‹œ์ž‘.
22+
3. BFS ํƒ์ƒ‰์œผ๋กœ ํ•ด๋‹น ์นธ์œผ๋กœ๋ถ€ํ„ฐ ์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ 0(๋ฒ„์„ฏ ๋†์‚ฌ ๊ฐ€๋Šฅ ์นธ)์ด ๋ช‡ ๊ฐœ์ธ์ง€ ํŒŒ์•…ํ›„ K๊ฐœ๋กœ ๋‚˜๋ˆ„๋ฉด ํ•ด๋‹น ๊ตฌ์—ญ์— ๋ช‡๊ฐœ์˜ ๋ฒ„์„ฏ ํฌ์ž๊ฐ€ ํ•„์š”ํ•œ์ง€ ๊ณ„์‚ฐ ๊ฐ€๋Šฅ
23+
4. 2-3์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๋ชจ๋“  ๋ฒ„์„ฏ ๋†์‚ฌ ๊ฐ€๋Šฅ ๊ตฌ์—ญ์„ ์„ธ๋ฉฐ ์ด ๋ช‡๊ฐœ์˜ ๋ฒ„์„ฏ ํฌ์ž๊ฐ€ ํ•„์š”ํ•œ์ง€ ๊ณ„์‚ฐ
24+
5. ํ•„์š”ํ•œ ๋ฒ„์„ฏ ํฌ์ž ๊ฐœ์ˆ˜๊ฐ€ M๋ณด๋‹ค ํฌ๋ฉด ๋‚จ์€ ๋ฒ„์„ฏ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅ ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด IMPOSSIBLE ์ถœ๋ ฅ.
25+
5-1. ์ด๋–„ ๋ฒ„์„ฏ ํฌ์ž๋ฅผ ํ•˜๋‚˜๋„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ IMPOSSIBLE ์ถœ๋ ฅ
26+
27+
'''
28+
29+
from collections import deque
30+
31+
32+
def bfs(i, j, visited):
33+
dx = [-1, 1, 0, 0]
34+
dy = [0, 0, -1, 1]
35+
36+
queue = deque()
37+
38+
# ์‹œ์ž‘์  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
39+
queue.append((i, j))
40+
visited[i][j] = 1
41+
42+
# ์‹œ์ž‘์ (x,y)์œผ๋กœ๋ถ€ํ„ฐ ์ธ์ ‘ํ•œ ๋ฒ„์„ฏ ๋†์‚ฌ ๊ฐ€๋Šฅ ์นธ์˜ ๊ฐœ์ˆ˜ (์‹œ์ž‘์  ํฌํ•จ)
43+
num = 1
44+
45+
while queue:
46+
x, y = queue.popleft()
47+
48+
# ์ƒํ•˜์ขŒ์šฐ ์ธ์ ‘ํ•œ ์นธ์— ๋ฒ„์„ฏ ๋†์‚ฌ ๊ฐ€๋Šฅํ•œ์ง€ ๋ณด๊ธฐ
49+
for k in range(4):
50+
nx = x + dx[k]
51+
ny = y + dy[k]
52+
53+
# ๊ทธ๋ž˜ํ”„ ๋ฒ”์œ„์— ์žˆ๋Š”์ง€, ๋ฒ„์„ฏ ๋†์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ์ง€, ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธ
54+
if (0 <= nx <= N - 1 and 0 <= ny <= N - 1) and graph[nx][ny] == 0 and visited[nx][ny] == 0:
55+
# ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
56+
queue.append((nx, ny))
57+
visited[nx][ny] = 1
58+
# ๋ฒ„์„ฏ ๋†์‚ฌ ๊ฐ€๋Šฅํ•œ ์นธ์˜ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€
59+
num += 1
60+
61+
# ํ•ด๋‹น ๊ตฌ์—ญ์— ํ•„์š”ํ•œ ๋ฒ„์„ฏ ํฌ์ž ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
62+
if num % K == 0:
63+
return num // K
64+
else:
65+
return (num // K) + 1
66+
67+
68+
# ์ž…๋ ฅ๋ฐ›๊ธฐ
69+
N, M, K = map(int, input().split())
70+
71+
# ๋‚˜๋ฌดํŒ ๋ฐฐ์—ด
72+
graph = []
73+
# ๋‚˜๋ฌดํŒ ๋ฐฉ๋ฌธ ๋ฐฐ์—ด
74+
visited = [[0] * N for _ in range(N)]
75+
76+
mushroom_count = 0
77+
78+
for _ in range(N):
79+
graph.append(list(map(int, input().split())))
80+
81+
for i in range(N):
82+
for j in range(N):
83+
# ๋‚˜๋ฌดํŒ์ด ๋ฒ„์„ฏ์„ ์‹ฌ์„ ์ˆ˜ ์žˆ๊ณ , ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด
84+
if graph[i][j] == 0 and visited[i][j] == 0:
85+
mushroom_count += bfs(i, j, visited)
86+
87+
if mushroom_count == 0 or mushroom_count > M:
88+
print("IMPOSSIBLE")
89+
else:
90+
print(f"POSSIBLE\n{M - mushroom_count}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
# ๋‘ ๋‹จ์–ด๊ฐ€ ํ•œ๊ธ€์ž๋งŒ ๋‹ค๋ฅธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
3+
def count_same_letter(word1, word2):
4+
5+
count = 0
6+
word_length = len(word1)
7+
# ๋ชจ๋“  ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” ๊ฐ™์œผ๋ฏ€๋กœ ๋‹จ์–ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฉด์„œ ๊ฐ™์€ ๊ธ€์ž ๊ณ„์‚ฐ
8+
for i in range(word_length):
9+
if word1[i] == word2[i]:
10+
count += 1
11+
12+
if word_length - 1 == count:
13+
return True
14+
else:
15+
return False
16+
17+
18+
19+
def dfs(begin, target, words, depth):
20+
global words_num
21+
global min_value
22+
23+
# depth๊ฐ€ words์˜ ํฌ๊ธฐ๋ณด๋‹ค ์ปค์ง€๋ฉด ํ•ด๋‹น ํƒ์ƒ‰ ์ข…๋ฃŒ
24+
if depth > words_num:
25+
return
26+
27+
# begin๊ณผ target์ด ๊ฐ™๋‹ค๋ฉด ํƒ์ƒ‰ ์ข…๋ฃŒ
28+
if begin == target:
29+
# ํ˜„์žฌ depth์™€ ์ตœ์†Ÿ๊ฐ’ ๋น„๊ต
30+
min_value = min(min_value, depth)
31+
return
32+
33+
copy_words = words[:]
34+
35+
for i in range(len(words)):
36+
# ๋‘ ๋‹จ์–ด๊ฐ€ ํ•œ๊ธ€์ž๋งŒ ๋‹ค๋ฅด๋‹ค๋ฉด ๊ทธ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜
37+
if count_same_letter(begin, words[i]):
38+
# ๋‹จ์–ด ๋ชฉ๋ก์—์„œ ํ˜„์žฌ ๋‹จ์–ด ์ œ์™ธํ•˜๊ณ 
39+
removed_word = copy_words.pop(i)
40+
# ๋‹ค์Œ detph dfs ์‹คํ–‰
41+
dfs(removed_word, target, copy_words, depth + 1)
42+
# ๋ฐฐ์—ด ์›๋ณต
43+
copy_words.insert(i, removed_word)
44+
45+
46+
47+
48+
def solution(begin, target, words):
49+
50+
# target์ด words์— ์—†์–ด์„œ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
51+
if target not in words:
52+
return 0
53+
54+
# ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜
55+
global words_num
56+
words_num = len(words)
57+
58+
global min_value
59+
min_value = 10000
60+
61+
dfs(begin, target, words, 0)
62+
63+
return min_value

โ€Ž_MonthlyChallenges/DASHBOARD.mdโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
| ์‚ฌ์šฉ์ž | ์ฑŒ๋ฆฐ์ง€ ์œ ํ˜• | ๋ฌธ์ œ ์ˆ˜ | ๋‹ฌ์„ฑ ์—ฌ๋ถ€ |
44
| ------ | ----------- | ------- | --------- |
55
| Mingguriguri | ๊ทธ๋ž˜ํ”„ | 0 | โŒ |
6-
| Mingguriguri | DP | 2 | โŒ |
6+
| Mingguriguri | DP | 5 | โœ… |

โ€Ž_MonthlyChallenges/scoreboard.jsonโ€Ž

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
"๊ทธ๋ž˜ํ”„": [],
66
"DP": [
77
2169,
8-
1520
8+
1520,
9+
11660,
10+
10844,
11+
1914
912
],
1013
"achieved": {
1114
"๊ทธ๋ž˜ํ”„": false,
12-
"DP": false
15+
"DP": true
1316
}
1417
}
1518
}

โ€Žminjeong/test.txtโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
test4
1+
test5

0 commit comments

Comments
ย (0)