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.



Sample input:
15


Sample output:
23

Submission status: Accepted

Solution code:
import sys
K = int(input())

cnt = 0
num = 1
while True:
    flag = True
    s_num = str(num)
    diff = 0
    pos = 0
    pos_value = 0
    for i in range(len(s_num)-1, 0, -1):
        diff = int(s_num[i]) - int(s_num[i-1])
        if abs(diff) > 1:
            flag = False
            pos = i
            pos_value = int(s_num[i])
            break
    if flag:
        cnt += 1
    
    if cnt == K:
        print(num)
        sys.exit()
    
    if pos == 0:
#        print(flag, "1:", cnt, num, 1)
        num += 1
    elif diff < 0:
#        print(flag, "-:", cnt, num, (abs(diff) - 1) * (10**(len(s_num)-pos-1)))
        num += (abs(diff) - 1) * (10**(len(s_num)-pos-1))
    else:
        if pos_value != 9:
#            print(flag, "+:", cnt, num, (10 - pos_value - 1) * (10**(len(s_num)-pos-1)) )
            num += (10 - pos_value - 1) * (10**(len(s_num)-pos-1))
        else:
#            print(flag, "+:", cnt, num, (10 - pos_value ) * (10**(len(s_num)-pos-1)) )
            num += (10 - pos_value ) * (10**(len(s_num)-pos-1))
            if pos != len(s_num) - 1:
                num -=  int(s_num[pos+1:])
#    print("-:", cnt, num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
# show_flg = True

g = []


def rec(i, s, N):
    # print(s)
    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), 11)
    g = sorted(g)
    # print(len(g))
    print(g[K-1])


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

K = int(input())
limit_list = []
upper_limit = 9
limit_list.append(upper_limit)
t_dict = {i: 1 for i in range(10)}
# 2桁目の場合
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}  # 2桁目

i = 3
max_limit = 10 ** 5
while upper_limit <= max_limit:
    # 3桁目
    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

# K = 208644

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

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

ans_list = []

# i-1桁目のMAX値
t = limit_list[keta - 2]

if keta == 1:
    ans_list = [K]
else:
    for i in keta_list[::-1]:
        # i桁目の数字を決めたい
        if i == keta_list[-1]:
            # iが最大桁の場合の候補の数字
            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)

Submission status: Accepted

Solution code:
"""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()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from collections import deque

k = int(input())

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

x = -1
for _ in range(k):
    # print(f"loop: {_}")
    # print(f"que: {que}")
    x = que.pop()
    # print(f"x: {x}\n")
    r_10 = x % 10
    if r_10 == 0:
        # 10, 100, 1000, ...
        que.appendleft(x * 10)
        que.appendleft(x * 10 + 1)
    elif r_10 == 9:
        # 9, 99, 989, 999, ...
        que.appendleft(x * 10 + x % 10 - 1)
        que.appendleft(x * 10 + x % 10)
    else:
        # 1, 2, ..., 8, 11, 12, 21, 22, 23, ...
        que.appendleft(x * 10 + x % 10 - 1)
        que.appendleft(x * 10 + x % 10)
        que.appendleft(x * 10 + x % 10 + 1)

print(x)

Submission status: Accepted

Solution code:
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
# show_flg = True

g = []


def rec(i, s, N):
    # print(s)
    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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)]

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

    #update
    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_)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque

K=input()

#ポイント：隣接リストがわかればdfsができる
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]

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)))

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

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

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


print(x)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

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()

Submission status: Accepted

Solution code:
# 解説PDF
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
# 解説放送
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)

Submission status: Accepted

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

i = 0
while lun:
    #print(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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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())

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
k = int(input())
R = [[0 for j in range(10)] for d in range(10)] #d:桁数-1, j:先頭の数
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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]))

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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
# show_flg = True

g = []


def rec(i, s, N):
    # print(s)
    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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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 #1桁目
    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)

Submission status: Accepted

Solution code:
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):
                #print(a)
                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()

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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):
        #print(num,len(num),keta)

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

        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()