도착점이 아닌 점에서 최단 시간으로 같은 점을 여러 번 방문하였을 때 방법을 세는 부분 필요 (백준 질문)
from collections import deque
import sys
n,k= map(int, sys.stdin.readline().rstrip().split())
q=deque()
q.append((n, 0)) #시작 위치, 횟수 (초기값은 0으로 conut)
cnt=0
visited=[-1 for _ in range(100001)] # 방문 여부 및 방문 최소 횟수를 기록
visited[n]=0 #-1이 아닌 방문 여부 및
case=[-1,1]
while q :
now = q.pop()
loc=now[0] ; time=now[1]
if loc==k: #k 만나면 cnt 갱신
cnt+=1
for i in range(3) :
if i==2: #순간이동 경우
tmploc=loc*2
else : # -1 , 1 더하는 경우
tmploc=loc+case[i]
if 0<=tmploc<100001 and (visited[tmploc]==-1 \
or visited[tmploc]==time+1):
# 주의! 단순히 한번 방문했다고 무시하면 안됨
# 이미 방문됐더라도 -
# 방문된 아이의 최소 시간과 지금 내 시간이 같으면
# 나도 q에 들어가기 가능
visited[tmploc]=time+1 # visited[tmploc]==-1 경우를 고려한 코드
q.appendleft((tmploc, time+1))
print(visited[k])
#visited에는 최소 횟수들이 기록돼있으니 k의 최소횟수 데려오면 됨
print(cnt)
from collections import deque
import sys
n,k= map(int, sys.stdin.readline().rstrip().split())
q=deque()
q.append((n,0))
mini=1000001
cnt=0
visited=[0 for _ in range(100001)]
visited[n]=1
while q :
now = q.pop()
#print("now",now) ;print(q)
loc=now[0] ; time=now[1]
if loc==k:
if mini>time:mini=time ; cnt=1;
elif time==mini: cnt+=1
if mini<1000001:
visited[mini]=0
if 0<=loc-1<100001 and visited[loc-1]==0:
visited[loc-1]=1
if loc-1==k:
if mini>time+1:mini=time+1; cnt=1;
elif time+1==mini: cnt+=1
visited[mini]=0
q.appendleft((loc-1, time+1))
if 0<=loc+1<100001 and visited[loc+1]==0:
visited[loc+1]=1
if loc+1==k:
if mini>time+1:mini=time+1; cnt=1;
elif time+1==mini: cnt+=1
visited[mini]=0
q.appendleft((loc+1, time+1))
if 0<=loc*2<100001 and visited[loc*2]==0:
visited[loc*2]=1
if loc*2==k:
if mini>time+1:mini=time+1; cnt=1;
elif time+1==mini: cnt+=1
visited[mini]=0
q.appendleft((loc*2, time+1))
print(mini)
print(cnt)
from collections import deque
import sys
n,k= map(int, sys.stdin.readline().rstrip().split())
q=deque()
q.append((n,0))
mini=1000001
cnt=0
visited=[0 for _ in range(100001)]
visited[n]=1
while q :
now = q.pop()
#print("now",now) ;print(q)
loc=now[0] ; time=now[1]
if 0<=loc-1<100001 and visited[loc-1]==0:
visited[loc-1]=1
if loc-1==k:
if mini>time+1:mini=time+1; cnt=1;
elif time+1==mini: cnt+=1
visited[mini]=0
q.appendleft((loc-1, time+1))
if 0<=loc+1<100001 and visited[loc+1]==0:
visited[loc+1]=1
if loc+1==k:
if mini>time+1:mini=time+1; cnt=1;
elif time+1==mini: cnt+=1
visited[mini]=0
q.appendleft((loc+1, time+1))
if 0<=loc*2<100001 and visited[loc*2]==0:
visited[loc*2]=1
if loc*2==k:
if mini>time+1:mini=time+1; cnt=1;
elif time+1==mini: cnt+=1
visited[mini]=0
q.appendleft((loc*2, time+1))
print(mini)
print(cnt)
from collections import deque
import sys
n,k= map(int, sys.stdin.readline().rstrip().split())
q=deque()
q.append((n,0))
mini=1000001
cnt=0
visited=[0 for _ in range(100001)]
visited[n]=1
while q :
now = q.pop()
loc=now[0] ; time=now[1]
if 0<=loc-1<100001 and visited[loc-1]==0:
visited[loc-1]=1
if loc-1==k:
if mini>time+1:mini=time+1; cnt=1; visited[mini]=0
elif time+1==mini: cnt+=1;visited[mini]=0
q.appendleft((loc-1, time+1))
if 0<=loc+1<100001 and visited[loc+1]==0:
visited[loc+1]=1
if loc+1==k:
if mini>time+1:mini=time+1; cnt=1;visited[mini]=0
elif time+1==mini: cnt+=1;visited[mini]=0
q.appendleft((loc+1, time+1))
if 0<=loc*2<100001 and visited[loc*2]==0:
visited[loc*2]=1
if loc*2==k:
if mini>time+1:mini=time+1; cnt=1;visited[mini]=0
elif time+1==mini: cnt+=1;visited[mini]=0
q.appendleft((loc*2, time+1))
print(mini)
print(cnt)
from collections import deque
import sys
n,k= map(int, sys.stdin.readline().rstrip().split())
q=deque()
q.append((n, 0))
mini=1000001
cnt=0
visited=[0 for _ in range(100001)]
visited[n]=1
case=[-1,1]
while q :
now = q.pop()
loc=now[0] ; time=now[1]
for i in range(3) :
if i==2:
tmploc=loc*2
else :
tmploc=loc+case[i]
#print(tmploc, q)
if 0<=tmploc<100001 and visited[tmploc]==0:
visited[tmploc]=1
if tmploc==k:
if mini>time+1:mini=time+1; cnt=1; visited[tmploc]=0
elif time+1==mini: cnt+=1;visited[tmploc]=0
if tmploc==2*loc: visited[tmploc]=0
q.appendleft((tmploc, time+1))
print(mini)
print(cnt)