Problem:
 Score : 400 points    Problem Statement  A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:   In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1 .   For example, 1234 , 1 , and 334 are lunlun numbers, while none of 31415 , 119 , or 13579 is.  You are given a positive integer K . Find the K -th smallest lunlun number.      Constraints   1 \leq K \leq 10^5  All values in input are integers.         Input  Input is given from Standard Input in the following format:  K       Output  Print the answer.        Sample Input 1 15      Sample Output 1 23  We will list the 15 smallest lunlun numbers in ascending order:  1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 21 , 22 , 23 . Thus, the answer is 23 .       Sample Input 2 1      Sample Output 2 1       Sample Input 3 13      Sample Output 3 21       Sample Input 4 100000      Sample Output 4 3234566667  Note that the answer may not fit into the 32 -bit signed integer type.

Solution (Python):


K = int(input())
limit_list = []
upper_limit = 9
limit_list.append(upper_limit)
t_dict = {i: 1 for i in range(10)}

s_dict = {k: 3 for k in range(1, 9)}
s_dict[9] = 2
s_dict[0] = 2
upper_limit = upper_limit + sum(s_dict.values()) - s_dict[0]
limit_list.append(upper_limit)
ww_dict = {1: t_dict, 2: s_dict}  

i = 3
max_limit = 10 ** 5
while upper_limit <= max_limit:
    
    w_dict = {
        k: ww_dict[i - 1][k - 1] + ww_dict[i - 1][k] + ww_dict[i - 1][k + 1]
        for k in range(1, 9)
    }
    w_dict[0] = ww_dict[i - 1][0] + ww_dict[i - 1][1]
    w_dict[9] = ww_dict[i - 1][8] + ww_dict[i - 1][9]
    ww_dict[i] = w_dict
    upper_limit = limit_list[-1] + sum(w_dict.values()) - w_dict[0]
    limit_list.append(upper_limit)
    i += 1



for i, l in enumerate(limit_list):
    if l >= K:
        keta = i + 1
        break

keta_list = list(range(1, keta + 1))

ans_list = []


t = limit_list[keta - 2]

if keta == 1:
    ans_list = [K]
else:
    for i in keta_list[::-1]:
        
        if i == keta_list[-1]:
            
            number_list = list(range(1, 10))
        else:
            if ans_list[-1] == 9:
                max_n = 9
            else:
                max_n = ans_list[-1] + 1
            min_n = 0 if ans_list[-1] == 0 else ans_list[-1] - 1
            number_list = list(range(min_n, max_n + 1))
        for n in number_list:
            if i == 1:
                w = t + 1
            else:
                w = t + ww_dict[i][n]
            if w >= K:
                ans_list.append(n)
                break
            
            t = w

ans = "".join([str(i) for i in ans_list])
print(ans)

Solution (Python):
"""1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 21, 22, 23, 32, 33, 34, 43, 44, 45, 54, 55, 56, 65, 66, 67, 76, 77, 78, 87, 88, 89, 98, 99, 
100, 101, 110, 111, 112, 121, 122, 123, 210, 211, 212, 221, 222, 223, 232, 233, 234, 321, 322, 323, 33

1 -> 10,11,12 -> 100,101 ,110,111,112, 121,122,123

2 -> 21,22,23 -> 210,211,212, 221,222,223, 232,233,234"""
def main():
    n = int(input())
    
    queue = [i for i in range(1,10)]
    if n < 10:
        print(queue[n-1])
        return
    n -= 9
    while True:
        if n <= 0:
            print(queue[n-1])
            return
        tmp = queue.pop(0)
        if tmp%10 == 0:
            queue += [tmp*10 + tmp%10, tmp*10 + tmp%10 + 1]
            n -= 2
        elif tmp%10 == 9:
            queue += [tmp*10 + tmp%10 - 1, tmp*10 + tmp%10]
            n -= 2
        else:
            queue += [tmp*10 + tmp%10 - 1, tmp*10 + tmp%10, tmp*10 + tmp%10 + 1]
            n -= 3

main()

Solution (Python):


from collections import deque

k = int(input())

que = deque(reversed([i for i in range(1, 10)]))

