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

al=[]
cand=[1,2,3,4,5,6,7,8,9]

temp=[]
for _ in range(9):
    for item in cand:
        if int(str(item)[-1])!=0:
            temp.append(int(str(item)[-1])-1+item*10)
        temp.append(int(str(item)[-1])+item*10)
        if int(str(item)[-1])!=9:
            temp.append(int(str(item)[-1])+1+item*10)
    al+=cand
    cand=temp[:]
    al=list(set(al))
    al.sort()
al+=temp
al=list(set(al))
al.sort()
print(al[K-1])

Solution (Python):
import sys

K = int(input())

NLUNLUN = [[-1 for _ in range(11)] for _ in range(10)]
def num_lunlun(n, digit):
    if digit == 1:
        NLUNLUN[n][digit] = 1
        return NLUNLUN[n][digit]

    if digit == 2:
        if 0 < n < 9:
            NLUNLUN[n][digit] = 3
        else:
            NLUNLUN[n][digit] = 2
        return NLUNLUN[n][digit]

    n2 = 0
    if n - 1 >= 0:
        n2 += NLUNLUN[n-1][digit-1]
    n2 += NLUNLUN[n][digit-1]
    if n + 1 <= 9:
        n2 += NLUNLUN[n+1][digit-1]
    NLUNLUN[n][digit] = n2
    return NLUNLUN[n][digit]


if K < 10:
    print(K)
    sys.exit(0)

k = 0
for digit in range(1, 11):
    for n in range(10):
        tmp = num_lunlun(n, digit)
        if n > 0:
            if k + tmp >= K:
                ans = str(n)
                n2 = n
                for digit2 in range(digit-1, 1, -1):
                    if n2 - 1 >= 0:
                        tmp = NLUNLUN[n2 - 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 - 1)
                            n2 -= 1
                            continue
                        k += tmp

                    tmp = NLUNLUN[n2][digit2]
                    if k + tmp >= K:
                        ans += str(n2)
                        continue
                    k += tmp

                    if n2 + 1 <= 9:
                        tmp = NLUNLUN[n2 + 1][digit2]
                        if k + tmp >= K:
                            ans += str(n2 + 1)
                            n2 += 1
                            continue
                        k += tmp

                if ans[-1] == '0':
                    ans = int(ans + str(n2 + K - k - 1))
                else:
                    ans = int(ans + str(n2 + K - k - 2))
                print(ans)

                sys.exit(0)
            k += tmp

Solution (Python):
k = int(input())
from collections import deque
queue = deque(range(1,10))
count = 0

while count < k:
  count += 1
  tmp = queue.popleft()
  tail = tmp % 10
  if tail!=0:
    queue.append(tmp*10+tail-1)
  queue.append(tmp*10+tail)
  if tail!=9:
    queue.append(tmp*10+tail+1)
  
print(tmp)

Solution (Python):
from collections import deque

K = int(input())
dq = deque([i for i in range(1,10)])

for k in range(K):
  num = dq.popleft()

  if num%10 != 0:
    dq.append(10*num + (num % 10) -1)
  dq.append(10*num + (num%10))
  if num%10 != 9:
    dq.append(10*num + (num % 10) +1)
    
print(num)

Solution (Python):
def findnxt(x):
	i=len(x)-1
	if x[i]==9:
		while True:
			if i==0 :
				break
			if(x[i]<=x[i-1] and x[i]!=9):
				break
			i-=1
		if x[i]==9:
			x.insert(0,1)
			for j in range(1,len(x)):
				x[j]=0
		else:
			x[i]+=1
			i+=1
			while i<len(x):
				if x[i-1]:
					x[i]=x[i-1]-1
				else:
					x[i]=0
				i+=1
	else:
		while i:
			if x[i]<=x[i-1]:
				break
			i-=1
		x[i]+=1
		i+=1
		while i<len(x):
			if x[i-1]:
				x[i]=x[i-1]-1
			else:
				x[i]=0
			i+=1
	return x
	
x=[1]
k=int(input())
for l in range(2,k+1):
	x=findnxt(x)
for l in x:
	print(l,end='')

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

Solution (Python):
import collections

