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

l = [1,2,3,4,5,6,7,8,9]
for i in range(k):
    a = l[i] % 10
    if a >= 1: 
        l.append(10*l[i] + a - 1)
    l.append(10*l[i] + a)
    if a <= 8:
        l.append(10*l[i] + a + 1)
print(l[k-1])

Solution (Python):
import queue


K = int(input())

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

for i in range(K):
    x = q.get()
    if x % 10 != 0:
        q.put(10 * x + (x % 10) - 1)
    q.put(10 * x + (x % 10))
    if x % 10 != 9:
        q.put(10 * x + (x % 10) + 1)
print(x)

Solution (Python):

from collections import deque
k = int(input())

def solve(k):
    q = deque()
    ans = 0
    for i in range(1,10):
        q.append(i)
    count = 0
    while count < k:
        val = q.popleft()
        tmp = val % 10
        if tmp != 0:
            q.append(10*val + tmp - 1)
        q.append(10*val + tmp)
        if tmp != 9:
            q.append(10*val + tmp + 1)
        ans = val
        count += 1
    return ans
print(solve(k))

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





all_list=[]

def rec(d,val):
    global all_list
    all_list.append(val)
    if d==10:
        return
    for j in range(-1,2,1):
        add=val%10+j
        if add>=0 and add<=9:
            rec(d+1,val*10+add)

for i in range(1,10):
    rec(1,i)
all_list.sort()
print(all_list[k-1])

Solution (Python):
import queue
import sys

q = queue.Queue()
K = int(input())

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

n = 0
while True:
    n += 1
    val = q.get()
    if n == K:
        print(val)
        sys.exit()

    mod = val%10
    if mod != 0:
        q.put(val*10+mod-1)
    q.put(val*10+mod)
    if mod != 9:
        q.put(val*10+mod+1)

Solution (Python):
import queue
import sys

q = queue.Queue()
K = int(input())

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

n = 0
while True:
    n += 1
    val = q.get()
    if n == K:
        print(val)
        sys.exit()

    mod = val%10
    if mod != 0:
        q.put(val*10+mod-1)
    q.put(val*10+mod)
    if mod != 9:
        q.put(val*10+mod+1)

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

def solve(K):
    q = deque()
    for i in range(1, 10):
        q.append(i)
    
    
    previousval = 0
    idx = 0
    while(idx < K):
        val = q.popleft()
        rem = val%10
        if rem != 0:
            q.append(10*val + rem - 1)
        q.append(10*val + rem)
        if rem != 9:
            q.append(10*val + rem + 1)
        
        previousval = val
        
        idx += 1
    return previousval

print(solve(K))

Solution (Python):
K=int(input())
arr=[[1,2,3,4,5,6,7,8,9]]
for _ in range(9):
    tmp=[]
    for v in arr[-1]:
        if v%10!=0:
            tmp.append(v*10+(v%10-1))
        tmp.append(v*10+(v%10))
        if v%10!=9:
            tmp.append(v*10+(v%10+1))
    arr.append(tmp)
ans=[]
for i in range(len(arr)):
    for v in arr[i]:
        ans.append(v)
ans=sorted(ans)
print(ans[K-1])

Solution (Python):
import queue
K = int(input())
q = queue.Queue()
for i in range(1,10):
    q.put(i)

for i in range(K):
    x = q.get()
    a = x%10
    if a !=0:
        q.put(10*x +a -1)
    q.put(10*x +a )
    if a != 9:
        q.put(10*x +a +1)
print(x)

Solution (Python):
import sys
from math import sqrt
from collections import Counter, defaultdict, deque

input = sys.stdin.readline


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


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


def LI():
    return list(MI())


def LIN(n: int):
    return [I() for _ in range(n)]


inf = float("inf")
mod = 10 ** 9 + 7


