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 in C++ (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,M=0;
  char p[20]={0};
  char ans[20]={0};
  cin >> N;
  char *adr;
  for(int a=0;a<=9;a++){ 
    p[11]=a;
    if(abs(p[12]-p[11])>1)
      continue;
    for(int b=0;b<=9;b++){ 
      p[10]=b;
      if(abs(p[11]-p[10])>1 && a)
        continue;
      for(int c=0;c<=9;c++){ 
        p[9]=c;
        if(abs(p[10]-p[9])>1 && a+b)
          continue;
        for(int d=0;d<=9;d++){ 
          p[8]=d;
          if(abs(p[9]-p[8])>1 && a+b+c)
            continue;
          for(int e=0;e<=9;e++){
            p[7]=e;
            if(abs(p[8]-p[7])>1 && a+b+c+d)
              continue;
            for(int f=0;f<=9;f++){ 
              p[6]=f;
              if(abs(p[7]-p[6])>1 && a+b+c+d+e)
                continue;
              for(int g=0;g<=9;g++){ 
                p[5]=g;
                if(abs(p[6]-p[5])>1 && a+b+c+d+e+f)
                  continue;
                for(int h=0;h<=9;h++){ 
                  p[4]=h;
                  if(abs(p[5]-p[4])>1 && a+b+c+d+e+f+g)
                    continue;
                  for(int i=0;i<=9;i++){ 
                    p[3]=i;
                    if(abs(p[4]-p[3])>1 && a+b+c+d+e+f+g+h)
                      continue;
                    for(int j=0;j<=9;j++){ 
                      p[2]=j;
                      if(abs(p[3]-p[2])>1 && a+b+c+d+e+f+g+h+i)
                        continue;
                      for(int k=0;k<=9;k++){ 
                        p[1]=k;
                        if(abs(p[2]-p[1])>1 && a+b+c+d+e+f+g+h+i+j)
                          continue;
                        for(int l=0;l<=9;l++){ 
                          p[0]=l;
                          if(abs(p[0]-p[1])>1 && a+b+c+d+e+f+g+h+i+j+k)
                            continue;
                          if(M++==N){
                            int ss[12]={a,b,c,d,e,f,g,h,i,j,k,l};
                            for(int uu=0;uu<12;uu++){
                              if(ss[uu]){
                                for(int ansp=uu;uu<12;uu++){
                                  ans[uu-ansp]=p[11-uu]+='0';
                                }
                            goto endend;
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  endend:
  puts(ans);
}