x = -1
for _ in range(k):
    
    
    x = que.pop()
    
    r_10 = x % 10
    if r_10 == 0:
        
        que.appendleft(x * 10)
        que.appendleft(x * 10 + 1)
    elif r_10 == 9:
        
        que.appendleft(x * 10 + x % 10 - 1)
        que.appendleft(x * 10 + x % 10)
    else:
        
        que.appendleft(x * 10 + x % 10 - 1)
        que.appendleft(x * 10 + x % 10)
        que.appendleft(x * 10 + x % 10 + 1)

print(x)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False


g = []


def rec(i, s, N):
    
    global g
    g.append(int(s))
    if len(s) > N:
        return

    if i - 1 >= 0:
        rec(i-1, s + str(i-1), N)
    rec(i, s + str(i), N)
    if i + 1 <= 9:
        rec(i+1, s + str(i+1), N)


def main():
    global g
    K = I()
    ans = []

    for i in range(1, 10):
        rec(i, str(i), 10)
    g = sorted(g)
    print(g[K-1])


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import os

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    K = ii()
    q = [1, 2, 3, 4, 5, 6, 7, 8, 9]

    for k in range(0, K):
        x = q[k]
        mod = x % 10
        if mod != 0:
            q.append(10 * x + mod - 1)
        q.append(10 * x + mod)
        if mod != 9:
            q.append(10 * x + mod + 1)

    print(q[K-1])


if __name__ == '__main__':
    main()

Solution (Python):
k = int(input())

def calc_dp(n):
    if(n==0):
        return 0

    s = str(n)
    slen = len(s)
    dp = [[0] * 12 for _ in range(slen)]
    dp_o = [[0] * 12 for _ in range(slen)]

    
    for i in range(1,int(s[0])):
        dp[0][i+1] = 1
    dp_o[0][int(s[0])+1] = 1

    
    for i in range(slen-1):
        for j in range(1,11):
            dp[i+1][j] = dp[i][j-1] + dp[i][j] + dp[i][j+1] +1
        dp[i+1][1] -= 1

        s_b = int(s[i])
        s_n = int(s[i+1])

        for j in range( max(0,s_b-1), min(10, s_b+2)  ):
            if(j < s_n ):
                dp[i+1][j+1] += dp_o[i][s_b+1]
            elif(j == s_n):
                dp_o[i+1][j+1] += dp_o[i][s_b+1]

    ans = 0
    for i in range(1,11):
        ans += dp[-1][i] + dp_o[-1][i]

    return ans

min_ = 0
max_ = 3234566676


for i in range(100):
    tmp = (min_+max_)//2
    tmp_c = calc_dp(tmp)
    if(k > tmp_c):
        min_ = tmp
    else:
        max_ = tmp

    if(max_ - min_ == 1 ):
        break

print(max_)

Solution (Python):
k = int(input())
cnt = 0

lun = [1, 2, 3, 4, 5, 6, 7, 8, 9]
i = 0
while True:
    ans = lun[i]
    a0 = ans % 10
    if a0 == 0:
        lun.append(ans*10+a0)
        lun.append(ans*10+a0+1)
    elif a0 == 9:
        lun.append(ans*10+a0-1)
        lun.append(ans*10+a0)
    else:
        lun.append(ans*10+a0-1)
        lun.append(ans*10+a0)
        lun.append(ans*10+a0+1)
    i += 1
    if i >= k:
        break
print(ans)

Solution (Python):

import sys
from collections import deque

K=input()


L=[]
q=deque()
for i in range(1,10):
    L.append(i)
    q.append(i)
cnt=0
while q and cnt<10**5:
    fro=q.popleft()
    ichi_no_kurai=fro%10
    for i in range(max(0,ichi_no_kurai-1),min(ichi_no_kurai+2,10)): 
        L.append(fro*10+i)
        q.append(fro*10+i)
        cnt+=1

print L[K-1]

Solution (Python):
k = int(input())
 
qu = list(range(1,10))
 
if (k <= 9):
  print(k)
else:
  cnt = 9
  while( cnt < k ):
    out = qu.pop(0)
    r = out%10
    y = 10*out+r
    if( r != 0 ):
      qu.append(y-1)
      cnt += 1
      if( cnt == k ):
        out = y-1
        break
    qu.append(y)
    cnt += 1
    if( cnt == k ):
      out = y
      break

    
    if( r!=9):
      qu.append(y+1)
      cnt+=1
      if( cnt == k ):
        out = y+1
        break
      

  print(out)