def main():
    k = I()

    q = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
    for i in range(1, 10 ** 5 + 1):
        ln = q.popleft()

        if i == k:
            print(ln)
            break

        r = ln % 10
        new_ln = ln * 10 + r
        if r != 0:
            q.append(new_ln - 1)
        q.append(new_ln)
        if r != 9:
            q.append(new_ln + 1)


if __name__ == "__main__":
    main()

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

import itertools

Lun = [[] for _ in range(11)]
Lun[1] = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(2, 11):
    for j in Lun[i-1]:
        j_mod = j % 10
        if j_mod != 0:
            Lun[i].append(j * 10 + j_mod - 1)
        Lun[i].append(j * 10 + j_mod)
        if j_mod != 9:
            Lun[i].append(j * 10 + j_mod + 1)

Lunlun = list(itertools.chain.from_iterable(Lun))
print(Lunlun[K-1])

Solution (Python):
import collections
ans=[]
s=collections.deque()
k=int(input())
ans.append(1)
for i in range(1,k):
    for x in str(ans[i-1]):
        s.append(int(x))
    ls=len(s)-1
    while ls>=0 and s[ls]==9:
        s[ls]=0
        ls-=1
    if ls==-1:s.appendleft(1)
    else:s[ls]+=1
    while True:
        b=-1
        for i in range(0,len(s)-1):
            if abs(s[i+1]-s[i])>1:
                b=i
                break
        if b==-1:break
        if s[b+1]>s[b]:s[b]+=1
        l=max(s[b]-1,0)
        for i in range(b+1,len(s)):
            s[i]=l
            l=max(l-1,0)
    
    t=0
    while len(s)>=1:
        x=s.popleft()
        
        t=t*10+x
    ans.append(t)
print(ans[k-1])

Solution (Python):
import queue
K=int(input())
q = queue.Queue()
for i in range(10):
    q.put(i)
q.get()
X=0
while X<=K:
    X+=1
    A=q.get()
    if X==K:
        print(A)
    if A%10!=0:
        q.put(A*10+A%10-1)
    q.put(A*10+A%10)
    if A%10!=9:
        q.put(A*10+A%10+1)

Solution (Python):
import queue


def main():
    K = int(input())
    number_list = queue.Queue()
    for i in range(1, 10):
        number_list.put(i)
    count = 0
    number = 0
    while(count != K):
        number = number_list.get()
        if number % 10 != 0:
            number_list.put((10 * number) + (number % 10) - 1)
        number_list.put(10 * number + number % 10)
        if number % 10 != 9:
            number_list.put(10 * number + (number % 10) + 1)
        count += 1
    print(number)


if __name__ == "__main__":
    main()

Solution (Python):
import queue

n = int(input())


q = queue.Queue()

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

x = 0
for i in range(n):
    x = q.get()
    l = x % 10

    if l != 0:
        q.put(x*10 + l - 1)
    q.put(x*10 + l)
    if l != 9:
        q.put(x*10 + l + 1)
        


    
print(x)

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

ans = [i + 1 for i in range(9)]

i = 0
while len(ans) <= K:
    s = str(ans[i])
    t = int(s[-1])
    d = [-1, 0, 1]
    if t == 0:
        d = [0, 1]
    elif t == 9:
        d = [-1, 0]

    for x in d:
        ans.append(s + str(t + x))
    i += 1
print(ans[K-1])

Solution (Python):
from collections import deque
K = int(input())
Q = deque([i for i in range(1,10)])

for _ in range(K-1):
  q = Q.popleft()
  if q%10 != 0:
    Q.append(q*10+q%10-1)
  Q.append(q*10+q%10)
  if q%10 != 9:
    Q.append(q*10+q%10+1)
print(Q.popleft())

Solution (Python):


from collections import deque


K = int(input())


num_queue = deque()
k = 1


