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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pb(n) push_back(n)
#define mp(n, m) make_pair(n, m)
#define fi first
#define se second
#define all(r) (r).begin(), (r).end()

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define rep(i, n) REP(i, 0, n)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<vi> vii;

const int INF = 100000000;
const double EPS = 1e-10;
int mod = 1000000007;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, n;
vector<string> s;

bool outOfRange(int y, int x) {
  if (x < 0 || x >= w || y < 0 || y >= h)
    return true;
  return false;
}

int calc(int y, int x, int c, int num) {
  // cout<<y<<x<<" "<<c<<" "<<num<<endl;
  if (num == n)
    return c;
  queue<pair<int, pii>> q;
  vector<vi> v(h, vi(w, -1));
  v[y][x] = 0;
  pair<int, pii> p(c, mp(y, x));
  q.push(p);
  while (!q.empty()) {
    // cout<<"  "<<q.size()<<endl;
    p = q.front();
    q.pop();
    int tx = p.se.se, ty = p.se.fi, tc = p.fi;
    for (int i = 0; i < 4; i++) {
      int nx = tx + dx[i], ny = ty + dy[i];
      if (outOfRange(ny, nx) || s[ny][nx] == 'X' || v[ny][nx] != -1)
        continue;
      // cout<<ny<<" "<<nx<<endl;
      if (s[ny][nx] == '1' + num) {
        // q = queue<pair<int, pii> >;
        // cout<<"AAA"<<endl;
        return calc(ny, nx, tc + 1, num + 1);
      }
      // cout<<" "<<nx<<" "<<ny<<endl;
      v[ny][nx] = tc + 1;
      q.push(mp(tc + 1, mp(ny, nx)));
    }
  }
  return -1;
}

int main() {
  cin >> h >> w >> n;
  // cout<<h<<" "<<w<<" "<<n<<endl;
  s = vector<string>(h);
  rep(i, h) cin >> s[i];
  // rep(i, h) cout<<s[i]<<endl;
  // cout<<s[2][2]<<endl;
  int sx, sy;
  rep(i, h) rep(j, w) {
    if (s[i][j] == 'S') {
      sy = i;
      sx = j;
    }
  }
  // cout<<sy<<" "<<sx<<endl;
  cout << calc(sy, sx, 0, 0) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int h, w, n;
vector<string> s;

int bfs(pii st, pii g) {
  vector<vector<int>> v(h, vi(w, 0));
  queue<pair<pii, int>> q;
  pair<pii, int> p(st, 0);
  q.push(p);
  while (!q.empty()) {
    p = q.front();
    q.pop();
    v[p.fi.fi][p.fi.se] = 1;
    if (g == p.fi) {
      return p.se;
    }
    for (int i = 0; i < 4; i++) {
      int y = p.fi.fi + dy[i], x = p.fi.se + dx[i];
      if (y < 0 || y >= h || x < 0 || x >= w || s[y][x] == 'X' || v[y][x])
        continue;
      q.push(pair<pii, int>(pii(y, x), p.se + 1));
    }
  }
  return -1;
}

int main() {
  cin >> h >> w >> n;
  s = vector<string>(h);
  rep(i, h) cin >> s[i];
  vector<pii> p(n + 1);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        p[0] = pii(i, j);
      } else if (s[i][j] >= '1' && s[i][j] <= '9') {
        p[s[i][j] - '0'] = pii(i, j);
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += bfs(p[i], p[i + 1]);
  }
  cout << ans << endl;
}