Solution (Python):
K = int(input())

from collections import deque
d = deque()
for i in range(1,10):
  d.append(str(i))

for _ in range(K):
  a = d.popleft()
  
  if a[-1] != '0':
    d.append(a+str(int(a[-1])-1))
  d.append(a+a[-1])
  if a[-1] != '9':
    d.append(a+str(int(a[-1])+1))

ans = a
print(ans)

Solution (Python):
from collections import deque

K = int(input())

q = deque()

for i in range(1, 10):
    q.appendleft(i)


cnt = 0

while True:
    cnt += 1
    temp = q.pop()
    st = str(temp)
    med = temp*10 + int(st[-1])
    if temp % 10 != 0:
        q.appendleft(med - 1)
    q.appendleft(med)
    if temp % 10 != 9:
        q.appendleft(med + 1)

    if cnt == K:
        print(temp)
        break

Solution (Python):
from collections import deque
k = int(input())
q = deque([])
for i in range(1, 10):
    q.append(i)

for i in range(k):
    a = q.popleft()
    if i == k-1:
        print(a)
        exit()
    b = str(a)
    if b[-1] != '0':
        q.append(int(b + str(int(b[-1])-1)))
    q.append(int(b + b[-1]))
    if b[-1] != '9':
        q.append(int(b + str(int(b[-1])+1)))

Solution (Python):
from collections import deque
K = int(input())
q = deque([i for i in range(1, 10)])
cnt = 1
while q:
  n = q.popleft()
  if cnt == K:
    print(n)
    exit()
  if n % 10  != 0:
    q.append(10 * n + n % 10 - 1)
  q.append(10 * n + n % 10)
  if n % 10 != 9:
    q.append(10 * n + n % 10 + 1)
  cnt += 1

Solution (Python):
K = int(input())

if K <= 9:
    print(K)
else:
    mylun = 9
    lunList = []

    for indx in range(1, 10):
        lunList.append(str(indx))

    while mylun < K:
        newLunList = []
        for lunItem in lunList:
            if lunItem[len(lunItem)-1] != "0":
                mylun += 1
                newLunList.append(lunItem + str( int(lunItem[len(lunItem)-1])-1 ))
                if mylun == K:
                    print(lunItem + str( int(lunItem[len(lunItem)-1])-1 ))
                    break
            
            mylun += 1
            newLunList.append(lunItem + str( int(lunItem[len(lunItem)-1]) ))
            if mylun == K:
                print(lunItem + str( int(lunItem[len(lunItem)-1]) ))
                break

            if lunItem[len(lunItem)-1] != "9":
                mylun += 1
                newLunList.append(lunItem + str( int(lunItem[len(lunItem)-1])+1 ))
                if mylun == K:
                    print(lunItem + str( int(lunItem[len(lunItem)-1])+1 ))
                    break
        
        lunList = newLunList

Solution (Python):
import heapq

K=int(input())
q=[1,2,3,4,5,6,7,8,9]
count=0
while K>count:
    x=heapq.heappop(q)
    count+=1
    if count==K:
        print(x)
    else:
        a=x%10
        if a!=0:
            heapq.heappush(q,10*x+a-1)
        heapq.heappush(q,10*x+a)
        if a!=9:
            heapq.heappush(q,10*x+a+1)

Solution (Python):
k = int(input())

Queue = [i for i in range(1, 10, 1)]

cnt = 0

while(cnt < k) : 
    cnt += 1
    x = Queue.pop(0)
    
    if cnt + len(Queue) >= k : 
        continue
    
    xx = x
    if len(str(xx)) >= 2 : 
        while(len(str(xx)) >= 2) : 
            xx = xx % 10

    
    if xx != 0 : 
        y = int("".join(map(str, [x, xx-1])))
        Queue.append(y)
        
    
    y = int("".join(map(str, [x, xx])))    
    Queue.append(y)

    
    if xx != 9 : 
        y = int("".join(map(str, [x, xx+1])))
        Queue.append(y)


print(x)

Solution (Python):