while True:
    if k>=1 and k<=9:
        num_queue.append(k)
        k += 1
    else:
        num = str(num_queue.popleft())
        num_str = int(num[-1])
        if num_str==0:
            for i in range(2):
                tmp = str(i)
                num_queue.append(int(num + tmp))
                k += 1
                if k-1==K:
                    break
        elif num_str==9:
            for i in range(8,10):
                tmp = str(i)
                num_queue.append(int(num + tmp))
                k += 1
                if k-1==K:
                    break
        else:
            for i in range(num_str-1, num_str+2):
                tmp = str(i)
                num_queue.append(int(num + tmp))
                k += 1
                if k-1==K:
                    break

    if k-1==K:
        break


print(num_queue[-1])

Solution (Python):
import heapq
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
heapq.heapify(l)

def f(x):
    
    min_x = int(list(str(x))[-1])
    x = str(x)
    if min_x == 0:
        yield int(x + "0")
        yield int(x + "1")
    elif min_x == 9:
        yield int(x + "8")
        yield int(x + "9")
    else:
        yield int(x + str(min_x - 1))
        yield int(x + str(min_x))
        yield int(x + str(min_x + 1))

K = int(input())
ans = 1
for i in range(K):
    if i == K - 1:
        ans = heapq.heappop(l)
        break
    for y in f(heapq.heappop(l)):
        heapq.heappush(l, y)
print(ans)

Solution (Python):
from heapq import heappop, heappush

k = int(input())

q = []

for i in range(9):
    heappush(q, i+1)

for _ in range(k - 1):
    x = heappop(q)
    if x % 10 != 0:
        heappush(q, 10 * x + (x % 10) - 1)
    if x % 10 != 9:
        heappush(q, 10 * x + (x % 10) + 1)
    heappush(q, 10 * x + (x % 10))

print(heappop(q))

Solution (Python):
from collections import deque


k = int(input())
d = deque([1,2,3,4,5,6,7,8,9])

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

print(a)

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

Solution (Python):

from collections import deque

k = int(input())
queue = deque([1,2,3,4,5,6,7,8,9])

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

print(a)

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

Solution (Python):
from collections import deque

if __name__ == '__main__':
    k = int(input())
    a = deque([1, 2, 3, 4, 5, 6, 7, 8, 9])
    for i in range(k - 1):
        x = a.popleft()
        if x % 10 != 0:
            a.append(10 * x + (x % 10) - 1)
        a.append(10 * x + (x % 10))
        if x % 10 != 9:
            a.append(10 * x + (x % 10) + 1)
    print(a.popleft())

Solution (Python):
import collections

q = collections.deque(range(1, 10))

K = int(input())

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

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

i = 1
q = []
while i <= k :
    if(i < 10) :
        q.append(i)
    else :
        x = q.pop(0)
        tmp = 10 * x + (x % 10)
        if x % 10 != 0 :
            q.append(tmp-1)
            i+=1
        q.append(tmp)
        if x % 10 != 9 :
            q.append(tmp+1)
            i+=1
    i+=1
print(q.pop(k-i))

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

if K<=10:
    print(K)
else:
    K=K-10
    lun=[n for n in range(1,10)]
    i=0

    for e in lun:

        a=e%10-1
        b=e%10
        c=e%10+1

        if a>=0:
            lun.append(10*e+a)
            i+=1
            if i>K:
                print(lun[-1])
                break

        lun.append(10*e+b)
        i+=1
        if i>K:
            print(lun[-1])
            break

        if c<10:
            lun.append(10*e+c)
            i+=1
            if i>K:
                print(lun[-1])
                break

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


















for i in range(K):
    a = l[i]
    b = a%10
    for j in [b-1, b, b+1]:
        if j<0 or j>9:
            continue
        else:
            l.append(10*a + j)
print(l[K-1])

Solution (Python):

import queue

K = int(input())



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

x = 0
i = 0

while i < K:
    
    x = q.get()

    one = x % 10
    
    if one != 0:
        q.put(x * 10 + one - 1)

    
    q.put(x * 10 + one)

    
    if one != 9:
        q.put(x * 10 + one + 1)

    i += 1


