반응형
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 |
Tags
- logistic regression
- Computer Vision
- Kaggle
- CNN
- 신경망
- 추천 시스템
- recommender system
- CPP
- neural network
- 컴퓨터 비전
- 딥러닝
- 컴퓨터 그래픽스
- Regularization
- petal to metal
- 그래픽스
- 머신러닝
- 비용함수
- Support Vector Machine
- pre-trained
- Vision
- 파이썬
- OpenGL
- 인공지능
- SVM
- 로지스틱 회귀
- Unsupervised learning
- SGD
- C++
- 백준
- cs231n
Archives
- Today
- Total
kwan's note
백준 1260번 DFS와 BFS 파이썬 (python code) 본문
반응형
난이도: 실버2
번호:9633번
그래프의 DFS BFS탐색에 관한 문제이다.
DFS와 BFS의 정의를 알고 구현할 수 있으면 쉽게 풀 수 있는데
bfs를 처음 풀어봐서 왜 queue를 이용해야 하는지 몰라서 오래걸렸다.
from sys import stdin
import time as t
s=t.time()
def dfs(node):
print(node, end=' ')
visitdfs[node] = 1
for i in range(1,N+1):
if visitdfs[i] == 0 and matrixmap[node][i] == 1:
dfs(i)
def bfs(node):
tovisit =[node]
visitbfs[node]=1
while tovisit:
node=tovisit[0]
print(tovisit.pop(0), end=' ')
for i in range(1,N+1):
if visitbfs[i] == 0 and matrixmap[node][i] == 1:
tovisit.append(i)
visitbfs[i] = 1
if __name__ == '__main__':
N,M,S = map(int,stdin.readline().split())
matrixmap = [[0] * (N+1) for i in range(N+1)]
visitdfs = [0] * (N+1)
visitbfs = [0] * (N+1)
for i in range(M):
x,y=map(int, stdin.readline().split())
matrixmap[x][y]=matrixmap[y][x]=1
dfs(S)
print()
bfs(S)
반응형
'Algorithm > python' 카테고리의 다른 글
백준 11047번 동전0 파이썬 (python code) (0) | 2021.01.14 |
---|---|
백준 7576번 토마토 파이썬 (python code) (0) | 2021.01.14 |
백준 9633번 NQueen 파이썬 (python code) (0) | 2021.01.12 |
백준 15649번 N과 M (3) 파이썬 (python code) (0) | 2021.01.12 |
백준-15649번 N과 M (2) 파이썬 (python code) (0) | 2021.01.12 |