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

// https://atcoder.jp/contests/abc161/tasks/abc161_d
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int K;
  cin >> K;
  queue<int> q;
  FOR(i, 1, 10) q.push(i);
  int cnt = 0;
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    cnt++;
    if (cnt >= K) {
      cout << t << endl;
      return 0;
    }
    vector<int> v;
    FOR(i, t % 10 - 1, t % 10 + 2) {
      if (i >= 0 && i <= 9) {
        v.push_back(i);
      }
    }
    for (auto &x : v) {
      q.push(t * 10 + x);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc161/tasks/abc161_d
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

// int dp[10][10][10][10][10];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int K;
  cin >> K;
  int cnt = -1;
  REP(i10, 10) {
    REP(i9, 10) {
      REP(i8, 10) {
        REP(i7, 10) {
          REP(i6, 10) {
            REP(i5, 10) {
              REP(i4, 10) {
                REP(i3, 10) {
                  REP(i2, 10) {
                    REP(i1, 10) {
                      if ((i10 == 0 || abs(i10 - i9) <= 1) &&
                          (i10 + i9 == 0 || abs(i9 - i8) <= 1) &&
                          (i10 + i9 + i8 == 0 || abs(i8 - i7) <= 1) &&
                          (i10 + i9 + i8 + i7 == 0 || abs(i7 - i6) <= 1) &&
                          (i10 + i9 + i8 + i7 + i6 == 0 || abs(i6 - i5) <= 1) &&
                          (i10 + i9 + i8 + i7 + i6 + i5 == 0 ||
                           abs(i5 - i4) <= 1) &&
                          (i10 + i9 + i8 + i7 + i6 + i5 + i4 == 0 ||
                           abs(i4 - i3) <= 1) &&
                          (i10 + i9 + i8 + i7 + i6 + i5 + i4 == 0 ||
                           abs(i4 - i3) <= 1) &&
                          (i10 + i9 + i8 + i7 + i6 + i5 + i4 + i3 == 0 ||
                           abs(i3 - i2) <= 1) &&
                          (i10 + i9 + i8 + i7 + i6 + i5 + i4 + i3 + i2 == 0 ||
                           abs(i2 - i1) <= 1)) {
                        cnt++;
                      }
                      if (cnt >= K) {
                        int t = i10 * 1000000000 + i9 * 100000000 +
                                i8 * 10000000 + i7 * 1000000 + i6 * 100000 +
                                i5 * 10000 + i4 * 1000 + i3 * 100 + i2 * 10 +
                                i1;
                        cout << t << endl;
                        return 0;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return 0;
}