print(x)

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




from collections import deque

q=deque([1,2,3,4,5,6,7,8,9])
ansl=[]
s=1
while len(str(s))<=10:
    s=q.popleft()
    ansl.append(s)
    sl=list(str(s))
    last_digit=int(sl[-1])
    for digit in range(max(0,last_digit-1),min(last_digit+1,9)+1):
        newval=int(str(s)+str(digit))
        
        q.append(newval)
print(ansl[k-1])

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




from collections import deque

q=deque([1,2,3,4,5,6,7,8,9])
ansl=[]
s=1
while len(str(s))<=10:
    s=q.popleft()
    ansl.append(s)
    sl=list(str(s))
    last_digit=int(sl[-1])
    for digit in range(max(0,last_digit-1),min(last_digit+1,9)+1):
        newval=int(str(s)+str(digit))
        
        q.append(newval)
print(ansl[k-1])

Solution (Python):
K = int(input())
l = [1,2,3,4,5,6,7,8,9]
while K>0:
    if len(l) > K:
        print(l[K-1])
        break
    else:
        K -= len(l)
        newl = []
        for i in l:
            if i%10 == 0:
                newl.append(10*i)
                newl.append(10*i + 1)
            elif i%10 == 9:
                newl.append(10*i + 8)
                newl.append(10*i + 9)
            else:
                newl.append(10*i + i%10 - 1)
                newl.append(10*i + i%10 )
                newl.append(10*i + i%10 + 1)
        l = newl

Solution (Python):
from collections import deque
k = int(input())
a = deque([1,2,3,4,5,6,7,8,9])
count=0
while count !=k:
  b = a.popleft()
  count += 1
  if b%10==0:
    a.append(b*10)
    a.append(b*10+1)
  elif b%10==9:
    a.append(b*10+8)
    a.append(b*10+9)
  else:
    a.append(b*10+b%10-1)
    a.append(b*10+b%10)
    a.append(b*10+b%10+1)
print(b)

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


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

Solution (Python):
from collections import deque

def main():
    K = int(input())
    
    
    q = deque(range(1, 10))
    for _  in range(K-1):
        now = q.popleft()
        d1 = now % 10
        base = 10 * now
        
        for suffix in [d1 - 1, d1, d1 + 1]:
            if suffix not in [-1, 10]:
                new = base + suffix
                q.append(new)

    ans = q.popleft()
    print(ans)
    

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

def main():
    K = int(input())
    
    
    count = 0
    q = deque(range(1, 10))
    while count < K-1:
        now = q.popleft()
        count += 1

        d1 = now % 10
        base = 10 * now
        for suffix in [d1 - 1, d1, d1 + 1]:
            if suffix not in [-1, 10]:
                new = base + suffix
                q.append(new)

    ans = q.popleft()
    print(ans)
    

if __name__ == "__main__":
    main()

Solution (Python):
k=int(input())
a=[i+1 for i in range(9)]
while (1):
  if k<=len(a):
    print(a[k-1])
    f=1
    break
  
  k=k-len(a)
  
  b=[]
  for x in a:
    for i in [-1,0,1]:
      d=x%10+i
      
      if d<0 or d>=10: continue
      b.append(x*10+d)
  a=b.copy()

Solution (Python):
import queue

q = queue.Queue()
K = int(input())

for i in range(1, 10):
    q.put(i)
for i in range(K-1):
    x = q.get()
    if x % 10 != 0:
        q.put(10*x+x % 10-1)
    q.put(10*x+x % 10)
    if x % 10 != 9:
        q.put(10*x+x % 10+1)
print(q.get())

Solution (Python):
k=int(input())
a=[]
def judge(x):
    if x> 3234566667:
        return
    a.append(x)
    for i in range(10):
        if abs(x%10-i)<=1:
            judge(x*10+i)

