alexjercan/bugnet huggingface dataset:
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PA;
int main() {

  int h, w, n, a[11][2];
  char c[1001][1001];
  cin >> h >> w >> n;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
      if (c[i][j] >= '1' && c[i][j] <= '9') {
        a[c[i][j] - '0'][0] = j;
        a[c[i][j] - '0'][1] = i;
      }
      if (c[i][j] == 'S') {
        a[0][0] = j;
        a[0][1] = i;
      }
    }
  }

  int sum = 0, suji[1001][1001], flag = 0;
  queue<PA> que;
  int dx[12] = {1, -1, 0, 0};
  int dy[12] = {0, 0, 1, -1};

  for (int i = 0; i < n; i++) {

    memset(suji, -1, sizeof(suji));
    que.push(PA(a[i][0], a[i][1]));
    suji[a[i][1]][a[i][0]] = 0;
    while (!que.empty()) {
      PA p = que.front();
      que.pop();

      for (int j = 0; j < 4; j++) {
        int x = p.first + dx[j];
        int y = p.second + dy[j];

        if (x < 0 || y < 0 || x >= w || y >= h || suji[y][x] != -1 ||
            c[y][x] == 'X')
          continue;
        suji[y][x] = suji[p.second][p.first] + 1;
        que.push(PA(x, y));
      }
    }

    sum += suji[a[i + 1][1]][a[i + 1][0]];
  }

  cout << sum << endl;

  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PA;
int main() {

  int h, w, n, a[11][2];
  char c[101][101];
  cin >> h >> w >> n;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
      if (c[i][j] >= '1' && c[i][j] <= '9') {
        a[c[i][j] - '0'][0] = j;
        a[c[i][j] - '0'][1] = i;
      }
      if (c[i][j] == 'S') {
        a[0][0] = j;
        a[0][1] = i;
      }
    }
  }

  int sum = 0, suji[101][101], flag = 0;
  queue<PA> que;
  int dx[12] = {1, -1, 0, 0};
  int dy[12] = {0, 0, 1, -1};

  for (int i = 0; i < n; i++) {

    memset(suji, -1, sizeof(suji));
    que.push(PA(a[i][0], a[i][1]));
    suji[a[i][1]][a[i][0]] = 0;
    while (!que.empty()) {
      PA p = que.front();
      que.pop();

      for (int j = 0; j < 4; j++) {
        int x = p.first + dx[j];
        int y = p.second + dy[j];

        if (x < 0 || y < 0 || x >= w || y >= h || suji[y][x] != -1 ||
            c[y][x] == 'X')
          continue;
        suji[y][x] = suji[p.second][p.first] + 1;
        que.push(PA(x, y));
      }
    }

    sum += suji[a[i + 1][1]][a[i + 1][0]];
  }

  cout << sum << endl;

  return (0);
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

// AOJ-0558
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

char map[1010][1010];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int vis[1010][1010];
int m, n, N;
struct node {
  int x, y, t;
};

node bfs(int x, int y, char k) {
  memset(vis, 0, sizeof(vis));
  queue<node> q;
  node s, e, p;
  s = {x, y, 0};
  q.push(s);
  vis[x][y] = 1;
  while (!q.empty()) {
    p = q.front();
    q.pop();
    if (map[p.x][p.y] == k)
      return p;
    for (int i = 0; i < 4; i++) {
      int nx = p.x + dx[i];
      int ny = p.y + dy[i];
      if (nx >= n || nx < 0 || ny >= m || ny < 0 || map[nx][ny] == 'X')
        continue;
      if (!vis[nx][ny]) {
        vis[nx][ny] = 1;
        e = {nx, ny, p.t + 1};
        q.push(e);
      }
    }
  }
}

int main() {
  while (~scanf("%d %d %d", &n, &m, &N)) {
    node s;
    for (int i = 0; i < n; i++) {
      scanf("%s", map[i]);
      for (int j = 0; j < m; j++) {
        if (map[i][j] == 'S') {
          s.x = i;
          s.y = j;
          s.t = 0;
        }
      }
    }
    int ans = 0;
    char c = '0';
    for (char i = 1; i <= N; i++) {
      node t = bfs(s.x, s.y, c + i);
      ans += t.t;
      s.x = t.x;
      s.y = t.y;
    }
    printf("%d\\n", ans);
  }
  return 0;
}


Error: MLE

Fixed submission:

// AOJ-0558
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

char map[1010][1010];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int vis[1010][1010];
int m, n, N;
struct node {
  int x, y, t;
};