def main():
  k = int(input())
  numbers = [i for i in range(1,10)]
  n = 0
  while True:
    n += len(numbers)
    if k <= n:
      break
    _numbers = []
    for number in numbers:
      if number % 10 != 0:
        _numbers.append(10*number + number%10 - 1)  
      _numbers.append(10*number + number%10)
      if number % 10 != 9:
        _numbers.append(10*number + number%10 + 1)
      numbers = _numbers
      
  print(numbers[k-n-1])
    
if __name__=='__main__':
  main()

Solution (Python):

from collections import deque

k = int(input())

que = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])

for i in range(k-1):
    x = que.popleft()
    y = x % 10  
    if y != 0:
        que.append(10*x+y-1)
    que.append(10*x+y)
    if y != 9:
        que.append(10*x+y+1)

ans = que.popleft()
print(ans)

Solution (Python):
from collections import deque
K = int(input())
q = deque([i for i in range(1, 10)])
cnt = 1
while q:
  n = q.popleft()
  if cnt == K:
    print(n)
    exit()
  if n % 10 - 1 >= 0:
    q.append(10 * n + n % 10 -1)
  q.append(10 * n + n % 10)
  if n % 10 + 1 <= 9:
    q.append(10 * n + n % 10 + 1)
  cnt += 1

Solution (Python):

k = int(input())
a = [i+1 for i in range(9)]


while 1:
    if k <= len(a):
        print(a[k-1])
        exit()
    k -= len(a)
    old = []
    a, old = old, a
    for x in old:
        for i in range(-1, 2):
            d = x % 10 + i
            if d < 0 or d > 9: continue
            nx = x*10 + d
            a.append(nx)

Solution (Python):
K = int(input())
from collections import deque
lun = deque(['1', '2', '3', '4', '5', '6', '7', '8', '9'])

i = 0
while lun:
    
    i += 1
    a = lun.popleft()
    if i == K:
        print(int(a))
        break
    if int(a[-1]) == 9:
        lun.append(a + '8')
        lun.append(a + '9')
    elif int(a[-1]) == 0:
        lun.append(a + '0')
        lun.append(a + '1')
    else:
        lun.append(a + str(int(a[-1]) - 1))
        lun.append(a + str(int(a[-1])))
        lun.append(a + str(int(a[-1]) + 1))

Solution (Python):
from collections import deque
a=int(input())
q=deque([1,2,3,4,5,6,7,8,9])
def dfs(k):
  while k<=a:
    s=q.popleft()
    if s%10-1>=0:
      q.append(s*10+s%10-1)
      k=k+1
      if k==a:
        print(q.pop())
    q.append(s*10+s%10)
    k=k+1
    if k==a:
        print(q.pop())
    if s%10+1<=9:
      q.append(s*10+s%10+1)
      k=k+1
      if k==a:
        print(q.pop())
  
 
if a<=9:
  print(a)
else:
  dfs(9)

Solution (Python):
from collections import deque

k = int(input())
Runrun = deque()
for i in range(9):
    Runrun.append(i + 1)
while k > 1:
    t = Runrun.popleft()
    r = t % 10
    if r != 0:
        Runrun.append(10 * t + r - 1)
    Runrun.append(10 * t + r)
    if r != 9:
        Runrun.append(10 * t + r + 1)
    k -= 1
print(Runrun.popleft())

Solution (Python):
from collections import deque

k = int(input().strip())

q=deque()
for i in range(1,10):
    q.append(i)
    if i==k:
        print(i)
        exit()
i=1
while True:
    old=q.popleft()
    if i==k:
        ans=old
        break
    last=old%10
    if last!=0:
        q.append(old*10+last-1)
    q.append(old*10+last)
    if last!=9:
        q.append(old*10+last+1)
    i+=1
print(ans)

Solution (Python):
k = int(input())
R = [[0 for j in range(10)] for d in range(10)] 
R_total = [[0 for j in range(10)] for d in range(10)] 
for j in range(10):
    R[0][j] = 1
    R_total[0][j] = j
