yukicoder No.3 ビットすごろく
やっと解けたぜ!ヾ(o゚ω゚o)ノ゙ ここまで長かった… from collections import deque N = int ( input ( ) ) que = deque ( [ 1 ] ) now = 1 visited = [ False for i in range ( N + 1 ) ] step = [ - 1 for i in range ( N + 1 ) ] visited [ 1 ] = True step [ 1 ] = 1 step_count = 1 while len ( que ) > 0 : now = que . popleft ( ) step_count = step [ now ] + 1 step_width = bin ( now ) . count ( "1" ) next_front = now + step_width next_back = now - step_width if next_front <= N and visited [ next_front ] is False : que . append ( next_front ) visited [ next_front ] = True step [ next_front ] = step_count if next_back >= 1 and visited [ next_back ] is False : que . append ( next_back ) visited [ next_back ] = True step [ next_back ] = step_count if now == N : print ( step [ N ] ) else : print ( - 1 )