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 | 31 |
Tags
- 격파르타 후기
- 백준
- BFS: Shortest Reach in a Graph
- 격파르타 합격후기
- [sqld]자격증합격
- 야근지수
- Find the nearest clone
- 코딩테스트
- DFS: Connected Cell in a Grid
- 머신러닝
- 프로그래머스
- Interview Preparation Kit
- hackerrank
- candies
- Common Child
- Max Array Sum
- 구슬탈출2
- Algorithm
- 알고리즘
- 피보나치 함수
- 해커랭크
- 파이썬
- 격파르타 장점
- 매칭점수
- Reverse Shuffle Merge
- python
- Recursion: Davis' Staircase
- Special String Again
- Roads and Libraries
- programmers
Archives
- Today
- Total
Archive
[BOJ] 1697 숨바꼭질 본문
풀이
-
deque를 사용한 bfs로 품
from collections import deque def bfs(start, end): cnt = 0 # [현재 위치, 현재까지 걸린 시간] pair queue = deque([[start, cnt]]) # 문제 조건에 0~100000 위치까지로 명시됨. # 방문안한 위치만 queue에 값 추가해서 탐색 visited = [False] * 100001 while queue: pos, cnt= queue.popleft() if visited[pos] == False: visited[pos] = True if end == pos: return cnt else: cnt += 1 if pos * 2 <= 100000: queue.append([pos*2, cnt]) if pos+1 <= 100000: queue.append([pos+1,cnt]) if pos-1 >= 0: queue.append([pos-1, cnt]) return cnt s, e = map(int, input().split()) print(bfs(s, e))
'공부 > Algorithm' 카테고리의 다른 글
[BOJ] 2178. 미로탐색 (0) | 2021.02.07 |
---|---|
[BOJ] 1303. 전쟁-전투 (0) | 2021.02.07 |
[BOJ] 1406 에디터 (0) | 2021.01.31 |
[BOJ] 스택수열 (0) | 2021.01.24 |
[BOJ] 9012 괄호 (0) | 2021.01.24 |
Comments