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
- docker
- leetcode
- pytorch
- GitHub Action
- pep8
- NLP
- 완전탐색
- 백준
- rnn
- DeepLearning
- Kaggle
- Matplotlib
- datascience
- GCP
- NaverAItech
- vscode
- FastAPI
- 프로그래머스
- FDS
- GIT
- 알고리즘
- PytorchLightning
- 코딩테스트
- 네이버AItech
- torchserve
- wandb
- Kubernetes
- python
- github
- autoencoder
Archives
- Today
- Total
Sangmun
백준 13913번 숨바꼭질 4 본문
https://www.acmicpc.net/problem/13913
13913번: 숨바꼭질 4
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일
www.acmicpc.net
BFS로 n이 k가 되는 최단거리와 최단경로를 찾는 문제이다.
n > k 인경우는 항상 worst case가 되는데 그래서 해당 케이스를 위한 로직을 따로 만들었다.
일단은 정답이라고 채점이 되었는데 이게 맞는지 모르겠다.
import sys
from collections import deque
input = sys.stdin.readline
n,k = map(int,input().split())
if n > k:
print(n-k)
for i in range(n,k-1,-1):
print(i,end=' ')
else:
visited = [0]*100001
q = deque()
q.append((n,[n]))
Shortest_path = []
while q:
a, SP = q.popleft()
if a == k:
Shortest_path = SP
break
visited[a] = 1
for each in [2,1,-1]:
tmp = a
if each == 2:
if 0 <= tmp*each <= 100000:
if not visited[tmp*each]:
q.append((tmp*each,SP + [tmp*each]))
else:
if 0 <= tmp + each <= 100000:
if not visited[tmp + each]:
q.append((tmp + each,SP +[tmp + each]))
print(len(Shortest_path) - 1)
print(*Shortest_path)
'알고리즘 > 백준' 카테고리의 다른 글
백준 11403번 경로찾기 (0) | 2022.10.01 |
---|---|
백준 14624번 - 전북대학교 (0) | 2022.09.28 |
백준 1766번 문제집 (0) | 2022.09.05 |
백준 1774 우주신과의 교감 (0) | 2022.08.30 |
백준 4386번 별자리 만들기 (0) | 2022.08.25 |
Comments