728x90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from collections import deque T = int(input()) for TC in range(1, T+1): N, M = map(int, input().split()) # 중복 연산 방지!! check = [0] * 987654321 check[N] = 1 result = 0 Q = deque() Q.append((N, 0)) while Q: v1, v2 = Q.popleft() if v1 == M: result = v2 break V =[v1*2, v1+1, v1-1, v1-10] for i in V: if 1 <= i <= 1000000 and check[i] == 0: Q.append((i, v2 + 1)) check[i] = 1 print("#{} {}".format(TC, result)) | cs |
728x90