K = int(input())
Q = collections.deque([i for i in range(1, 10)])
for _ in range(K):
  x = Q.popleft()
  r = x%10
  if r != 0: Q.append(x*10 + r - 1)
  Q.append(x*10 + r)
  if r != 9: Q.append(x*10 + r + 1)
print(x)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
def lcm(a, b): return a * b // gcd(a, b)
from itertools import count, permutations, chain
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok

def get_inv(n, modp):
    return pow(n, modp-2, modp)

def factorials_list(n, modp):    
    fs = [1]
    for i in range(1, n+1):
        fs.append(fs[-1] * i % modp)
    return fs

def invs_list(n, fs, modp):     
    invs = [get_inv(fs[-1], modp)]
    for i in range(n, 1-1, -1):
        invs.append(invs[-1] * i % modp)
    invs.reverse()
    return invs

def comb(n, k, modp):
    num = 1
    for i in range(n, n-k, -1):
        num = num * i % modp
    den = 1
    for i in range(2, k+1):
        den = den * i % modp
    return num * get_inv(den, modp) % modp

def comb_from_list(n, k, modp, fs, invs):   
    return fs[n] * invs[n-k] * invs[k] % modp



class UnionFindEx:
    def __init__(self, size):
        
        self.roots = [-1] * size
    def getRootID(self, i):
        r = self.roots[i]
        if r < 0:   
            return i
        else:
            r = self.getRootID(r)
            self.roots[i] = r
            return r
    def getGroupSize(self, i):
        return -self.roots[self.getRootID(i)]
    def connect(self, i, j):
        r1, r2 = self.getRootID(i), self.getRootID(j)
        if r1 == r2:
            return False
        if self.getGroupSize(r1) < self.getGroupSize(r2):
            r1, r2 = r2, r1
        self.roots[r1] += self.roots[r2]    
        self.roots[r2] = r1
        return True

Yes = 'Yes'
No = 'No'


def main():
    def gen():
        counter = 0
        q = deque(range(1, 10))
        while counter < 100001:
            n = q.popleft()
            yield n
            counter += 1
            t = n % 10
            if t > 0:
                q.append(n*10 + (t - 1))
            q.append(n*10 + t)
            if t < 9:
                q.append(n*10 + (t + 1))
    g = gen()
    for _ in range(ii() - 1):
        next(g)
    print(next(g))

main()

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

a=[]
for i in range(1,10):
    a.append(i)
while True:
    if k<=len(a):
        print(a[k-1])
        break
    
    k-=len(a)
    old=[]
    old,a=a,old
    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):
from collections import deque
que=deque([1,2,3,4,5,6,7,8,9])
k=int(input())
for i in range(k-1):
  q=que.popleft()
  
  m=q%10
  
  if m==0:
    que.append(q*10)
    que.append(q*10+1)
  elif m==9:
    que.append(q*10+8)
    que.append(q*10+9)
  else:
    que.append(q*10+m-1)
    que.append(q*10+m)
    que.append(q*10+m+1)
print(que[0])

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

Q = deque(range(1, 10)) 


for i in range(k):
  x = Q.popleft()
  
  last_digit = x % 10 
  z = 10*x + last_digit 

  if last_digit != 0:
    Q.append(z-1)
    
  Q.append(z)
  
  if last_digit != 9:
    Q.append(z+1)
    
print(x)

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

Q = deque(range(1, 10)) 


for i in range(k):
  x = Q.popleft()
  
  last_digit = x % 10 
  if last_digit == 0:
        Q.append(10*x + 0)
        Q.append(10*x + 1)

  elif last_digit == 9:
        Q.append(10*x + 8)
        Q.append(10*x + 9)

  else: 
        Q.append(10*x + last_digit - 1)
        Q.append(10*x + last_digit)
        Q.append(10*x + last_digit + 1)
print(x)

Solution (Python):
import collections

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

print(x)

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

if K < 11:
    print(K)
    exit()