node bfs(int x, int y, int k) {
  memset(vis, 0, sizeof(vis));
  queue<node> q;
  vis[x][y] = 1;
  node s;
  s.x = x;
  s.y = y;
  s.t = 0;
  q.push(s);
  while (!q.empty()) {
    node p = q.front();
    q.pop();
    vis[p.x][p.y] = 1;
    node e;
    if (map[p.x][p.y] == k + '0')
      return p;
    for (int i = 0; i < 4; i++) {
      e.x = p.x + dx[i];
      e.y = p.y + dy[i];
      if (map[e.x][e.y] == 'X' || vis[e.x][e.y] == 1)
        continue;
      e.t = p.t + 1;
      q.push(e);
    }
  }
  s.x = s.y = s.t = -1;
  return s;
}
void clear() {
  for (int i = 0; i < 1010; i++)
    for (int j = 0; j < 1010; j++)
      map[i][j] = 'X';
}
int main() {
  while (scanf("%d %d %d", &n, &m, &N) != EOF) {
    clear();
    node s;
    for (int i = 1; i <= n; i++) {
      getchar();
      for (int j = 1; j <= m; j++) {
        map[i][j] = getchar();
        if (map[i][j] == 'S') {
          map[i][j] = '.';
          s.x = i;
          s.y = j;
          s.t = 0;
        }
      }
    }
    int ans = 0;
    for (int i = 1; i <= N; i++) {
      s = bfs(s.x, s.y, i);
      map[s.x][s.y] = '.';
      ans += s.t;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;

typedef struct {
  int x, y;
} p;
char a[1005][1005];
p acm[11];
p s, g;
int b[1005][1005];
int H, W, N;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int bfs(p s, p g) {
  queue<p> q;
  p n;
  memset(b, -1, sizeof(b));
  q.push(s);
  b[s.x][s.y] = 0;
  while (!q.empty()) {
    p h;
    h = q.front();
    q.pop();
    if (h.x == g.x && h.y == g.y) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      n.x = h.x + dx[i];
      n.y = h.y + dy[i];
      if (n.x >= 0 && n.x < H && n.y >= 0 && n.y < W && a[n.x][n.y] != 'X' &&
          b[n.x][n.y] == -1) {
        q.push(n);
        b[n.x][n.y] = b[h.x][h.y] + 1;
      }
    }
  }
  return b[g.x][g.y];
}
int main() {
  int i, j, t, step = 0;
  scanf("%d%d%d", &H, &W, &N);
  for (i = 0; i < H; i++) {
    scanf("%s", a[i]);
  }
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++) {
      if (a[i][j] >= '1' && a[i][j] <= '9') {
        t = a[i][j] - '0';
        acm[t].x = i;
        acm[t].y = j;
      } else if (a[i][j] == 'S') {
        s.x = i;
        s.y = j;
        a[i][j] = '.';
      }
    }
  for (i = 1; i <= N; i++) {
    g.x = acm[i].x;
    g.y = acm[i].y;
    step += bfs(s, g);
    s.x = g.x;
    s.y = g.y;
    a[g.x][g.y] = '.';
  }

  printf("%d\\n", step);
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

int w, h, N;
char grid[1000][1000];
int dist[1000][1000];

bool viable(int i, int j);
void wipe();

int main() {
  int x, y;
  int tx, ty;
  int choice[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  queue<int> qx, qy;
  char target = '1';
  int res = 0;

  cin >> w >> h >> N;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }

  while (target - '0' <= N) {
    wipe();
    queue<int>().swap(qx);
    qx.push(x);
    queue<int>().swap(qy);
    qy.push(y);
    dist[x][y] = 0;
    while (!qx.empty()) {
      x = qx.front();
      qx.pop();
      y = qy.front();
      qy.pop();
      if (grid[x][y] == target) {
        res += dist[x][y];
        break;
      }
      for (int i = 0; i < 4; i++) {
        tx = x + choice[i][0];
        ty = y + choice[i][1];
        if (viable(tx, ty)) {
          dist[tx][ty] = dist[x][y] + 1;
          qx.push(tx);
          qy.push(ty);
        }
      }
    }
    target += 1;
  }
  cout << res << endl;
  return 0;
}

void wipe() {
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      dist[i][j] = -1;
    }
  }
}
bool viable(int i, int j) {
  return 0 <= i && i <= w && 0 <= j && j <= h && grid[i][j] != 'X' &&
         dist[i][j] == -1;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error


#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>

using namespace std;

typedef pair<int, int> P;

const int maxn = 1e3 + 5;
int inf = 500;

int h, w, n, sx, sy;
char s[maxn][maxn];
int book[maxn][maxn], goal[10];
int Next[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};

int judge(int row, int col) {
  if (row >= 0 && row < h && col >= 0 && col < w) {
    return 1;
  }
  return 0;
}

int bfs() {
  int res = 0;
  deque<P> q;
  for (int ci = 0; ci < n; ci++) {
    fill(book[0], book[0] + maxn * maxn, inf);
    q.push_back(P(sx, sy));
    book[sx][sy] = 0;
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop_front();
      if ((int)s[x][y] == 49 + ci) {
        goal[ci] = book[x][y];
        sx = x;
        sy = y;
        break;
      }
      for (int i = 0; i < 4; i++) {
        int nx = x + Next[i][0];
        int ny = y + Next[i][1];
        if (judge(nx, ny) && s[nx][ny] != 'X' && book[nx][ny] == inf) {
          q.push_back(P(nx, ny));
          book[nx][ny] = book[x][y] + 1;
        }
      }
    }
    q.clear();
    res += goal[ci];
  }
  return res;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  int goalnum = bfs();
  cout << goalnum << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int H, W, n;
int res = 0;
char chee[105][105];
int d[105][105];
void bfs(int x, int y, char ch) {
  memset(d, INF, sizeof(d));
  d[x][y] = 0;
  queue<P> que;
  que.push(P(x, y));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (chee[p.first][p.second] == ch) {
      res += d[p.first][p.second];
      break;
    }
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (d[nx][ny] == INF && chee[nx][ny] != 'X') {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
}
void rec(char ch) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (chee[i][j] == ch) {
        bfs(i, j, ch + 1);
        return;
      }
    }
  }
}
int main() {
  cin >> H >> W >> n;
  for (int i = 0; i < H; i++)
    scanf("%s", chee[i]);
  res = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (chee[i][j] == 'S') {

        bfs(i, j, '1');
        break;
      }
    }
  }
  for (int k = 1; k < n; k++) {
    rec(k + '0');
  }

  printf("%d\\n", res);

  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Time Limit Exceeded