for d in range(1, 10):
    R[d][0] = R[d - 1][0] + R[d - 1][1]
    R_total[d][0] = R_total[d - 1][9]
    for j in range(1, 9):
        R[d][j] = R[d - 1][j - 1] + R[d - 1][j] + R[d - 1][j + 1]
        R_total[d][j] = R_total[d][j - 1] + R[d][j]
    R[d][9] = R[d - 1][8] + R[d - 1][9]
    R_total[d][9] = R_total[d][8] + R[d][9]
k_digit = 0
k_top = 1
while k > R_total[k_digit][9]:
    k_digit += 1
while k > R_total[k_digit][k_top]:
    k_top += 1
k_d = k_digit
k_j = k_top
answer =  k_top * (10 ** (k_digit))
while k_d:
    k_d -= 1
    if k_j == 9:
        k = k - (R_total[k_d + 1][9] - R_total[k_d][9])
        if k <= R_total[k_d][8]:
            k_j = 8
    else:
        k = k - (R_total[k_d + 1][k_j] - R_total[k_d][k_j + 1])
        if k_j == 0:
            if k > R_total[k_d][0]:
                k_j = 1
        else:
            if k <= R_total[k_d][k_j - 1]:
                k_j -= 1
            elif k > R_total[k_d][k_j]:
                k_j += 1
    answer += k_j * (10 ** k_d)
print(answer)

Solution (Python):
def next(num):
    s = str(num)
    length = len(s)

    index = -1

    for i in range(length - 1):
        if (s[i] > s[i + 1]) or (int(s[i]) <= 8 and s[i] == s[i + 1]):
            index = i

    ans = ""
    if index != -1:
        for i in range(length):
            if i <= index:
                ans += s[i]
            elif i == index + 1:
                ans += str(int(s[i]) + 1)
            else:
                if ans[-1] == "0":
                    ans += "0"
                else:
                    ans += str(int(ans[-1]) - 1)
    
    else:
        for i in range(length):
            if i == 0:
                ans += str(int(s[i]) + 1)
            else:
                if ans[-1] == "0":
                    ans += "0"
                else:
                    ans += str(int(ans[-1]) - 1)

    return int(ans)



K = int(input())

ans = 0
while K > 0:
    ans = next(ans)
    K -= 1

print(ans)

Solution (Python):
k = int(input())

keta = 1
_list = [0]
p = keta-1

while k > 0:
    k -= 1
    if keta == 1:
        _list[0] += 1
    else:
        while _list[p]-1 == _list[p-1] or _list[p] == 9:
            p -= 1
            if p == 0:
                break
        _list[p] += 1
        for i in range(p+1, keta):
            _list[i] = (_list[i-1]-1) * (_list[i-1] > 0)
    if _list[0] == 10:
        _list = [1] + [0]  * keta
        keta += 1
    p = keta-1

for num in _list:
    print(num, end="")
print()

Solution (Python):
from collections import deque

k = int(input())
q = deque(range(1, 10))
 
for i in range(k):
    x = q.popleft()
    if x % 10 != 0:
        q.append(10 * x + x % 10 - 1) 

    q.append(10 * x + x % 10) 

    if x % 10 != 9:
        q.append(10 * x + x % 10 + 1) 
 
print(x)

Solution (Python):
K = int(input())
count = 9
lunlun = [1,2,3,4,5,6,7,8,9]
i = 0

while count < K:
  if (lunlun[i]%10) != 0:
    lunlun.append(lunlun[i]*10+lunlun[i]%10-1)
    count = count + 1
  lunlun.append(lunlun[i]*10+lunlun[i]%10)
  count = count + 1
  if (lunlun[i]%10) != 9:
    lunlun.append(lunlun[i]*10+lunlun[i]%10+1)
    count = count+1
  i= i+1
print(lunlun[K-1])

Solution (Python):
K = int(input())

def B(m,n):
  A = []
  if n == 1:
    A.append(str(m))
  else:
    if m == 0:
      for i in range(0,2,1):
        for j in B(i,n-1):
          A.append(str(0) + j)
    elif m == 9:
      for i in range(8,10,1):
        for j in B(i,n-1):
          A.append(str(9) + j)
    else:
      for i in range(m-1,m+2,1):
        for j in B(i,n-1):
          A.append(str(m) + j)
  return A

C = []
for j in range(1,11,1):
  for i in range(1,10,1):
    C += B(i,j)

print(int(C[K-1]))