for i in range(1,10):
    judge(i)

a.sort()
print(a[k-1])

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K = int(input())
RUN = {
    0:[0, 1],
    1:[0, 1, 2],
    2:[1, 2, 3],
    3:[2, 3, 4],
    4:[3, 4, 5],
    5:[4, 5, 6],
    6:[5, 6, 7],
    7:[6, 7, 8],
    8:[7, 8, 9],
    9:[8, 9]
}
cnt = -1

def dfs(i, s, p, cnt):
    if i == 0:
        cnt += 1
        if cnt == K:
            print(s)
            exit()
        else:
            return cnt
    if s == 0:
        for ii in range(10):
            cnt = dfs(i-1, s+ii*10**(i-1), ii, cnt)
    else:
        for ii in RUN[p]:
            cnt = dfs(i-1, s+ii*10**(i-1), ii, cnt)

    return cnt

dfs(10, 0, 0, cnt)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K = int(input())
RUN = {
    0:[0, 1],
    1:[0, 1, 2],
    2:[1, 2, 3],
    3:[2, 3, 4],
    4:[3, 4, 5],
    5:[4, 5, 6],
    6:[5, 6, 7],
    7:[6, 7, 8],
    8:[7, 8, 9],
    9:[8, 9]
}
cnt = 0

def dfs(i, s, p, cnt):
    if i == 0:
        if s == 0:
            return cnt        
        cnt += 1
        if cnt == K:
            print(s)
            exit()
        else:
            return cnt
    if s == 0:
        for ii in range(10):
            cnt = dfs(i-1, s+ii*10**(i-1), ii, cnt)
    else:
        for ii in RUN[p]:
            cnt = dfs(i-1, s+ii*10**(i-1), ii, cnt)

    return cnt

dfs(10, 0, 0, cnt)

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

from heapq import heappush, heappop,heappushpop,heapify
ans=[1,2,3,4,5,6,7,8,9]
heapify(ans)

a=0
for i in range(k):
    a=heappop(ans)
    md=a%10
    if md != 0:
        heappush(ans,10*a + md-1)
    if md != 9:
        heappush(ans,10*a + md+1)
    heappush(ans,10*a+md)
print(a)

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

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

while len(lunlun) < k:
    for new_lunlun in generate_lunlun(lunlun[p]):
        lunlun.append(new_lunlun)
    p += 1

print(lunlun[k-1])

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
K=int(input())
la=[]
tmp=0
sans=""
def f(tmp,K,i,j):
    global sans
    

    if j==9:
        ra=range(8,10)
    elif j==0:
        ra=range(2)
    else:
        ra=range(j-1,j+2)
    
    for k in ra:
        tmp+=la[i-1][k]
        if tmp==K and i==1:
            sans+=str(k)
            return
        elif tmp>=K:
            
            
            sans+=str(k)
            
            
            f(tmp-la[i-1][k],K,i-1,k)
            return
        """
        elif tmp==K and i==1:
            sans+=str(k)
            return
        """



for i in range(100):
    la.append([0 for _ in range(10)])
    if i==0:
        
        la[i][0]=1
        for j in range(1,10):
            la[i][j]=1
            tmp+=1
            if tmp==K:
                sans=str(j)
                break
        else:
            continue
        break
    else:
        
        la[i][0]=sum(la[i-1][:2])
        for j in range(1,10):
            if j==9:
                la[i][9]=sum(la[i-1][8:])
            else:
                la[i][j]=sum(la[i-1][j-1:j+2])
            tmp+=la[i][j]
            if tmp>=K:
                
                
                sans+=str(j)
                
                
                tmp-=la[i][j]
                f(tmp,K,i,j)
                break
        else:
            continue
        break


print(int(sans))

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




from collections import deque