/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 1000;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct QUEUE {
  int x, y, dep;
  QUEUE() {}
  QUEUE(int _x, int _y, int _dep) {
    x = _x;
    y = _y;
    dep = _dep;
  }
};
int r, c, n;
int goal[10][2];
bool vis[MAXN + 5][MAXN + 5];
char maz[MAXN + 5][MAXN + 5];
int BFS(int *beg, int cnt) {
  memset(vis, false, sizeof vis);
  queue<QUEUE> que;
  que.push(QUEUE(beg[0], beg[1], 0));
  while (!que.empty()) {
    QUEUE pre = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int X = pre.x + dir[i][0], Y = pre.y + dir[i][1], depth = pre.dep + 1;
      if (X < 1 || X > r || Y < 1 || Y > c || maz[X][Y] == 'X' || vis[X][Y])
        continue;
      vis[X][Y] = true;
      if (X == goal[cnt][0] && Y == goal[cnt][1])
        return depth;
      que.push(QUEUE(X, Y, depth));
    }
  }
  return -1;
}
int main() {
  scanf("%d%d%d", &r, &c, &n);
  for (int i = 1; i <= r; i++) {
    scanf("%s", maz[i] + 1);
    for (int j = 1; j <= c; j++) {
      if ('1' <= maz[i][j] && maz[i][j] <= '9')
        goal[maz[i][j] - '0'][0] = i, goal[maz[i][j] - '0'][1] = j,
                         maz[i][j] = '.';
      if (maz[i][j] == 'S')
        goal[0][0] = i, goal[0][1] = j, maz[i][j] = '.';
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    ans += BFS(goal[i - 1], i);
  printf("%d\\n", ans);
  return 0;
} //


Error: TLE

Fixed submission:

/*Lucky_Glass*/
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000;
const int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int vis[MAXN + 5][MAXN + 5][10];
char maz[MAXN + 5][MAXN + 5];
int r, c, n, ans;
void DFS(int x, int y, int k, int dep) {
  if (vis[x][y][k] <= dep)
    return;
  vis[x][y][k] = dep;
  if (maz[x][y] == '0' + k) {
    if (k == n) {
      ans = min(ans, dep);
      return;
    }
    k++;
  }
  for (int i = 0; i < 4; i++) {
    int X = x + dir[i][0], Y = y + dir[i][1];
    if (X < 1 || X > r || Y < 1 || Y > c || maz[X][Y] == 'X')
      continue;
    DFS(X, Y, k, dep + 1);
  }
}
int main() {
  scanf("%d%d%d", &r, &c, &n);
  int begx, begy;
  for (int i = 1; i <= r; i++) {
    scanf("%s", maz[i] + 1);
    for (int j = 1; j <= c; j++)
      if (maz[i][j] == 'S')
        begx = i, begy = j;
  }
  ans = (1 << 29);
  memset(vis, 0x3f, sizeof vis);
  DFS(begx, begy, 1, 0);
  printf("%d\\n", ans);
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

/*
 *
 */
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#define CLR(ARR, NUM) memset(ARR, NUM, sizeof(ARR))
#define GETNUM(NUM) scanf("%d", &NUM)
#define faster_io() ios_base::sync_with_stdio(false)
using namespace std;
const int MAXN = 1010;
char map[MAXN][MAXN];
bool vis[10][MAXN][MAXN];
struct node {
  int x;
  int y;
  int deep;
  int n;
};
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

int main() {
  CLR(vis, false);
  CLR(map, 'X');
  int h, w, n;
  cin >> h >> w >> n;
  getchar();
  int sx, sy;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      map[i][j] = getchar();
      if (map[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
    getchar();
  }
  node t;
  t.x = sx;
  t.y = sy;
  t.deep = 0;
  t.n = 0;
  queue<node> q;
  vis[0][sx][sy] = true;
  q.push(t);
  int ans = -1;
  while (!q.empty()) {
    node cur = q.front();
    if (cur.n == n) {
      ans = cur.deep;
      break;
    }
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = cur.x + dx[i];
      int ty = cur.y + dy[i];
      if (map[tx][ty] != 'X' && !vis[cur.n][tx][ty]) {
        node t = cur;
        t.x = tx;
        t.y = ty;
        t.deep++;
        if (map[tx][ty] == '1' + t.n) {
          t.n++;
        }
        vis[t.n][tx][ty] = true;
        q.push(t);
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

#define INF 0x7f7f7f7f
#define PI acos(-1.0)

#define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define lowbit(x) (x) & (-x)
#define Mid(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define iabs(x) (x) < 0 ? -(x) : (x)
#define CLR(arr, val) memset(arr, val, sizeof(arr))

using namespace std;

char map[1024][1024];
bool vis[1024][1024];
int cur, h, w;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};

struct node {
  int x, y, step;
} sta, now;

bool check(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w && !vis[x][y] && map[x][y] != 'X')
    return true;
  return false;
}

void bfs(node s) {
  memset(vis, 0, sizeof(vis));

  queue<node> que;
  que.push(s);
  while (!que.empty()) {
    now = que.front();
    que.pop();
    vis[now.x][now.y] = true;

    node next;
    next.step = now.step + 1;
    for (int i = 0; i < 4; i++) {
      next.x = now.x + dir[i][0];
      next.y = now.y + dir[i][1];

      if (check(next.x, next.y)) {
        if (map[next.x][next.y] == cur + '0') {
          map[next.x][next.y] = '.';
          sta = next;
          return;
        }
        que.push(next);
      }
    }
  }
}

int main() {
  int n;
  while (cin >> h >> w >> n) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
        if (map[i][j] == 'S')
          sta.x = i, sta.y = j, map[i][j] = '.';
      }
    }
    sta.step = 0;
    for (cur = 1; cur <= n; cur++) {
      bfs(sta);
    }
    cout << sta.step << endl;
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INFI 100000000

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1, 0, -1, 0};
int ver[] = {0, -1, 0, 1};
int ans;

void clear();
void show();
P bfs(int cheese, P n);

int main(void) {
  cin >> H >> W >> N;
  P s, e;
  for (int i = 0; i < H; i++)
    cin >> Maps[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Maps[i][j] == 'S')
        s.first = i, s.second = j;

  for (int i = 1; i <= N; i++) {
    clear();
    s = bfs(i, s);
  }

  cout << ans << endl;
  return 0;
}

P bfs(int cheese, P n) {
  queue<P> que;
  que.push(n);
  dist[n.first][n.second] = 0;

  P top;
  while (que.size()) {
    top = que.front();
    if (Maps[top.first][top.second] - '0' == cheese) {
      ans += dist[top.first][top.second];
      break;
    }

    que.pop();
    for (int i = 0; i < 4; i++) {
      int nv = top.first + ver[i], nh = top.second + hor[i];
      if (nv >= 0 && nv < H && nh >= 0 && nh < W && dist[nv][nh] == INFI &&
          Maps[nv][nh] != 'X') {
        que.push(P(nv, nh));
        dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second] + 1);
      }
    }
  }
  return top;
}

void clear() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dist[i][j] = INFI;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#define min(x, y) ((x) < (y) ? (x) : (y))
#define INFI 65535

using namespace std;
typedef pair<int, int> P;

int Strength = 1;
char Maps[1010][1010];
int dist[1010][1010];
int H, W, N;
int hor[] = {1, 0, -1, 0};
int ver[] = {0, -1, 0, 1};
int ans;

void clear();
void show();
P bfs(int cheese, P n);

int main(void) {
  cin >> H >> W >> N;
  P s, e;
  for (int i = 0; i < H; i++)
    cin >> Maps[i];
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (Maps[i][j] == 'S')
        s.first = i, s.second = j;

  for (int i = 1; i <= N; i++) {
    clear();
    s = bfs(i, s);
  }

  cout << ans << endl;
  return 0;
}

P bfs(int cheese, P n) {
  queue<P> que;
  que.push(n);
  dist[n.first][n.second] = 0;

  P top;
  while (que.size()) {
    top = que.front();
    if (Maps[top.first][top.second] - '0' == cheese) {
      ans += dist[top.first][top.second];
      break;
    }

    que.pop();
    for (int i = 0; i < 4; i++) {
      int nv = top.first + ver[i], nh = top.second + hor[i];
      if (nv >= 0 && nv < H && nh >= 0 && nh < W && dist[nv][nh] == INFI &&
          Maps[nv][nh] != 'X') {
        que.push(P(nv, nh));
        dist[nv][nh] = min(dist[nv][nh], dist[top.first][top.second] + 1);
      }
    }
  }
  return top;
}

void clear() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dist[i][j] = INFI;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <climits>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

typedef pair<int, int> pii;

int main() {
  int H, W, N;
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  cin >> H >> W >> N;
  char map[H][W];
  int d[H][W], hp = 1, ans = 0;
  fill(d[0], d[H], INT_MAX);
  queue<pii> q;
  REP(i, H) {
    REP(j, W) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        q.push({i, j});
        d[i][j] = 0;
      }
    }
  }
  while (1) {
    pii p = q.front();
    q.pop();
    if (hp == map[p.first][p.second] - '0') {
      ans += d[p.first][p.second];
      hp++;
      fill(d[0], d[H], INT_MAX);
      d[p.first][p.second] = 0;
      while (!q.empty())
        q.pop();
    }
    if (hp == N + 1) {
      cout << ans << endl;
      return 0;
    }
    REP(i, 4) {
      pii next_p = {p.first + dx[i], p.second + dy[i]};
      if (next_p.first < 0 || next_p.first >= H || next_p.second < 0 ||
          next_p.second >= W || map[next_p.first][next_p.second] == 'X')
        continue;
      if (d[next_p.first][next_p.second] > d[p.first][p.second] + 1) {
        d[next_p.first][next_p.second] = d[p.first][p.second] + 1;
        q.push(next_p);
      }
    }
  }
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

