-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from collections import deque | ||
|
||
def dfs(graph, v, visited): | ||
visited[v] = True | ||
print(v, end=' ') | ||
for i in graph[v]: | ||
if not visited[i]: | ||
dfs(graph, i, visited) | ||
|
||
def bfs(graph, start, visited): | ||
queue = deque([start]) | ||
visited[start] = True | ||
while queue: | ||
v = queue.popleft() | ||
print(v, end=' ') | ||
for i in graph[v]: | ||
if not visited[i]: | ||
queue.append(i) | ||
visited[i] = True | ||
|
||
|
||
n, m, v = map(int, input().split()) | ||
|
||
data = [[] for _ in range(n+1)] | ||
|
||
for i in range(m): | ||
a, b = map(int, input().split()) | ||
data[a].append(b) | ||
data[b].append(a) | ||
|
||
for i in data: | ||
i.sort() | ||
|
||
visited = [False] * (n+1) | ||
dfs(data, v, visited) | ||
print() | ||
visited = [False] * (n+1) | ||
bfs(data, v, visited) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
n, m = map(int, input().split()) | ||
|
||
graph = [] | ||
for i in range(n): | ||
graph.append(list(map(int, input()))) | ||
|
||
# DFS로 특정한 노드를 방문한 뒤에 연결된 모든 노드들도 방문 | ||
def dfs(x, y): | ||
# 주어진 범위를 벗어나는 경우 즉시 종류 | ||
if x <= -1 or x >= n or y <= -1 or y >= m: | ||
return False | ||
# 현재 노드를 아직 방문하지 않은 경우 | ||
if graph[x][y] == 0: | ||
# 해당 노드 방문 처리함 | ||
graph[x][y] = 1 | ||
# 상, 하, 좌, 우 모두 재귀적으로 호출 | ||
dfs(x-1, y) | ||
dfs(x+1, y) | ||
dfs(x, y-1) | ||
dfs(x, y+1) | ||
return True | ||
return False | ||
|
||
# 모든 노드에 대하여 음료수 채우기 | ||
result = 0 | ||
for i in range(n): | ||
for j in range(m): | ||
if dfs(i, j) == True: | ||
result += 1 | ||
print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
def dfs(graph, v, visited): | ||
#햔재 노드 방문처리 | ||
visited[v] = True | ||
print(v, end=' ') | ||
# 현재 노드와 연결된 다른 노드를 재귀적으로 방문 | ||
for i in graph[v]: | ||
if not visited[i]: | ||
dfs(graph, i, visited) | ||
|
||
graph = [ | ||
[], | ||
[2, 3, 8], | ||
[1, 7], | ||
[1, 4, 5], | ||
[3, 5], | ||
[3, 4], | ||
[7], | ||
[2, 6, 8], | ||
[1, 7] | ||
] | ||
|
||
visited = [False] * 9 | ||
|
||
dfs(graph, 1, visited) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from collections import deque | ||
|
||
def bfs(graph, start, visited): | ||
queue = deque([start]) | ||
#현재 노드를 방문처리 | ||
visited[start] = True | ||
#큐가 빌 때까지 반복 | ||
while queue: | ||
# 큐에서 하나의 원소를 뽑아 출력 | ||
v = queue.popleft() | ||
print(v, end=' ') | ||
# 해당 원소와 연결되어있지만 아직 방문하지 않은 원소들을 큐에 삽입 | ||
for i in graph[v]: | ||
if not visited[i]: | ||
queue.append(i) | ||
visited[i] = True | ||
|
||
graph = [ | ||
[], | ||
[2, 3, 8], | ||
[1, 7], | ||
[1, 4, 5], | ||
[3, 5], | ||
[3, 4], | ||
[7], | ||
[2, 6, 8], | ||
[1, 7] | ||
] | ||
|
||
visited = [False] * 9 | ||
bfs(graph, 1, visited) |