q=deque([1,2,3,4,5,6,7,8,9])
ansl=[]
s=1
while len(str(s))<=10:
    s=q.popleft()
    ansl.append(s)
    sl=list(str(s))
    last_digit=int(sl[-1])
    for digit in range(max(0,last_digit-1),min(last_digit+1,9)+1):
        newval=int(str(s)+str(digit))
        
        q.append(newval)
print(ansl[k-1])

Solution (Python):
k=int(input())
a=[]
def judge(x):
    if x> 3234566667:
        return
    a.append(x)
    for i in range(10):
        if abs(x%10-i)<=1:
            judge(x*10+i)

for i in range(1,10):
    judge(i)

a.sort()
print(a[k-1])

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

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

while(1):
    if k <= len(a):
        print (a[k-1])
        break

    b = []
    k = k - len(a)

    for x in a:
        for i in [-1,0,1]:
            d =  x%10  + i
            if d < 0 or d > 9: continue
            b.append( x*10 + d)
    a = b.copy()

Solution (Python):
k = int(input())
a=['1','2','3','4','5','6','7','8','9']
 
while k>len(a):
    b=[]
    for i in a:
        if i[-1]!='0':
            b.append(i+chr(ord(i[-1])-1))
        b.append(i+i[-1])
        if i[-1]!='9':
            b.append(i+chr(ord(i[-1])+1))
    k-=len(a)
    a=b.copy()
 
print(a[k-1])

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

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

while(1):
    if k <= len(a):
        print (a[k-1])
        break

    b = []
    k = k - len(a)
    x = a
    a = b
    b = x

    for x in b:
        for i in [-1,0,1]:
            d =  x%10  + i
            if d < 0 or d > 9: continue
            a.append( x*10 + d)

Solution (Python):


import queue
k = int(input())

q = queue.Queue()

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

for i in range(k):
    tmp = q.get()
    if tmp % 10 != 0:
        q.put(10*tmp + tmp % 10 - 1)
    q.put(10*tmp + tmp % 10)
    if tmp % 10 != 9:
        q.put(10*tmp + tmp % 10 + 1)
print(tmp)

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




def swap(a,b):
    x = a
    a = b
    b = x

    return a,b

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

while(1):
    if k <= len(a):
        print (a[k-1])
        break

    b = []
    k = k - len(a)
    a,b = swap(a,b)

    for x in b:
        for i in [-1,0,1]:
            d =  x%10  + i
            if d < 0 or d > 9: continue
            a.append( x*10 + d)

Solution (Python):

dp = [[0,1,0,0,1,0,0,1,0,0,1,0]]
K = int(input())
sum = 0
deg = 1
flag = False

def deg_runrun(sum,i,deg,K):
    if deg == 0:
        return
    if deg == 1:
        for j in range(i-1,i+2):
            sum += dp[deg][j]
            if sum == K: 
                print(j-1,end='')
                return

    for j in range(i-1,i+2):
        if sum + dp[deg][j] >= K:
            print(j-1,end='')
            deg_runrun(sum, j, deg-1, K)
            return
        sum += dp[deg][j]


while not flag:
    dp.append([0]*12)
    for i in range(1,11):
        dp[deg][i] = dp[deg -1][i-1]+dp[deg-1][i]+dp[deg -1][i+1] 
    for i in range(2,11):
        if sum + dp[deg][i] >= K:
            print(i-1,end='')
            deg_runrun(sum, i, deg-1, K)
            flag = True
            break
        sum += dp[deg][i]
    deg += 1

print('')

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

while k>len(a):
    b=[]
    for i in a:
        if i[-1]!='0':
            b.append(i+chr(ord(i[-1])-1))
        b.append(i+i[-1])
        if i[-1]!='9':
            b.append(i+chr(ord(i[-1])+1))
    k-=len(a)
    a=b.copy()

print(a[k-1])

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

