Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define ll_INF 9223372036854775807
const static ll MOD = 1e9 + 7;
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
using namespace std;

string solve(ll K) {
  vector<vector<vector<string>>> vec(10, vector<vector<string>>(10));
  int cnt = 0;
  vec[0][0].push_back(to_string(0));
  for (int i = 1; i < 10; i++) {
    vec[0][i].push_back(to_string(i));
    cnt++;
    if (cnt == K) {
      return to_string(i);
    }
  }

  for (int i = 1; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (j == 0) {
        for (int k = 0; k < vec[i - 1][j].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j][k];
          vec[i][j].push_back(temp);
        }
        for (int k = 0; k < vec[i - 1][j + 1].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j + 1][k];
          vec[i][j].push_back(temp);
        }
      } else if (j > 0 and j < 9) {
        for (int k = 0; k < vec[i - 1][j - 1].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j - 1][k];
          cnt++;
          if (cnt == K) {
            return temp;
          }
          vec[i][j].push_back(temp);
        }
        for (int k = 0; k < vec[i - 1][j].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j][k];
          cnt++;
          if (cnt == K) {
            return temp;
          }
          vec[i][j].push_back(temp);
        }
        for (int k = 0; k < vec[i - 1][j + 1].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j + 1][k];
          cnt++;
          if (cnt == K) {
            return temp;
          }
          vec[i][j].push_back(temp);
        }
      } else {
        for (int k = 0; k < vec[i - 1][j - 1].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j - 1][k];
          cnt++;
          if (cnt == K) {
            return temp;
          }
          vec[i][j].push_back(temp);
        }
        for (int k = 0; k < vec[i - 1][j].size(); k++) {
          string temp = to_string(j) + vec[i - 1][j][k];
          cnt++;
          if (cnt == K) {
            return temp;
          }
          vec[i][j].push_back(temp);
        }
      }
    }
  }
}
int main() {
  ll K;
  cin >> K;
  string ans = solve(K);

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define ll_INF 9223372036854775807
const static ll MOD = 1e9 + 7;
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
using namespace std;

#define Max 3234566668

bool isGood(ll x) {
  string s = to_string(x);
  for (int i = 1; i < s.size(); i++) {
    if (abs(s[i - 1] - s[i]) > 1) {
      return 0;
    }
  }

  return 1;
}
int main() {
  int K;
  cin >> K;
  ll ans = 0;
  int cnt = 1;
  for (ll x = 1; cnt <= K; x++) {
    if (isGood(x)) {
      ans = x;
      cnt++;
    }
  }
  cout << ans << endl;
}