Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> tup;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))

// int tansaku[110];
// map<string,string> d;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  bool visited[1010][1010];
  char field[1010][1010];
  queue<tup> kuwa;
  int H, W, N;
  // CLR(visited);
  REP(i, W) { REP(j, H) visited[i][j] = false; }
  cin >> H >> W >> N;
  REP(j, H) {
    REP(i, W) { cin >> field[i][j]; }
  }
  tup start;
  cin.ignore();
  REP(i, W) {
    REP(j, H) {
      if (field[i][j] == 'S') {
        start = {0, {i, j}};
      }
    }
  }
  kuwa.push(start);
  int ans = 0;
  int cost = 0;
  char cheeze = '1';
  while (!kuwa.empty()) {
    tup r = kuwa.front();
    kuwa.pop();
    int haba = r.first;
    PII p = r.second;
    visited[p.first][p.second] = true;
    if (field[p.first][p.second] == cheeze) {
      cost += haba;
      if (cheeze - '0' == N)
        break;
      cheeze++;
      queue<tup> daisuke;
      swap(kuwa, daisuke);
      kuwa.push(make_pair(0, p));
      // CLR(visited);
      REP(i, W) { REP(j, H) visited[i][j] = false; }

      continue;
    }
    for (int i = 0; i < 4; i++) {
      PII new_xy = {p.first + dx[i], p.second + dy[i]};
      if (0 <= new_xy.first && new_xy.first < W && 0 <= new_xy.second &&
          new_xy.second < H && !visited[new_xy.first][new_xy.second] &&
          field[new_xy.first][new_xy.second] != 'X') {
        kuwa.push(make_pair(haba + 1, new_xy));
        visited[new_xy.first][new_xy.second] = true;
      }
    }
  }
  cout << cost << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int>> tup;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double PI = acos(-1.0);
#define CLR(a) memset((a), 0, sizeof(a))

// int tansaku[110];
// map<string,string> d;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  bool visited[1010][1010];
  char field[1010][1010];
  queue<tup> kuwa;
  int H, W, N;
  // CLR(visited);
  REP(i, W) { REP(j, H) visited[i][j] = false; }
  cin >> H >> W >> N;
  REP(j, H) {
    REP(i, W) { cin >> field[i][j]; }
  }
  tup start;
  cin.ignore();
  REP(i, W) {
    REP(j, H) {
      if (field[i][j] == 'S') {
        start = {0, {i, j}};
      }
    }
  }
  kuwa.push(start);
  int ans = 0;
  int cost = 0;
  char cheeze = '1';
  while (!kuwa.empty()) {
    tup r = kuwa.front();
    kuwa.pop();
    int haba = r.first;
    PII p = r.second;
    visited[p.first][p.second] = true;
    if (field[p.first][p.second] == cheeze) {
      cost += haba;
      if (cheeze - '0' == N)
        break;
      cheeze++;
      queue<tup> daisuke;
      swap(kuwa, daisuke);
      kuwa.push(make_pair(0, p));
      // CLR(visited);
      REP(i, W) { REP(j, H) visited[i][j] = false; }

      continue;
    }
    for (int i = 0; i < 4; i++) {
      PII new_xy = {p.first + dx[i], p.second + dy[i]};
      if (0 <= new_xy.first && new_xy.first < W && 0 <= new_xy.second &&
          new_xy.second < H && !visited[new_xy.first][new_xy.second] &&
          field[new_xy.first][new_xy.second] != 'X') {
        kuwa.push(make_pair(haba + 1, new_xy));
      }
    }
  }
  cout << cost << endl;
  return 0;
}