while 1:
    if k<=len(a):
        print(a[k-1])
        exit()
    b=[]
    for i in a:
        if i[-1]!='0':
            b.append(i+chr(ord(i[-1])-1))
        b.append(i+i[-1])
        if i[-1]!='9':
            b.append(i+chr(ord(i[-1])+1))
    k-=len(a)
    a=b.copy()

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

Solution (Python):
from collections import deque

K = int(input())
d = deque([1,2,3,4,5,6,7,8,9])
ansq = deque([1,2,3,4,5,6,7,8,9])

while len(ansq)<=K:
    num = d.popleft()

    if str(num)[-1] == "0":
        da = num*10
        db = num*10+1
        d.append(da)
        d.append(db)
        ansq.append(da)
        ansq.append(db)
        continue

    if str(num)[-1] == "9":
        da = num*10+8
        db = num*10+9
        d.append(da)
        d.append(db)
        ansq.append(da)
        ansq.append(db)
        continue

    da = num*10+int(str(num)[-1])-1
    db = num*10+int(str(num)[-1])
    dc = num*10+int(str(num)[-1])+1
    d.append(da)
    d.append(db)
    d.append(dc)
    ansq.append(da)
    ansq.append(db)
    ansq.append(dc)


print(ansq[K-1])

Solution (Python):
from collections import deque

K = int(input())
d = deque([1,2,3,4,5,6,7,8,9])
ansq = deque([1,2,3,4,5,6,7,8,9])

while len(ansq)<=K:
    num = d.popleft()

    if str(num)[-1] == "0":
        d.append(num*10)
        d.append(num*10+1)
        ansq.append(num*10)
        ansq.append(num*10+1)
        continue

    if str(num)[-1] == "9":
        d.append(num*10+8)
        d.append(num*10+9)
        ansq.append(num*10+8)
        ansq.append(num*10+9)
        continue

    d.append(num*10+int(str(num)[-1])-1)
    d.append(num*10+int(str(num)[-1]))
    d.append(num*10+int(str(num)[-1])+1)
    ansq.append(num*10+int(str(num)[-1])-1)
    ansq.append(num*10+int(str(num)[-1]))
    ansq.append(num*10+int(str(num)[-1])+1)
    
print(ansq[K-1])

Solution (Python):
import sys

def JudgeLunLun(num):
  val = num
  next = 0
  if num <= 12:
    val = num
  else:
    
    
    A = str(num)
    for i in range(len(A)-1):
      if abs(int(A[i])-int(A[i+1])) > 1:
        val = 0
        
        if int(A[i]) < int(A[i+1]):
          head = str(int(A[:i+1])+1)
          body = '0'
          tail = '0' * (len(A) - (i+2))
        else:
          if int(A[i]) - int(A[i+1]) > 2:
            head = A[:i+1]
            body = str(int(A[i]) -1)
            tail = '0' * (len(A) - (i+2))            
          else:
            head = A[:i+1]
            body = str(int(A[i+1]) +1)
            tail = '0' * (len(A) - (i+2))
            if body == '10':
              head = str(int(head)+1)
              body = '0'
        
        
        next = int(head+body+tail)
  
  return val, next

def main():
  K = int(input())
  cnt = 0
  i = 1
  while cnt < K:
    val, next = JudgeLunLun(i)
    
    if val > 0:
      i += 1
      cnt += 1
      if cnt == K:
        break
    else:
      i = next
    
  print(val)

if __name__ == '__main__':
  main()

Solution (Python):
from collections import deque

N = int(input())

q = deque()
for i in range(1, 10):
    q.append(i)

for i in range(N):
    a = q.popleft()
    d = a % 10
    if d > 0:
        q.append(10*a + d - 1)
    q.append(10*a + d)
    if d < 9:
        q.append(10*a + d + 1)

print(a)

Solution (Python):
from collections import deque

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

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

print(x)

Solution (Python):
from collections import deque


def dump_neighbor(val):
    if(val == 0):
        return [val, val+1]
    elif(val == 9):
        return [val-1, val]
    else:
        return [val-1, val, val+1]

