Problem p02720 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define F first
#define S second
#define FOR(i, r) for (int i = 0; i < r; i++)
#define REP(i, l, r) for (int i = l; i < r; i++)
#define RER(i, l, r) for (int i = l; i >= r; i--)
#define print(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << ' ';
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mem(arr, k) memset(arr, k, sizeof(arr));
#define pii acos(-1.0)
#define PI 3.1415926535897932385
#define Sin(a) sin((pi * a) / 180)
#define test()                                                                 \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
using namespace std;
set<int> res;
int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  FASTIO;
  queue<int> q;
  REP(i, 1, 10) { q.push(i); }
  while (!q.empty()) {
    int nd = q.front();
    q.pop();
    res.insert(nd);
    if (res.size() > 1e5)
      break;
    int last = nd % 10;
    if (last - 1 >= 0)
      q.push(nd * 10 + (last - 1));
    q.push(nd * 10 + last);
    if (last + 1 <= 9)
      q.push(nd * 10 + (last + 1));
  }
  int k;
  cin >> k;
  k--;
  for (auto i : res) {
    if (k == 0) {
      cout << i << endl;
      return 0;
    }
    k--;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define F first
#define S second
#define FOR(i, r) for (int i = 0; i < r; i++)
#define REP(i, l, r) for (int i = l; i < r; i++)
#define RER(i, l, r) for (int i = l; i >= r; i--)
#define print(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << ' ';
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mem(arr, k) memset(arr, k, sizeof(arr));
#define pii acos(-1.0)
#define PI 3.1415926535897932385
#define Sin(a) sin((pi * a) / 180)
#define test()                                                                 \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
using namespace std;
set<int> res;
int32_t main() {

  FASTIO;
  queue<int> q;
  REP(i, 1, 10) { q.push(i); }
  while (!q.empty()) {
    int nd = q.front();
    q.pop();
    res.insert(nd);
    if (res.size() > 1e5)
      break;
    int last = nd % 10;
    if (last - 1 >= 0)
      q.push(nd * 10 + (last - 1));
    q.push(nd * 10 + last);
    if (last + 1 <= 9)
      q.push(nd * 10 + (last + 1));
  }
  int k;
  cin >> k;
  k--;
  for (auto i : res) {
    if (k == 0) {
      cout << i << endl;
      return 0;
    }
    k--;
  }
  return 0;
}