728x90
728x90
ํ์ด1
solution ํจ์
- n : ์ปดํจํฐ์ ๊ฐ์
- computers : ์ฐ๊ฒฐ์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ธด 2์ฐจ์ ๋ฐฐ์ด
- return : ๋คํธ์ํฌ์ ๊ฐ์
dfs ํจ์
- graph : computers
- v : ํ์ฌ index
- visited : ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ์ํ 1์ฐจ์ ๋ฐฐ์ด
์์ธํ ํ์ด
- ์ฐ๊ฒฐ๋์ง ์์ ๋คํธ์ํฌ๊ฐ ์์์๋ ์์ผ๋ฏ๋ก index ๋ฅผ n ๋งํผ ๋ฐ๋ณตํ๋ฉฐ dfs ํจ์๋ฅผ ํธ์ถํ๋ค.
- ์ฒ์ ๋ฐฉ๋ฌธํ๋ ๊ฒฝ์ฐ์๋ ์๋ก์ด ๋คํธ์ํฌ๊ฐ ์์๋๋ ๊ฒ๊ณผ ๊ฐ๋ค.
- count ๋ฅผ ์ฆ๊ฐ์ํด
- ํ์ฌ index ๋ฅผ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ ์ํด
- ์ด๋ฏธ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ ๋์ด์๋ค๋ฉด ๋คํธ์ํฌ๊ฐ ์ฐ๊ฒฐ๋์ด ์์ผ๋ฏ๋ก, countํ์ง ์์
- graph ๋ฅผ ๋ฐ๋ณตํ๋ฉฐ ๋ฐฉ๋ฌธํ์ง ์์ ๋ ธ๋์ ๋ํด ๋ฐฉ๋ฌธ์ฒ๋ฆฌ, dfs ์ฌ๊ทํธ์ถ
count = 0
def dfs(graph, v, visited):
global count
if not visited[v]:
count += 1
visited[v] = True
# DFS ์ํ
for i, node in enumerate(graph[v]):
if i == v or node == 0:
continue
if not visited[i]:
visited[i] = True
dfs(graph, i, visited)
def solution(n, computers):
global count
count = 0
visited = [False] * n
for i in range(n):
dfs(computers, i, visited)
return count
ํ์ด2
- index ๋ฅผ n ๋งํผ ๋ฐ๋ณตํ๋ฉฐ ์ฐ๊ฒฐ๋์ง ์์ ๋คํธ์ํฌ๋ฅผ ์ฐพ๋๋ค.
- visited[i] == False ์ธ ๊ฒฝ์ฐ์๋ง dfs ํจ์๋ฅผ ํธ์ถํ๋ค.
- dfs ๋ฅผ ํธ์ถํ๋ ๋ฐ๋ณต๋ฌธ ๋ด์์ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ์ ๋ํ if ๋ฌธ ์ถ๊ฐ
- dfs ํจ์ ๋ด์์ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ์ ๋ํ if ๋ฌธ ์ ๊ฑฐ
- dfs ๋ฅผ ํธ์ถํ๋ ๋ฐ๋ณต๋ฌธ ๋ด์์ count ์ฆ๊ฐ
- dfs ๋ฅผ ํธ์ถํ๋ solution ํจ์ ๋ด์์ count ๋ณ์ ์ง์
def dfs(graph, v, visited):
# ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
visited[v] = True
# DFS ์ํ
for i, node in enumerate(graph[v]):
if i == v or node == 0:
continue
if not visited[i]:
dfs(graph, i, visited)
def solution(n, computers):
count = 0
visited = [False] * n
for i in range(n):
# ์๋ก์ด ๋คํธ์ํฌ๊ฐ ์์๋๋ ๊ฒฝ์ฐ
if not visited[i]:
dfs(computers, i, visited)
count += 1
return count
print(solution(3, [[1, 1, 0], [1, 1, 0], [0, 0, 1]])) # return 2
print(solution(3, [[1, 1, 0], [1, 1, 1], [0, 1, 1]])) # return 1
์ ํ์ฑ ํ ์คํธ ๋น๊ต
๋๋ณด๊ธฐ
ํ์ด1
ํ ์คํธ 1 ใ | ํต๊ณผ (0.02ms, 10.2MB) |
ํ ์คํธ 2 ใ | ํต๊ณผ (0.02ms, 10.1MB) |
ํ ์คํธ 3 ใ | ํต๊ณผ (0.06ms, 10.1MB) |
ํ ์คํธ 4 ใ | ํต๊ณผ (0.08ms, 10.3MB) |
ํ ์คํธ 5 ใ | ํต๊ณผ (0.01ms, 10.3MB) |
ํ ์คํธ 6 ใ | ํต๊ณผ (0.42ms, 10.2MB) |
ํ ์คํธ 7 ใ | ํต๊ณผ (0.03ms, 10.2MB) |
ํ ์คํธ 8 ใ | ํต๊ณผ (0.31ms, 10.2MB) |
ํ ์คํธ 9 ใ | ํต๊ณผ (0.20ms, 10.3MB) |
ํ ์คํธ 10 ใ | ํต๊ณผ (0.20ms, 10.2MB) |
ํ ์คํธ 11 ใ | ํต๊ณผ (1.91ms, 10.2MB) |
ํ ์คํธ 12 ใ | ํต๊ณผ (1.26ms, 10.3MB) |
ํ ์คํธ 13 ใ | ํต๊ณผ (0.63ms, 10.2MB) |
ํ์ด2
ํ ์คํธ 1 ใ | ํต๊ณผ (0.01ms, 10.2MB) |
ํ ์คํธ 2 ใ | ํต๊ณผ (0.01ms, 10.2MB) |
ํ ์คํธ 3 ใ | ํต๊ณผ (0.05ms, 10.1MB) |
ํ ์คํธ 4 ใ | ํต๊ณผ (0.05ms, 10.2MB) |
ํ ์คํธ 5 ใ | ํต๊ณผ (0.01ms, 10.1MB) |
ํ ์คํธ 6 ใ | ํต๊ณผ (0.24ms, 10.3MB) |
ํ ์คํธ 7 ใ | ํต๊ณผ (0.02ms, 10.2MB) |
ํ ์คํธ 8 ใ | ํต๊ณผ (0.18ms, 10.2MB) |
ํ ์คํธ 9 ใ | ํต๊ณผ (0.11ms, 10.1MB) |
ํ ์คํธ 10 ใ | ํต๊ณผ (0.10ms, 10.1MB) |
ํ ์คํธ 11 ใ | ํต๊ณผ (0.86ms, 10.3MB) |
ํ ์คํธ 12 ใ | ํต๊ณผ (0.74ms, 10.2MB) |
ํ ์คํธ 13 ใ | ํต๊ณผ (0.32ms, 10.1MB) |
์ด๋ฒ ๋ฌธ์ ๋ฅผ ํ๋ฉด์
- ๋ฌธ์ ์์ DFS / BFS ๋ฅผ ์ด์ฉํ๋ผ๊ณ ์น์ ํ๊ฒ ์๋ ค์คฌ๋ค.
- ๋ค๋ฅธ ๋ฌธ์ ๋ฅผ ํ ๋ "DFS / BFS ๋ฅผ ์ด์ฉํด์ผํ๋ค" ๋ ํํธ๋ฅผ ์ฐพ์๋ด์ผ๊ฒ ๋ค.
- BFS ํ์ด ์ฐธ๊ณ - ๋งํฌ
728x90
728x90
'๐ Algorithm > ์๊ณ ๋ฆฌ์ฆ-Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ/Python] BOJ 1182 ๋ถ๋ถ์์ด์ ํฉ (0) | 2021.04.21 |
---|---|
[์๊ณ ๋ฆฌ์ฆ/Python] ํ๋ก๊ทธ๋๋จธ์ค - ์์ฅ (ํด์) (0) | 2021.03.11 |
[์๊ณ ๋ฆฌ์ฆ/Python] ํ๋ก๊ทธ๋๋จธ์ค - ํ๊ฒ ๋๋ฒ (DFS) (0) | 2021.03.06 |
[์๊ณ ๋ฆฌ์ฆ/Python] BOJ 1158 - ์์ธํธ์ค ๋ฌธ์ (๊ธฐ์ด ์๋ฃ๊ตฌ์กฐ) (2) | 2021.03.06 |
[์๊ณ ๋ฆฌ์ฆ] Day3 - 1967๋ฒ ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2018.08.08 |