Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Algorithm
- sorting
- 종만북
- dfs
- Algospot
- JavaScript
- DP
- 따배씨
- 정수론
- 따라하며 배우는 C언어
- php
- BASIC
- greedy
- graph
- BFS
- Cleancode
- udemy
- 생활코딩
- programmers
- String
- 백준
- 따라하면서 배우는 C언어
- 인프런
- Math
- Python
- server
- C
- BOJ
- web
- C언어
Archives
- Today
- Total
몽상실현개발주의
[BOJ] 1260 / DFS와 BFS / Python 파이썬 본문
[BOJ] 1260 / DFS와 BFS / Python 파이썬
https://www.acmicpc.net/problem/1260
1260번: DFS와 BFS
첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사
www.acmicpc.net
풀이
DFS 와 BFS 탐색을 시행하여 출력하는 문제이다.
DFS 는 재귀로, BFS 는 queue 로 구현하였다.
def DFS(V, N, maps):
global DFSvisited
DFSvisited.append(V)
for i in range(1, N+1):
if maps[V][i] and i not in DFSvisited:
DFS(i, N, maps)
def BFS(V, N, maps):
global BFSVisited
queue = [V]
BFSVisited = [V]
while queue:
f = queue.pop(0)
for i in range(1, N+1):
if maps[f][i] and i not in BFSVisited:
queue.append(i)
BFSVisited.append(i)
N, M, V = map(int, input().split())
maps = [[0 for _ in range(N+1)] for _ in range(N+1)]
for _ in range(M):
f, t = map(int, input().split())
maps[f][t] = 1
maps[t][f] = 1
DFSvisited = []
DFS(V, N, maps)
print(" ".join(map(str, DFSvisited)))
BFSVisited = []
BFS(V, N, maps)
print(" ".join(map(str, BFSVisited)))
'Algorithm PS > BOJ' 카테고리의 다른 글
[BOJ] 1707 / 이분 그래프 / Python 파이썬 (0) | 2021.06.07 |
---|---|
[BOJ] 11724 / 연결 요소의 개수 / Python 파이썬 (0) | 2021.06.05 |
[BOJ] 1676 / 팩토리얼 0의 개수 / Python 파이썬 (0) | 2021.06.04 |
[BOJ] 10872 / 팩토리얼 / Python 파이썬 (0) | 2021.06.04 |
[BOJ] 2089 / -2진수 / Python 파이썬 (0) | 2021.06.04 |
Comments