Solution (Python):
k = int(input())
luns = [i for i in range(1, 10)]

def generate_lunlun(p):
    end = p % 10
    if end - 1 >= 0:
        yield 10 * p + end - 1
    yield 10 * p + end
    if end + 1 <= 9:
        yield 10 * p + end + 1

p = 0
while len(luns) < k:
    for i in generate_lunlun(luns[p]):
        luns.append(i)
    p += 1
print(luns[k - 1])

Solution (Python):
from collections import deque
k = int(input())
lunlun = deque(range(1, 10))
for i in range(k):
    x = lunlun.popleft()
    next = 10*x + (x % 10)
    if x % 10 != 0:
        lunlun.append(next - 1)
    lunlun.append(next)
    if x % 10 != 9:
        lunlun.append(next + 1)
print(x)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False


g = []


def rec(i, s, N):
    
    global g
    g.append(int(s))
    if len(s) > N:
        return

    if i - 1 >= 0:
        rec(i-1, s + str(i-1), N)
    rec(i, s + str(i), N)
    if i + 1 <= 9:
        rec(i+1, s + str(i+1), N)


def main():
    global g
    K = I()
    ans = []

    for i in range(1, 10):
        rec(i, str(i), 9)
    g = sorted(g)
    print(g[K-1])


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

k = int(input())

q = deque([i for i in range(1, 10)])

cnt = 0
while True:
    cnt += 1
    v = q.popleft()
    if cnt == k:
        print(v)
        exit()
    m = v % 10
    if m != 0:
        q.append(10*v + m - 1)
    q.append(10*v + m)
    if m != 9:
        q.append(10*v + m + 1)

Solution (Python):
from collections import deque
k = int(input())
q=deque([1,2,3,4,5,6,7,8,9])
for i in range(k-1):
    x = q.popleft()
    y = x%10 
    if y != 0:
        q.append(10*x+y-1)
    q.append(10*x+y)
    if y != 9:
        q.append(10*x+y+1)
ans = q.popleft()
print(ans)

Solution (Python):
import numpy as np
def resolve():
    k = int(input())
    n = 20
    l = [[] for _ in range(n)]
    size = 0
    pre_size = 0

    for i in range(1, 10, 1):
        l[0].append(i)
    
    size += len(l[0])
    for j in range(n-1):
        if size >= k:
            break
        for var in l[j]:
            v = var % 10
            for a in range(v-1, v+2, 1):
                
                if a >= 0 and a <= 9:
                    l[j+1].append(var*10 + a)
        size += len(l[j+1])
        pre_size += len(l[j])

    print(l[j][k - pre_size - 1])

if __name__ == "__main__":
    resolve()

Solution (Python):
K = int(input())
d = [1,2,3,4,5,6,7,8,9]

if K <= 9:
  print(K)
  exit()

cnt = 9
while cnt < K:
  _ = d.pop(0)
  matsubi = _%10
  
  if matsubi == 0:
    d.append(_*10+matsubi)
    d.append(_*10+matsubi+1)
    cnt += 2

  elif matsubi == 9:
    d.append(_*10+matsubi-1)
    d.append(_*10+matsubi)
    cnt += 2

  else:
    d.append(_*10+matsubi-1)
    d.append(_*10+matsubi)
    d.append(_*10+matsubi+1)
    cnt += 3
      
ans = -1*(cnt - K + 1)
print(d[ans])

Solution (Python):
import queue
N = int(input())

q =  queue.Queue()
for i in range(1,10):
    q.put(i)

i = 0
while True:
    out = q.get()
    i += 1
    if i == N:
        break
    b = out*10
    if out%10 > 0:
        q.put(b+out%10-1)
    q.put(b+out%10)
    if out%10 < 9:
        q.put(b+out%10+1)
    
print(out)

Solution (Python):
def dfs(inParam, digits, lunlunCount, lunlun):
    if len(inParam) != digits:
        if int(inParam[len(inParam)-1]) == 0:
            nextNumList = [0, 1]
        elif int(inParam[len(inParam)-1]) == 9:
            nextNumList = [8, 9]
        else:
            nextNum = int(inParam[len(inParam)-1])
            nextNumList = [nextNum-1, nextNum, nextNum+1]
        
        for item in nextNumList:
            myTupp = dfs(inParam+str(item), digits, lunlunCount, lunlun)
            lunlunCount = myTupp[0]
            if myTupp[2] == 'e':
                return myTupp
        
        return myTupp
    
    else:
        lunlunCount += 1
        state = 'c'

        if lunlunCount == lunlun:
            state = 'e'
            print(inParam)

        return (lunlunCount, inParam, state)

