https://school.programmers.co.kr/learn/courses/30/lessons/154540
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
โ DFS ํ์ด
import sys
sys.setrecursionlimit(10**6)
dx = [1, -1, 0, 0]
dy = [0, 0, -1, 1]
def dfs(x, y, graph):
count = graph[x][y] # ํ์ฌ ์์น ์๋๊ฐ
graph[x][y] = 0 # ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < len(graph) and 0 <= ny < len(graph[0]) and graph[nx][ny] > 0:
count += dfs(nx, ny, graph)
return count
def solution(maps):
graph = []
for m in maps:
m = m.replace('X', '0')
graph.append(list(map(int, list(m))))
answer = []
for x in range(len(graph)):
for y in range(len(graph[0])):
if graph[x][y] > 0: # ์์ง ๋ฐฉ๋ฌธํ์ง ์์
count = dfs(x, y, graph)
answer.append(count)
if answer:
return sorted(answer)
else:
return [-1]
์ด ๋ฌธ์ ์ ๊ฐ์ด ๊ทธ๋ฃนํ์ ํด์ผํ๋ ๊ฒฝ์ฐ์, ๊ฐ์ง๋ฅผ ๋๊น์ง ํ๊ณ ๋๋ DFS ๋ฐฉ์์ด ์ ํฉํ๋ค๊ณ ์๊ฐํ์๋ค.
ํ์ง๋ง, ๋ชจ๋ ํ ์คํธ์ผ์ด์ค์ ๋ํด ํต๊ณผํ์ง ๋ชปํด ์ฌ๊ท๊น์ด๋ฅผ ๋๋ ค์ฃผ๋ ํต๊ณผํ ์ ์์๋ค.
โ BFS ํ์ด
from collections import deque
dx = [1, -1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(x, y, graph):
queue = deque([(x, y)])
count = 0
while queue:
x, y = queue.popleft()
if graph[x][y] == 0:
continue
count += graph[x][y]
graph[x][y] = 0 # ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < len(graph) and 0 <= ny < len(graph[0]) and graph[nx][ny] > 0:
queue.append((nx, ny))
return count
def solution(maps):
graph = []
for m in maps:
m = m.replace('X', '0')
graph.append(list(map(int, list(m))))
answer = []
for x in range(len(graph)):
for y in range(len(graph[0])):
if graph[x][y] > 0: # ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๊ฒฝ์ฐ
count = bfs(x, y, graph)
answer.append(count)
if answer:
return sorted(answer)
else:
return [-1]
'ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ํ๊ฒ๋๋ฒ (0) | 2024.08.28 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] H-Index (0) | 2024.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.1] ์นด๋๋ญ์น (0) | 2024.08.01 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.3] ์ด์ค ์ฐ์ ์์ํ (0) | 2024.07.31 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ๋ ๋งต๊ฒ (0) | 2024.07.30 |
https://school.programmers.co.kr/learn/courses/30/lessons/154540
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
โ DFS ํ์ด
import sys
sys.setrecursionlimit(10**6)
dx = [1, -1, 0, 0]
dy = [0, 0, -1, 1]
def dfs(x, y, graph):
count = graph[x][y] # ํ์ฌ ์์น ์๋๊ฐ
graph[x][y] = 0 # ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < len(graph) and 0 <= ny < len(graph[0]) and graph[nx][ny] > 0:
count += dfs(nx, ny, graph)
return count
def solution(maps):
graph = []
for m in maps:
m = m.replace('X', '0')
graph.append(list(map(int, list(m))))
answer = []
for x in range(len(graph)):
for y in range(len(graph[0])):
if graph[x][y] > 0: # ์์ง ๋ฐฉ๋ฌธํ์ง ์์
count = dfs(x, y, graph)
answer.append(count)
if answer:
return sorted(answer)
else:
return [-1]
์ด ๋ฌธ์ ์ ๊ฐ์ด ๊ทธ๋ฃนํ์ ํด์ผํ๋ ๊ฒฝ์ฐ์, ๊ฐ์ง๋ฅผ ๋๊น์ง ํ๊ณ ๋๋ DFS ๋ฐฉ์์ด ์ ํฉํ๋ค๊ณ ์๊ฐํ์๋ค.
ํ์ง๋ง, ๋ชจ๋ ํ ์คํธ์ผ์ด์ค์ ๋ํด ํต๊ณผํ์ง ๋ชปํด ์ฌ๊ท๊น์ด๋ฅผ ๋๋ ค์ฃผ๋ ํต๊ณผํ ์ ์์๋ค.
โ BFS ํ์ด
from collections import deque
dx = [1, -1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(x, y, graph):
queue = deque([(x, y)])
count = 0
while queue:
x, y = queue.popleft()
if graph[x][y] == 0:
continue
count += graph[x][y]
graph[x][y] = 0 # ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < len(graph) and 0 <= ny < len(graph[0]) and graph[nx][ny] > 0:
queue.append((nx, ny))
return count
def solution(maps):
graph = []
for m in maps:
m = m.replace('X', '0')
graph.append(list(map(int, list(m))))
answer = []
for x in range(len(graph)):
for y in range(len(graph[0])):
if graph[x][y] > 0: # ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ๊ฒฝ์ฐ
count = bfs(x, y, graph)
answer.append(count)
if answer:
return sorted(answer)
else:
return [-1]
'ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ํ๊ฒ๋๋ฒ (0) | 2024.08.28 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] H-Index (0) | 2024.08.02 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.1] ์นด๋๋ญ์น (0) | 2024.08.01 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.3] ์ด์ค ์ฐ์ ์์ํ (0) | 2024.07.31 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv.2] ๋ ๋งต๊ฒ (0) | 2024.07.30 |