K -=10
ll_num = [0,1]
while(K>0):
    ll_num[0]+=1
    digit_num = len(ll_num)
    for digit in range(digit_num):
        if ll_num[digit] ==10:
            if digit == digit_num-1:
                ll_num.append(0)
                digit_num +=1
            ll_num[digit+1] +=1
            if ll_num[digit+1] ==10:
                ll_num[digit] = 0
            for i in reversed(range(0,digit+1)):
                ll_num[i] = ll_num[i+1]-1
                if ll_num[i] < 0:
                    ll_num[i] = 0

    for digit in range(digit_num-1):
        if ll_num[digit]>ll_num[digit+1]+1:
            ll_num[digit+1] +=1
            for i in reversed(range(0,digit+1)):
                ll_num[i] = ll_num[i+1]-1
                if ll_num[i] < 0:
                    ll_num[i] = 0
    K -=1
result_str = ""
for num in range(len(ll_num)):
     result_str = str(ll_num[num]) + result_str
print(int(result_str))

Solution (Python):

import sys
input = sys.stdin.readline

dp = [[0] * 10 for _ in range(11)]
for i in range(10):
    dp[0][i] = 1
for i in range(1, 11):
    for j in range(0, 10):
        dp[i][j] += dp[i-1][j]
        if j-1 >= 0:
            dp[i][j] += dp[i-1][j-1]
        if j+1 < 10:
            dp[i][j] += dp[i-1][j+1]
    
k = int(input())
ans = ""
ret = 0
lv = 0
for i in range(11):
    if k > sum(dp[i][1:]):
        k -= sum(dp[i][1:])
    else:
        lv = i
        break

lft = 1; rgt = 10 
for l in range(lv, -1, -1):
    for i in range(lft, rgt):
        item = dp[l][i]
        if k > item:
            k -= item
        else:
            lft = max(i - 1, 0)
            rgt = i + 2
            ans += str(i)
            break
print(ans)

Solution (Python):
K=int(input())
a=list(range(1,10))
if K<=9:
  print(a[K-1])
  quit()
count=9
i=0
while count<K:
  b=a[i]%10
  if b!=0:
    a.append(a[i]*10+b-1)
    count+=1
  a.append(a[i]*10+b)
  count+=1
  if b!=9:
    a.append(a[i]*10+b+1)
    count+=1
  i+=1
print(a[K-1])

Solution (Python):
K=int(input())
a=list(range(1,10))
if K<=9:
  print(a[K-1])
  quit()
count=9
i=0
while count<K+1:
  b=a[i]%10
  if b!=0:
    a.append(a[i]*10+b-1)
    count+=1
  a.append(a[i]*10+b)
  count+=1
  if b!=9:
    a.append(a[i]*10+b+1)
    count+=1
  i+=1
print(a[K-1])

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

def dfs(n):
	if n>3234566667:
		return
	temp2=[]
	for i in range(3):
		m=int(str(n)[-1])
		if 0<=m+i-1<=9:
			nn=n*10+m+i-1
			temp2.append(nn)
			L.append(nn)
	if temp2:
		temp1.append(temp2)
	for n in temp1[-1]:
		dfs(n)

for i in range(9):
	dfs(i+1)

LS=sorted(L)
print(LS[k-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):
    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 f(k):
    if 1 <= k <= 9:
        print(k)
    else:
        nxt = [[0,1], [0,1,2], [1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9]]
        cum_npi = [0, 9, 35, 110, 327, 956, 2782, 8089, 23527, 68468, 199368]
        for length, begin in enumerate(cum_npi):
            if k < begin:
                found = cum_npi[length - 1]
                break
        begin = 10 ** (length - 1)
        length, begin, found
        k -= found
        x = [[]] * (length + 1)
        x[1] = list(range(1, 10))
        x

        terminate = False
        i = 1
        while True:
            
            if len(x[i]): 
                digit = x[i][0]
                if i < length: 
                    i += 1
                    x[i] = nxt[digit].copy()
                    
                    
                    if i == length: 
                        while len(x[i]): 
                            k -= 1
                            
                            if k == 0:
                                x.pop(0)
                                
                                ans = "".join([str(j.pop(0)) for j in x])
                                print(ans)
                                terminate = True
                                break
                            else:
                                x[i].pop(0)
                            
                        else:
                            
                            while len(x[i]) == 0:
                                i -= 1
                                x[i].pop(0)
                        if terminate:
                            break

f(int(input()))

Solution (Python):


from collections import deque

k = int(input())

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

for _ in range(k):
    ans = d.popleft()
    keta1 = ans%10
    for i in [-1, 0, 1]:
        if keta1+i>=0 and keta1+i<=9:
            d.append(ans*10+keta1+i)

print(ans)

Solution (Python):
import sys
import queue
I=input()
K=int(I)
Q=queue.Queue()
for _ in range(1,10):
    Q.put(_)
x=0
for _ 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())
queue = deque([i for i in range(1,10)])