K = int(input())

mylunlun = 0
mydigits = 1
while mylunlun != K:
    for indx in range(1, 10):
        mylunlun, lunlunNumber, _ = dfs(str(indx), mydigits, mylunlun, K)
        
        
    
    if mylunlun >= K:
        break

    mydigits += 1

Solution (Python):
K = int(input())
d = [1,2,3,4,5,6,7,8,9]

if K <= 9:
  print(K)
  exit()

cnt = 9
while cnt < K:
  _ = d.pop(0)
  matsubi = _%10
  
  if matsubi == 0:
    d.append(_*10+matsubi)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()
    d.append(_*10+matsubi+1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()
    
  elif matsubi == 9:
    d.append(_*10+matsubi-1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

    d.append(_*10+matsubi)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()
  else:
    d.append(_*10+matsubi-1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

    d.append(_*10+matsubi)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

    d.append(_*10+matsubi+1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

Solution (Python):
K = int(input())
d = [1,2,3,4,5,6,7,8,9]

if K <= 9:
  print(K)
  exit()

cnt = 9
while cnt < K:
  _ = d.pop(0)
  matsubi = _%10
  
  if matsubi == 0:
    d.append(_*10+matsubi)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()
    d.append(_*10+matsubi+1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()
    
  elif matsubi == 9:
    d.append(_*10+matsubi-1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

    d.append(_*10+matsubi)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()
  else:
    d.append(_*10+matsubi-1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

    d.append(_*10+matsubi)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

    d.append(_*10+matsubi+1)
    cnt += 1
    if cnt == K:
      print(d[-1])
      exit()

Solution (Python):
from collections import deque
k = int(input())
lunlun = deque([i for i in range(9, 0, -1)])
for i in range(k):
    x = lunlun.pop()
    if i == k - 1:
        print(x)
    next_lunlun = 10*x + (x % 10)
    if x % 10 != 0:
        lunlun.appendleft(next_lunlun - 1)
    lunlun.appendleft(next_lunlun)
    if x % 10 != 9:
        lunlun.appendleft(next_lunlun + 1)

Solution (Python):
def dfs(inParam, digits, lunlunCount, lunlun):
    if len(inParam) != digits:
        if int(inParam[len(inParam)-1]) == 0:
            nextNumList = [0, 1]
        elif int(inParam[len(inParam)-1]) == 9:
            nextNumList = [8, 9]
        else:
            nextNum = int(inParam[len(inParam)-1])
            nextNumList = [nextNum-1, nextNum, nextNum+1]
        
        for item in nextNumList:
            myTupp = dfs(inParam+str(item), digits, lunlunCount, lunlun)
            lunlunCount = myTupp[0]
            if myTupp[2] == 'e':
                return myTupp
        
        return myTupp
    
    else:
        lunlunCount += 1
        state = 'c'

        if lunlunCount == lunlun:
            state = 'e'
            print(inParam)

        return (lunlunCount, inParam, state)

K = int(input())

mylunlun = 0
mydigits = 1
while mylunlun != K:
    for indx in range(1, 10):
        mylunlun, lunlunNumber, _ = dfs(str(indx), mydigits, mylunlun, K)
        if mylunlun == K:
            break
    
    if mylunlun == K:
        break

    mydigits += 1

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from copy import deepcopy

def main():
    k = int(input())
    l = []
    ll = [1,2,3,4,5,6,7,8,9]

    def dfs(num, keta):
        

        if len(num) == keta:
            ll.append(int(num))
            

        else:
            if num[-1] == '0':
                dfs(num + '0', keta)
                dfs(num + '1', keta)
            elif num[-1] == '9':
                dfs(num + '9', keta)
                dfs(num + '8', keta)
            else:
                dfs(num + str(int(num[-1]) + 1), keta)
                dfs(num + str(int(num[-1])), keta)
                dfs(num + str(int(num[-1]) - 1), keta)
    for i in range(2,10000):
        l = l + ll
        if len(l) > k:
            l.sort()
            print(l[k-1])
            break
        else:
            tmp_ll = deepcopy(ll)
            ll = []
            for el in tmp_ll:
                dfs(str(el), i)



if __name__ == '__main__':
    main()

Solution (Python):
k=int(input())

def count(digit,num):
    if digit==1:
        return 1
    c=0
    if num!=0:
        c+=count(digit-1,num-1)
    c+=count(digit-1,num)
    if num!=9:
        c+=count(digit-1,num+1)
    return c


def answer(digit,num,i,ans):
    
    if digit==2:
        add=(i+num-2)
        if num==0:
            add+=1
        return ans*10+add
    if num!=0:
        c=count(digit-1,num-1)
        if c>=i:
            return answer(digit-1,num-1,i,ans*10+num-1)
        i-=c
    c=count(digit-1,num)
    if c>=i:
        return answer(digit-1,num,i,ans*10+num)
    i-=c
    if num!=9:
        c=count(digit-1,num+1)
        if c>=i:
            return answer(digit-1,num+1,i,ans*10+num+1)
    return 0

size=9
num=1
digit=2
while True:
    s=count(digit,num)
    if s+size>=k:
        break
    size+=s
    num+=1
    if num==10:
        num=1
        digit+=1
ans=0
if k<=9:
    ans=k
else:
    ans=answer(digit,num,k-size,num)
print(ans)

Solution (Python):
from collections import deque


k = int(input())
q = deque(range(1, 10))

for i in range(k):
    x = q.popleft()
    if x % 10 != 0:
        q.append(10 * x + x % 10 - 1)
    q.append(10 * x + x % 10)
    if x % 10 != 9:
        q.append(10 * x + x % 10 + 1)

print(x)

Solution (Python):



def calcComb(n, dnum, dc):
    return sum(dc[n][dnum - 1])

def lunNum(n, dnum, remain, dc):
    sl = len(str(n))
    if dnum == sl:
        return n

    
    rd = dnum - sl
    ld = n % 10
    
    if ld > 0:
        c = calcComb(ld - 1, dnum - sl, dc)
        if remain > c:
            remain -= c
        else:
            return lunNum(n * 10 +  ld - 1, dnum, remain, dc)
        
    c = calcComb(ld, dnum - sl, dc)
    if remain > c:
        remain -= c
    else:
        return lunNum(n * 10 + ld, dnum, remain, dc)

    if ld == 9:
        print("Oops!!")
        return -1

    return lunNum(n * 10 + ld + 1, dnum, remain, dc)

k = int(input())

dc = {}

for i in range(10):
    l = [ 1 if i == j else 0 for j in range(10) ]
    dc[i] = [l]


    
dnum = 1
s = 0
ps = 0
ck = 0
while True:
    out = False
    for key in range(1, 10):
        ck = key
        l = dc[key][dnum - 1]
        ps = s        
        s += sum(l)
        if k <= s:
            out = True
            break

    if out:
        break
    
    
    for key in dc:
        l = dc[key][dnum - 1]
        nl = [0 for i in range(10)]

        for i in range(10):
            nl[i] += l[i]
            if i > 0:
                nl[i] += l[i - 1]
            if i < 9:
                nl[i] += l[i + 1]
        dc[key].append(nl)

    dnum += 1


remain = k - ps

print(lunNum(ck, dnum, remain, dc))

Solution (Python):
K=int(input())
count=0
from collections import deque


def bfs():
  count=0
  d=deque()
  for i in range(1,10):
    d.append(str(i))
    count+=1
    if count==K:
      print(i)
      return
  while count<K:
    S=d.popleft()
    if S[-1]=="9":
      for i in [8,9]:
        d.append(S+str(i))
        count+=1
        if count==K:
          print(S+str(i))
          break
    elif S[-1]=="0":
      for i in [0,1]:
        d.append(S+str(i))
        count+=1
        if count==K:
          print(S+str(i))
          break
    else:
      s=int(S[-1])
      for i in [s-1,s,s+1]:
        d.append(S+str(i))
        count+=1
        if count==K:
          print(S+str(i))
          break
bfs()