반응형
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
- 싸피
- kakao
- 프로그래머스
- Blind
- 알고리즘
- 그래프
- BFS
- javascript
- SW역량테스트
- 파이썬
- sort
- DFS
- 코테
- 자료구조
- 스택
- 힙큐
- 다이나믹프로그래밍
- 백준
- Python
- 자바스크립트
- 삼성
- 카카오
- 코딩테스트
- DP
- boj
- SWEA
- Backjoon
- 완전탐색
- SSAFY
- algorithm
Archives
- Today
- Total
맞왜틀
[python] 백준 - 13023. ABCDE 본문
반응형

🤔문제 해결
문제는 이어져있는 친구가 4명인지 물어보는 것... ( 헷갈렸다 )
기본적인 DFS 문제이다. 중간에 조건을 줘서 잘 멈춰주기만 한다면 시간초과는 해결될 것이다.
💻소스 코드
import sys
def dfs(current, cnt):
global answer
if answer == 1: # 답을 이미 찾았다면 dfs 멈추기
return
if cnt == 5: # 답 찾았을 때 (친구가 4명)
answer = 1
return
visited[current] = 1
for neighbor in adj[current]:
if not visited[neighbor]:
dfs(neighbor, cnt + 1)
visited[current] = 0
input = sys.stdin.readline
answer = 0
N, M = map(int, input().split())
adj = [[] for _ in range(N)]
for _ in range(M):
a, b = map(int, input().split())
adj[a].append(b)
adj[b].append(a)
visited = [0 for _ in range(N)]
for i in range(N):
if not visited[i]:
dfs(i, 1)
print(answer)
📕문제 확인
출처: BACKJOON ONLINE JUDGE
반응형
'Algorithm Problem > Python' 카테고리의 다른 글
| [python] 백준 - 1148. 단어 만들기 (0) | 2021.09.06 |
|---|---|
| [python] 백준 - 1105. 팔 (0) | 2021.09.05 |
| [python] 백준 - 2615. 오목 (2) | 2021.09.03 |
| [python] 백준 - 1052. 물병 (0) | 2021.09.02 |
| [python] 프로그래머스 - 이진 변환 반복하기(월간 코드 챌린지 시즌1) (0) | 2021.09.01 |
