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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll inf = 1LL << 62;
const int INF = 1 << 30;
struct node {
  int x;
  int y;
  int c;
  bool operator<(node a) const { return c < a.c; }
  bool operator>(node a) const { return c > a.c; }
};

int dis[1010][1010];
int nx[10];
int ny[10];
string maze[1010];
int hmax = 1;

int main(void) {
  int H, W, n;
  cin >> H >> W >> n;
  rep(i, H) cin >> maze[i];
  node init;
  rep(i, H) rep(j, W) {
    if (maze[i][j] == 'S') {
      nx[0] = j;
      ny[0] = i;
    } else if (isdigit(maze[i][j])) {
      nx[maze[i][j] - '0'] = j;
      ny[maze[i][j] - '0'] = i;
    }
  }
  int ans = 0;
  rep(j, n) {
    rep(k, H) rep(l, W) dis[k][l] = INF;
    init.x = nx[j];
    init.y = ny[j];
    init.c = 0;
    priority_queue<node, vector<node>, greater<node>> q;
    q.push(init);
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    while (!q.empty()) {
      node cur;
      cur = q.top();
      q.pop();
      // テァツ渉セテ・ツ慊ィテ、ツスツ催ァツスツョテ」ツ?ョテ・ツ按、テ・ツョツ堙・ツ?ヲテァツ青?
      if (dis[cur.y][cur.x] <= cur.c)
        continue;
      else
        dis[cur.y][cur.x] = cur.c;
      if (hmax == (maze[cur.y][cur.x] - '0')) {
        ans += cur.c;
        hmax++;
        break;
      }
      // テ」ツ??ヲツャツ。テ」ツ?ョテァツ環カテヲツ?凝」ツ?クテ」ツ?ョテゥツ?キテァツァツサ
      rep(i, 4) {
        node next;
        next.x = cur.x + dx[i];
        next.y = cur.y + dy[i];
        next.c = cur.c + 1;
        if (0 <= next.x && next.x < W && 0 <= next.y && next.y < H &&
            maze[next.y][next.x] != 'X')
          q.push(next);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll inf = 1LL << 62;

struct node {
  int x;
  int y;
  int c;
};

bool check[1010][1010];
string maze[1010];
int hmax = 1;

int main(void) {
  int H, W, n;
  cin >> H >> W >> n;
  rep(i, H) cin >> maze[i];
  node init;
  rep(i, H) rep(j, W) {
    check[i][j] = false;
    if (maze[i][j] == 'S') {
      init.x = j;
      init.y = i;
      init.c = 0;
    }
  }
  int ans = 0;
  rep(j, n) {
    //	cout << j << endl;
    queue<node> q;
    q.push(init);
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    while (!q.empty()) {
      node cur;
      cur = q.front();
      q.pop();
      // テァツ渉セテ・ツ慊ィテ、ツスツ催ァツスツョテ」ツ?ョテ・ツ按、テ・ツョツ堙・ツ?ヲテァツ青?
      if (check[cur.y][cur.x])
        continue;
      if (hmax == (maze[cur.y][cur.x] - '0')) {
        ans += cur.c;
        hmax++;
        break;
      }
      // テ」ツ??ヲツャツ。テ」ツ?ョテァツ環カテヲツ?凝」ツ?クテ」ツ?ョテゥツ?キテァツァツサ
      rep(i, 4) {
        node next;
        next.x = cur.x + dx[i];
        next.y = cur.y + dy[i];
        next.c = cur.c + 1;
        if (0 <= next.x && next.x < W && 0 <= next.y && next.y < H &&
            maze[next.y][next.x] != 'X')
          q.push(next);
      }
    }
    rep(k, H) rep(l, W) {
      check[k][l] = false;
      if (maze[k][l] == ('0' + (hmax - 1))) {
        init.x = l;
        init.y = k;
        init.c = 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}