while K != 0:
  if K <= len(queue):
    print(queue[K-1])
    exit(0)

  K -= 1
  num = queue.popleft()

  o = int(str(num)[-1])
  for n in [o-1, o, o+1]:
    if n == -1 or n == 10: continue
    queue.append( int(str(num)+str(n)) )

Solution (Python):
K = int(input())
queue = []
for i in range(1, 10):
  queue.append(i)

while K != 0:
  if K <= len(queue):
    print(queue[K-1])
    exit(0)

  K -= 1
  num = queue.pop(0)

  if K == 0:
    print(num)
    exit(0)

  o = int(str(num)[-1])
  for n in [o-1, o, o+1]:
    if n == -1 or n == 10: continue
    queue.append( int(str(num)+str(n)) )

Solution (Python):
import queue
k = int(input())
q = queue.Queue()
for i in range(1, 10):
    q.put(i)
for i in range(k):
    a = q.get()
    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)
print(a)

Solution (Python):
from collections import deque

K = int(input())
D = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

if K <= 9:
  print(D[K])
else:
  start = 1
  K -= 9
  while True:
    d = D[start]
    start += 1
    ichi = d % 10
    if ichi == 0:
      if K == 1:
        print(d * 10 + 0)
        break
      elif K == 2:
        print(d * 10 + 1)
        break
      else:
        D.append(d * 10 + 0)
        D.append(d * 10 + 1)
        K -= 2
    elif ichi == 9:
      if K == 1:
        print(d * 10 + 8)
        break
      elif K == 2:
        print(d * 10 + 9)
        break
      else:
        D.append(d * 10 + 8)
        D.append(d * 10 + 9)
        K -= 2
    else:
      if K == 1:
        print(d * 10 + ichi - 1)
        break
      elif K == 2:
        print(d * 10 + ichi)
        break
      elif K == 3:
        print(d * 10 + ichi + 1)
        break
      else:
        D.append(d * 10 + ichi - 1)
        D.append(d * 10 + ichi)
        D.append(d * 10 + ichi + 1)
        K -= 3

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

def bfs():
    q = deque([1,2,3,4,5,6,7,8,9])
    cnt = 0
    while q:
        num = q.popleft()
        cnt += 1
        if cnt == k:
            return num
        else:
            if str(num)[-1] == "0":
                q.append(num*10)
                q.append(num*10+1)
            elif str(num)[-1] == "9":
                q.append(num*10+8)
                q.append(num*10+9)
            else:
                q.append(num*10+int(str(num)[-1])-1)
                q.append(num*10+int(str(num)[-1]))
                q.append(num*10+int(str(num)[-1])+1)


print(bfs())