def dfs(d, K):
    for i in range(K):
        val = d.popleft()
        neighbors = dump_neighbor(val%10)
        for neighbor in neighbors:
            d.append(val*10 + neighbor)
    print(val)
    

if __name__ == "__main__":
    K = int(input().rstrip())
    d = deque([val for val in range(1,10)])
    dfs(d, K)

Solution (Python):
count = 9
dp = [[[str(i)] for i in range(10)] for j in range(100)]
ans = [i for i in range(1,10)]

keta = 0
while count < 10**5:
    for i in range(10):
        curr = []
        for elem in dp[keta][i]:
            top = int(elem[0])
            if top > 0:
                curr += [str(top-1) + elem]
                if top > 1:
                    ans += [int(str(top-1) + elem)]
            if top < 9:
                curr += [str(top+1) + elem]
                ans += [int(str(top+1) + elem)]
            curr += [str(top) + elem]
            if top > 0 :
                ans += [int(str(top) + elem)]
        dp[keta+1][i] = curr
        count += len(curr)
    keta += 1

ans.sort()

K = int(input())
print(ans[K-1])

Solution (Python):
from collections import deque
import sys
l = int(input())
a = deque(range(1, 10))
count = 0
while True:
	x = a.popleft()
	count += 1
	if count==l:
		print(x)
		sys.exit(0)
	mod = x%10
	if mod !=0:
		a.append(x*10 + mod -1)
	a.append(x*10 + mod)
	if mod !=9:
		a.append(x*10 + mod +1)

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

from collections import deque
q=deque()

for i in range(1,10):
  q.append(i)
  
cnt=0
while cnt<K:
  num=q.popleft()
  cnt+=1
  one=num%10
  if one!=0:
    q.append(num*10+(one-1))
  q.append(num*10+one)
  if one!=9:
    q.append(num*10+(one+1))
  
print(num)

Solution (Python):
from collections import deque

def main():
  k = int(input())

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

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

  for _ in range(k-1):
    p = q.popleft()
    l = p % 10
    

    if l - 1 >= 0:
      new_data1 = p*10 + (l - 1)
      
      q.append(new_data1)
      
    
    new_data2 = p*10 + l
    
    q.append(new_data2)
    

    if l + 1 <= 9:
      new_data3 = p*10 + (l + 1)
      
      q.append(new_data3)
      

  print(q[0])

if __name__ == "__main__":
  main()

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    k = int(readline())
    if k < 10:
        print(k)
    else:
        ls_r = [str(i) for i in range(1, 10)]
        ls_z = ["0"]
        t = 9
        while True:
            t_ls_r = []
            t_ls_z = []
            for v in ls_r + ls_z:
                top = int(v[0])
                for i in range(max(0, top - 1), min(10, top + 2)):
                    if i == 0:
                        t_ls_z.append("".join([str(i)] + list(str(v))))
                    else:
                        t_ls_r.append("".join([str(i)] + list(str(v))))
            if len(t_ls_r) + t >= k:
                t_ls_r.sort()
                print(t_ls_r[k - t - 1])
                exit()
            else:
                ls_r = t_ls_r
                ls_z = t_ls_z
                t += len(t_ls_r)


if __name__ == '__main__':
    solve()

Solution (Python):
from collections import deque

def main():
  k = int(input())

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

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

  for _ in range(k-1):
    p = q.popleft()
    l = p % 10
    

    if l - 1 >= 0:
      new_data1 = int(str(p) + str(l - 1))
      
      q.append(new_data1)
      
    
    new_data2 = int(str(p) + str(l))
    
    q.append(new_data2)
    

    if l + 1 <= 9:
      new_data3 = int(str(p) + str(l + 1))
      
      q.append(new_data3)
      

  
  print(q[0])
  

if __name__ == "__main__":
  main()