import sys import heapq input = sys.stdin.readline N,M = map(int,input().split()) dp = [-1]*100001 stack = [] dp[N] = 0 root_dict = {} root_dict[N] = -1 heapq.heappush(stack,(0,N)) if N == M: print(0) print(N) else: flag = False while stack: cnt, x = heapq.heappop(stack) for ind,nx in enumerate([2*x,x-1,x+1]): if 0<=nx<100001: if dp[nx] == -1: dp[nx] = cnt + 1 root_dict[nx] = x heapq.heappush(stack,(cnt+1,nx)) if nx == M: find_route = [nx] cu_route = nx while cu_route != N: cu_route = root_dict[cu_route] find_route.append(cu_route) flag = True print(cnt+1) print(*reversed(find_route)) break if flag: break
import sys from collections import deque input = sys.stdin.readline N,M = map(int,input().split()) dp = [-1]*100001 stack = deque() dp[N] = 0 root_list = [-1]*100001 stack.append((0,N)) if N > M: print(N-M) print(' '.join(map(str,range(N,M-1,-1)))) elif N == M: print(0) print(N) else: flag = False while stack: cnt, x = stack.popleft() for ind,nx in enumerate([2*x,x-1,x+1]): if 0<=nx<100001: if dp[nx] == -1: dp[nx] = cnt + 1 root_list[nx] = x stack.append((cnt+1,nx)) if nx == M: find_route = [nx] cu_route = nx while cu_route != N: cu_route = root_list[cu_route] find_route.append(cu_route) flag = True print(cnt+1) print(*reversed(find_route)) break if flag: break
'알고리즘 > 백준_복기_미완료' 카테고리의 다른 글
[BOJ/백준] 14476 최대공약수 하나 빼기 (0) | 2021.05.05 |
---|---|
[BOJ/백준] 13974 파일 합치기 2 (0) | 2021.05.05 |
[BOJ/백준] 13460 구슬 탈출 2 (0) | 2021.05.05 |
[BOJ/백준] 13458 시험감독 (0) | 2021.05.05 |
[BOJ/백준] 13334 철로 (0) | 2021.05.05 |