Solution (Python):
a = [1,2,3,4,5,6,7,8,9,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000]
import heapq
heapq.heapify(a)
cnt = 18
c = 1
while True:
  if cnt > 10**5:
    break
  for i in a:
    if len(str(i)) == c:
      m = i//10**(c-1)
      if i//10**(c-1) == 1:
        heapq.heappush(a,i+10**c*(i//10**(c-1)))
        heapq.heappush(a,i+10**c*(i//10**(c-1)+1))
        cnt += 2
        for j in range(10-c):
          heapq.heappush(a,i+10**c*(10**(j+1)))
          cnt += 1
      elif i//10**(c-1) == 9:
        heapq.heappush(a,i+10**c*(i//10**(c-1)-1))
        heapq.heappush(a,i+10**c*(i//10**(c-1)))
        cnt += 2
      else:
        heapq.heappush(a,i+10**c*(i//10**(c-1)-1))
        heapq.heappush(a,i+10**c*(i//10**(c-1)))
        heapq.heappush(a,i+10**c*(i//10**(c-1)+1))
        cnt += 3
  c += 1

k = int(input())


for i in range(k-1):
  heapq.heappop(a)
print(heapq.heappop(a))

Solution (Python):
def pat(K,count,keta,num):
    
    if count == -1:
        return -1
    elif keta == 1:
        if count == K-1:
            print(num)
            return -1
        else:
            return (count+1)
    else:
        if (int(num[-1])) -1 >= 0:
            count = pat(K,count,keta-1,num+str(int(num[-1])-1))
        count = pat(K,count,keta-1,num+num[-1])
        if (int(num[-1])+1) <= 9:
            count = pat(K,count,keta-1,num+str(int(num[-1])+1))
            
    return count


K = int(input())
count = 0
ans = list()
keta = 0

while True:
    keta += 1
    for i in range(1,10):
        count = pat(K,count,keta,str(i))
        
        if count  == -1:
            break
    if count == -1:
        break

Solution (Python):
from collections import deque

k = int(input())

if 1 <= k and k <= 9:
    print(k)
    exit()

d = deque([i for i in range(1, 10)])
k -= 9
while k > 0:
    num = d.popleft()
    if num % 10 != 0:
        new = 10*num + (num%10) - 1
        d.append(new)
        k -= 1
        if k == 0: break
    new = 10*num + (num%10)
    d.append(new)
    k -= 1
    if k == 0: break
    if num % 10 != 9:
        new = 10*num + (num%10) + 1
        d.append(new)
        k -= 1
        if k == 0: break

print(new)

Solution (Python):
from collections import deque

K = int(input())

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

x = 1
count = 0
while K != count:
    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)
    count += 1
print(x)

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

q = deque([str(i) for i in range(1, 10)])
cnt = 0
while True:
    if cnt == K-1:
        print(q.popleft())
        sys.exit()
    cnt += 1
    x = q.popleft()
    b = int(x[-1])
    if b > 0:
        q.append(x+str(b-1))
    q.append(x+str(b))
    if b < 9:
        q.append(x+str(b+1))

Solution (Python):
import collections

k = int(input())
q = collections.deque()
Lunlun = []
for i in range(1, 10):
    q.append(i)
while True:
    if len(Lunlun) >= 10**5:
        break
    now = q.popleft()
    Lunlun.append(now)
    for i in range(-1, 2):
        if (now % 10) + i < 0 or 9 < (now % 10) + i:
            continue
        q.append(now*10 + (now % 10) + i)

print(Lunlun[k-1])

Solution (Python):
from queue import Queue
k = int(input())
q = Queue()
Lunlun = []
for i in range(1, 10):
    q.put(i)
while True:
    if len(Lunlun) >= 10**5:
        break
    now = q.get()
    Lunlun.append(now)
    for i in range(-1, 2):
        if (now % 10) + i < 0 or 9 < (now % 10) + i:
            continue
        q.put(now*10 + (now % 10) + i)

print(Lunlun[k-1])

Solution (Python):
import heapq
import sys

def solve(k):
    if k < 10:
        return k
    
    heap = [1,2,3,4,5,6,7,8,9]
    heapq.heapify(heap)

    for i in range(k-1):
        lunlun = heapq.heappop(heap)
        lastDigit = lunlun  % 10
        
        heapq.heappush(heap, (lunlun*10) + lastDigit)

        if lastDigit == 0:
            newlunlun = (lunlun * 10) + 1
            heapq.heappush(heap, newlunlun)    
        elif lastDigit == 9:
            newlunlun = (lunlun * 10) + 8
            heapq.heappush(heap, newlunlun)  
        else:
            newlunlun1 = (lunlun * 10) + lastDigit + 1
            newlunlun2 = (lunlun * 10) + lastDigit - 1
            heapq.heappush(heap, newlunlun1)
            heapq.heappush(heap, newlunlun2)

    return heapq.heappop(heap)

k = input()

print(solve(int(k)))

Solution (Python):
from collections import Counter, deque







import sys
sys.setrecursionlimit(10 ** 5 + 10)


def resolve():
    K=int(input())
    d=deque(['1','2','3','4','5','6','7','8','9'])
    for i in range(K):
        dd=d.popleft()
        if i==K-1:
            print(dd)
        else:
            ddd=dd[-1]
            if int(ddd)==0:
                d.append(dd+str(int(ddd)))
                d.append(dd+str(int(ddd)+1))
            if 1<=int(dd[-1])<=8:
                d.append(dd+str(int(ddd)-1))
                d.append(dd+str(int(ddd)))
                d.append(dd+str(int(ddd)+1))
            if int(dd[-1])==9:
                d.append(dd+str(int(ddd)-1))
                d.append(dd+str(int(ddd)))


resolve()

Solution (Python):
from collections import deque


k = int(input())

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

for _ in range(k):
    num = queue.popleft()
    x = num % 10

    if x != 0:
        queue.append(num*10 + x-1)

    queue.append(num*10 + x)

    if x != 9:
        queue.append(num*10 + x+1)

print(num)

Solution (Python):
from collections import deque


k = int(input())

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

for _ in range(k):
    num = queue.popleft()
    a = num // 10
    b = num % 10

    if b != 0:
        queue.append(num*10 + b-1)

    queue.append(num*10 + b)
    
    if b != 9:
        queue.append(num*10 + b+1)

print(num)

Solution (Python):
k=int(input())
lun=[1,2,3,4,5,6,7,8,9]
for i in range(10**5+1):
    if len(lun)>=k:
        break
    di=lun[i]%10
    for j in range(max(0,di-1),min(9,di+1)+1):
        lun.append(lun[i]*10+j)
print(lun[k-1])

Solution (Python):


k = int(input())
l = list(range(1,10))
x = 0

while True:
    a = l.pop(0)
    x += 1
    
    if x >= k:
        print(a)
        exit()
    if k > 10000 and x > k/2:
        continue
    
    if a%10 == 0:
        b = int(str(a) + "0")
        l.append(b)
        
        b = int(str(a) + "1")
        l.append(b)
        
            
    elif a%10 == 9:
        b = int(str(a) + "8")
        l.append(b)
        
        b = int(str(a) + "9")
        l.append(b)
        
        
    else:
        b = int(str(a) + str((a%10)-1))
        l.append(b)
        
        b = int(str(a) + str(a%10))
        l.append(b)
        
        b = int(str(a) + str((a%10)+1))
        l.append(b)
        
        
        if x >= k:
            print(a)
            exit()

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):
  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):
k = int(input())
a = [[1] * 10]
ps = 0
s = 0
for i in range(10000):
  for j in range(1, 10):
    ps = s
    s += a[i][j]
    if s >= k:
      break
  else:
    a.append([a[i][j-1] + a[i][j] + a[i][j+1] if j != 0 and j != 9 else a[i][0] + a[i][1] if j == 0 else a[i][8] + a[i][9] for j in range(10)])
    continue
  break
n = i
ans = str(j)
for i in range(n - 1, -1, -1):
  k -= ps
  m = j
  s = 0
  for j in range(max(0, m - 1), min(10, m + 2)):
    ps = s
    s += a[i][j]
    if s >= k:
      break
  ans += str(j)
print(int(ans))

Solution (Python):
K = int(input())
cnt = 0
ans = 0
for i in range(0, 10):
    for j in range(0, 10):
        if (j <= i-2 or j >= i+2) and i != 0:
            continue
        for k in range(0, 10):
            if (k <= j-2 or k >= j+2) and i+j != 0:
                continue
            for l in range(0, 10):
                if (l <= k-2 or l >= k+2) and i+j+k != 0:
                    continue
                for m in range(0, 10):
                    if (m <= l-2 or m >= l+2) and i+j+k+l != 0:
                        continue
                    for n in range(0, 10):
                        if (n <= m-2 or n >= m+2) and i+j+k+l+m != 0:
                            continue
                        for o in range(0, 10):
                            if (o <= n-2 or o >= n+2) and i+j+k+l+m+n != 0:
                                continue
                            for p in range(0, 10):
                                if (p <= o-2 or p >= o+2) and i+j+k+l+m+n+o != 0:
                                    continue
                                for q in range(0, 10):
                                    if (q <= p-2 or q >= p+2) and i+j+k+l+m+n+o+p != 0:
                                        continue
                                    for r in range(0, 10):
                                        if ((r <= q-2 or r >= q+2) and i+j+k+l+m+n+o+p+q != 0) or i+j+k+l+m+n+o+p+q+r == 0:
                                            continue
                                        cnt += 1
                                        if cnt == K:
                                            ans = r + 10 * q + 100 * p + 1000 * o + 10000 * n + 100000 * m + 1000000 * l + 10000000 * k + 100000000 * j + 1000000000 * i
print(ans)

Solution (Python):
import heapq
k = int(input())
run_list = [i for i in range(1,10)]
heapq.heapify(run_list)
result = 0
for i in range(k):
  result = heapq.heappop(run_list)
  if result % 10 ==0:
    new_1 = result * 10
    new_2 = result * 10 + 1
    heapq.heappush(run_list, new_1)
    heapq.heappush(run_list, new_2)
  elif result % 10 ==9:
    new_1 = result * 10 + 8
    new_2 = result * 10 + 9
    heapq.heappush(run_list, new_1)
    heapq.heappush(run_list, new_2)
  else:
    new_1 = result * 10 + (result % 10)
    new_2 = result * 10 + (result % 10) + 1
    new_3 = result * 10 + (result % 10) - 1
    heapq.heappush(run_list, new_1)
    heapq.heappush(run_list, new_2)
    heapq.heappush(run_list, new_3)
print(result)

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

from collections import deque

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

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

Solution (Python):
from collections import deque

def main():
    K = int(input())
    que = deque([i for i in range(1, 10)])

    while K:
        if K == 1:
            ans = que.popleft()
            break
        K -= 1
        v = que.popleft()
        for i in range(-1, 2, 1):
            a = v % 10 + i
            if a < 0 or a > 9:
                continue
            que.append(v * 10 + a)
            
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

def main():
    K = int(input())
    que = deque([i for i in range(1, 10)])

    while K:
        if K == 1:
            ans = que.popleft()
            break
        K -= 1
        v = que.popleft()
        if v % 10 == 0:
            que.append(v * 10)
            que.append(v * 10 + 1)
        elif v % 10 == 9:
            que.append(v * 10 + 8)
            que.append(v * 10 + 9)
        else:
            que.append(v * 10 + (v % 10 - 1))
            que.append(v * 10 + (v % 10))
            que.append(v * 10 + (v % 10 + 1))
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    K = int(input())
    que = deque(['1', '2', '3', '4', '5', '6', '7', '8', '9'])

    while K:
        if K == 1:
            ans = que.popleft()
            break
        K -= 1
        v = que.popleft()
        if v[-1] == '0':
            que.append(v + '0')
            que.append(v + '1')
        elif v[-1] == '9':
            que.append(v + '8')
            que.append(v + '9')
        else:
            que.append(v + str(int(v[-1]) - 1))
            que.append(v + str(int(v[-1])))
            que.append(v + str(int(v[-1]) + 1))

    print(ans)

if __name__ == '__main__':
    main()

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

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

while K:
    if K == 1:
        ans = que.popleft()
        break
    K -= 1
    v = que.popleft()
    if v[-1] == '0':
        que.append(v + '0')
        que.append(v + '1')
    elif v[-1] == '9':
        que.append(v + '8')
        que.append(v + '9')
    else:
        que.append(v + str(int(v[-1]) - 1))
        que.append(v + str(int(v[-1])))
        que.append(v + str(int(v[-1]) + 1))

print(ans)

Solution (Python):

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

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

def readInts():
  return list(map(int,input().split()))

def main():
  """
  10,11,12 １の末尾に0,1,2を付けたもの！
  21,22,23 2の末尾に0,1,2を付けたもの！
  queueを使う！
  1,2,3,4,5,6,7,8,9,
  2,3,4,5,6,7,8,9,10,11,12
  3,4,5,6,7,8,9,10,11,12,21,22,23
  という風に、pushしていく
  """
  k = int(input())
  from collections import deque
  d = deque()
  
  for i in range(1,10):
    d.append(i)
  
  
  
  
  cnt = len(d)
  while True:
    if k <= cnt:
      print(d[k-1])
      exit()
    
    x = d.popleft()
    cnt -=1
    k -=1
    for i in range(-1,2,1):
      a = x % 10 + i
      if a < 0 or a > 9 :continue
      nx = x * 10 + a 
      d.append(nx)
      cnt += 1
      
    
if __name__ == '__main__':
  main()

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

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

print(num)

Solution (Python):


import collections


def solve():

    K = int(input())
    q = collections.deque()

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

    lunlun = 0

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

    print(lunlun)


if __name__ == "__main__":
    solve()