typedef pair<int, int> pii;

int main() {
  int H, W, N;
  const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
  cin >> H >> W >> N;
  char map[H][W];
  int d[N][H][W], hp = 1, ans = 0;
  fill(d[0][0], d[N][0], INT_MAX);
  queue<pii> q;
  REP(i, H) {
    REP(j, W) {
      cin >> map[i][j];
      if (map[i][j] == 'S') {
        q.push({i, j});
        d[hp - 1][i][j] = 0;
      }
    }
  }
  while (1) {
    pii p = q.front();
    q.pop();
    if (hp == map[p.first][p.second] - '0') {
      ans += d[hp - 1][p.first][p.second];
      hp++;
      d[hp - 1][p.first][p.second] = 0;
      while (!q.empty())
        q.pop();
    }
    if (hp == N + 1) {
      cout << ans << endl;
      return 0;
    }
    REP(i, 4) {
      pii next_p = {p.first + dx[i], p.second + dy[i]};
      if (next_p.first < 0 || next_p.first >= H || next_p.second < 0 ||
          next_p.second >= W || map[next_p.first][next_p.second] == 'X')
        continue;
      if (d[hp - 1][next_p.first][next_p.second] >
          d[hp - 1][p.first][p.second] + 1) {
        d[hp - 1][next_p.first][next_p.second] =
            d[hp - 1][p.first][p.second] + 1;
        q.push(next_p);
      }
    }
  }
}