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

#include <bits/stdc++.h>

using namespace std;

struct Node {
  int x, y, t;
  Node(int x_, int y_, int t_) {
    x = x_;
    y = y_;
    t = t_;
  }
};

int H, W, N;
int area[1001][1001];
bool memo[1001][1001];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int bfs(int sx, int sy, int HP) {
  fill_n(*memo, 1001 * 1001, 0);
  queue<Node> Q;
  Q.push(Node(sx, sy, 0));
  int ret;

  while (!Q.empty()) {
    int X, Y, T;
    Node node = Q.front();
    X = node.x;
    Y = node.y;
    T = node.t;
    Q.pop();
    if (area[X][Y] == HP) {
      ret = T;
      break;
    }
    if (memo[X][Y] == 1)
      continue;
    memo[X][Y] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = X + dx[i];
      int ny = Y + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (area[nx][ny] == -1)
        continue;
      Q.push(Node(nx, ny, T + 1));
    }
  }
  return ret;
}

int main() {
  fill_n(*area, 1001 * 1001, 0);
  int sx[10];
  int sy[10];
  char c;
  int ans = 0;
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      if (c == 'X') {
        area[j][i] = -1;
      } else if (c == '.') {
        area[j][i] = 0;
      } else if (c == 'S') {
        area[j][i] = 0;
        sx[0] = j;
        sy[0] = i;
      } else {
        area[j][i] = c - '0';
        sx[c - '0'] = j;
        sy[c - '0'] = i;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    ans += bfs(sx[i], sy[i], i + 1);
  }
  cout << ans << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef struct xyt {
  int xx;
  int yy;
  int tt;
} xyt;

int H, W, N, minute = 0;
char area[1001][1001] = {};
bool areab[1001][1001] = {};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void dfs(int x, int y) {
  int X, Y, time;
  int HP = 1;
  xyt now = {x, y, 0};
  queue<xyt> Q;
  Q.push(now);

  while (HP <= N) {
    X = Q.front().xx;
    Y = Q.front().yy;
    time = Q.front().tt;
    Q.pop();

    for (int i = 0; i <= 4; i++) {
      if (X + dx[i] <= 0 || Y + dy[i] <= 0 || X + dx[i] > W || Y + dy[i] > H)
        continue;

      if (areab[X + dx[i]][Y + dy[i]] == 0) {
        if (area[X + dx[i]][Y + dy[i]] == HP) {
          minute += time;
          while (!Q.empty()) {
            Q.pop();
          }
          now.xx = X + dx[i];
          now.yy = Y + dy[i];
          now.tt = 0;
          Q.push(now);
          HP++;
          break;
        } else {
          now.xx = X + dx[i];
          now.yy = Y + dy[i];
          now.tt = time + 1;
          Q.push(now);
        }
      }
    }
  }
  return;
}

int main() {
  cin >> H >> W >> N;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> area[j][i];
      if (area[j][i] == 'X') {
        areab[j][i] = 1;
      }
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (area[j][i] == 'S') {
        dfs(j, i);
      }
    }
  }
  cout << minute << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long int

using namespace std;

const int INF = 100000000;

typedef pair<int, int> P;
char masu[1003][1003];
int W, H, K, ans = 0;
int sx[10], sy[10];
int gx[10], gy[10];

int d[1003][1003];

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

queue<P> que;

void sho() {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[j][i] = INF;
    }
  }
  while (que.size()) {
    que.pop();
  }
}

int bfs() {
  for (int i = 0; i < K; i++) {
    sho();
    que.push(P(sx[i], sy[i]));
    d[sx[i]][sy[i]] = 0;
    while (que.size()) {
      P p = que.front();
      que.pop();
      if (p.first == gx[i + 1] && p.second == gy[i + 1])
        break;
      for (int j = 0; j < 4; j++) {
        int nx = p.first + dx[j], ny = p.second + dy[j];
        if (0 <= nx & nx < W && 0 <= ny && ny < H && masu[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(P(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
    }
    ans += d[gx[i + 1]][gy[i + 1]];
  }
  return ans;
}

signed main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> masu[j][i];
      int a = masu[j][i] - '0';
      if (isdigit(masu[j][i])) {
        gx[a] = j, gy[a] = i, sx[a] = j, sy[a] = i;
      } else if (masu[j][i] == 'S') {
        sx[0] = j, sy[0] = i;
      }
    }
  }
  int answer = bfs();
  cout << answer << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ctype.h>
#define int long long int
#define rep(a, b) for (int a = 0; a < b; a++)
#define INF 1000006
using namespace std;

int tx[] = {1, 0, -1, 0};
int ty[] = {0, 1, 0, -1};
int w, h, che, sx, sy;
char bases;
int base[1003][1003], base2[1003][1003], memo[1003][1003];

int chee(int x, int y, int n) {
  int ans = INF;
  if (memo[x][y] > 0 && (memo[x][y] != INF || base[x][y] < 0))
    return memo[x][y];
  else if (base[x][y] == n)
    return 0;
  else if (base[x][y] >= 0) {
    base[x][y] = -1;
    for (int i = 0; i < 4; i++) {
      if (x + tx[i] >= 0 && x + tx[i] < w && y + ty[i] >= 0 && y + ty[i] < h) {
        ans = min(ans, chee(x + tx[i], y + ty[i], n) + 1);
      }
    }
    base[x][y] = 0;
  }
  memo[x][y] = ans;
  return ans;
}
void sho(int b[1003][1003], int a[1003][1003]) {
  rep(i, 1003) {
    rep(j, 1003) { b[i][j] = a[i][j]; }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int zx[20], zy[20];
  cin >> h >> w >> che;
  rep(f, h) {
    rep(s, w) {
      cin >> bases;
      switch (bases) {
      case 'S':
        sx = s;
        sy = f;
      case '.':
        base[s][f] = 0;
        break;
      case 'X':
        base[s][f] = -1;
        break;
      default: {
        base[s][f] = bases - '0';
        zx[base[s][f]] = s;
        zy[base[s][f]] = f;
      }
      }
    }
  }
  sho(base2, base);
  int ans = 0;
  ans += chee(sx, sy, 1);
  for (int i = 2; i <= che; i++) {
    sho(base, base2);
    fill((int *)memo, (int *)(memo + 1003), 0);
    ans += chee(zx[i - 1], zy[i - 1], i);
  }
  cout << ans << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

typedef pair<int, int> P;
typedef pair<P, P> RAT;

const int INF = 100000000;

char map[1005][1005];
int flag[1005][1005];
int H, W, N;
int cheese;
int sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  queue<RAT> que;
  int tx, ty;
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    for (int j = 0; j < W; j++)
      if (map[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  }

  que.push(RAT(P(sx, sy), P(1, 0)));
  cheese = 1;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      flag[i][j] = INF;
  for (;;) {
    RAT rat = que.front();
    que.pop();
    if (rat.second.first == N + 1) {
      printf("%d\\n", rat.second.second);
      break;
    }
    if (cheese > rat.second.first)
      continue;
    if (cheese < rat.second.first) {
      cheese = rat.second.first;
      for (int i = 0; i < H; i++)
        for (int j = 0; j < W; j++)
          flag[i][j] = INF;
    }
    for (int i = 0; i < 4; i++) {
      tx = rat.first.first + dx[i];
      ty = rat.first.second + dy[i];
      if (tx >= 0 && tx < H && ty >= 0 && ty < W && map[tx][ty] != 'X' &&
          flag[tx][ty] != 0) {
        if (rat.second.first == map[tx][ty] - 48) {
          que.push(
              RAT(P(tx, ty), P(rat.second.first + 1, rat.second.second + 1)));
          flag[tx][ty] = 0;
        } else {
          que.push(RAT(P(tx, ty), P(rat.second.first, rat.second.second + 1)));
          flag[tx][ty] = 0;
        }
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <queue>
#include <utility>

using namespace std;

typedef pair<int, int> P;
typedef pair<P, P> RAT;

char map[1005][1005];
int H, W, N;
int sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  queue<RAT> que;
  int tx, ty;
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    for (int j = 0; j < W; j++)
      if (map[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  }

  que.push(RAT(P(sx, sy), P(1, 0)));
  for (;;) {
    RAT rat = que.front();
    que.pop();
    if (rat.second.first == N + 1) {
      printf("%d\\n", rat.second.second);
      break;
    }
    for (int i = 0; i < 4; i++) {
      tx = rat.first.first + dx[i];
      ty = rat.first.second + dy[i];
      if (tx >= 0 && tx < H && ty >= 0 && ty < W && map[tx][ty] != 'X') {
        if (rat.second.first == map[tx][ty] - 48)
          que.push(
              RAT(P(tx, ty), P(rat.second.first + 1, rat.second.second + 1)));
        else
          que.push(RAT(P(tx, ty), P(rat.second.first, rat.second.second + 1)));
      }
    }
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define F first
#define S second

const int M = 1024;
const int INF = 114514810;
int h, w, n;
char f[M][M];
int d[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<P> cheese(12);

void init() {
  rep(y, M) rep(x, M) f[y][x] = '$';
  cheese.clear();
}

bool ng(int x, int y) { return (x < 0 or w <= x or y < 0 or h <= y); }

int bfs(P s, P g) {
  int gx = g.F, gy = g.S;
  queue<P> Q;
  rep(y, M) rep(x, M) d[y][x] = INF;
  Q.push(s);
  d[s.S][s.F] = 0;

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    if (p.F == gx and p.S == gy)
      break;

    rep(i, 4) {
      int nx = p.F + dx[i], ny = p.S + dy[i];
      if (ng(nx, ny) or f[ny][nx] == 'X' or d[ny][nx] < INF)
        continue;
      d[ny][nx] = d[p.S][p.F] + 1;
      Q.push(P(nx, ny));
    }
  }

  return d[gy][gx];
}

int main() {
  while (cin >> h >> w >> n) {
    init();
    rep(y, h) rep(x, w) {
      cin >> f[y][x];
      char c = f[y][x];
      if (c != '.' and c != 'S' and c != 'X')
        cheese[c - '0'] = make_pair(x, y);
      if (c == 'S')
        cheese[0] = make_pair(x, y);
    }

    int ans = 0;
    rep(i, n) ans += bfs(cheese[i], cheese[i + 1]);
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define F first
#define S second

const int M = 1024;
const int INF = 114514;
int h, w, n;
char f[M][M];
int d[M][M];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
vector<P> cheese(12);

void init() {
  rep(y, M) rep(x, M) f[y][x] = '$';
  cheese.clear();
}

bool ng(int x, int y) { return (x < 0 or w <= x or y < 0 or h <= y); }

int bfs(P s, P g) {
  int gx = g.F, gy = g.S;
  queue<P> Q;
  rep(y, M) rep(x, M) d[y][x] = INF;
  Q.push(s);
  d[s.S][s.F] = 0;

  while (Q.size()) {
    P p = Q.front();
    Q.pop();
    if (p.F == gx and p.S == gy)
      break;

    rep(i, 4) {
      int nx = p.F + dx[i], ny = p.S + dy[i];
      if (ng(nx, ny) or f[ny][nx] == 'X' or d[ny][nx] < INF)
        continue;
      d[ny][nx] = d[p.S][p.F] + 1;
      Q.push(P(nx, ny));
    }
  }

  return d[gy][gx];
}

int main() {
  while (cin >> h >> w >> n) {
    init();
    rep(y, h) rep(x, w) {
      cin >> f[y][x];
      char c = f[y][x];
      if (c != '.' and c != 'S' and c != 'X')
        cheese[c - '0'] = make_pair(x, y);
      if (c == 'S')
        cheese[0] = make_pair(x, y);
    }

    int ans = 0;
    rep(i, n) ans += bfs(cheese[i], cheese[i + 1]);
    cout << ans << endl;
  }

  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int h, w, n;
vector<vector<bool>> mp;
vector<pair<int, int>> f;

int calc(pair<int, int> start, pair<int, int> end) {
  vector<vector<int>> hi(w, vector<int>(h, 1145141919));
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      q;
  q.push(make_pair(0, start));
  while (q.size()) {
    int c = q.top().first;
    pair<int, int> p = q.top().second;
    q.pop();
    if (p == end)
      return c;
    int x = p.first;
    int y = p.second;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if ((!!dx ^ !!dy) && 0 <= x + dx && x + dx < w && 0 <= y + dy &&
            y + dy < h && !mp[x + dx][y + dy] && hi[x + dx][y + dy] > c + 1) {
          hi[x + dx][y + dy] = c + 1;
          q.push(make_pair(c + 1, make_pair(x + dx, y + dy)));
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> h >> w >> n;
  mp.assign(w, vector<bool>(h, false));
  f.assign(n + 1, make_pair(0, 0));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      char a;
      cin >> a;
      if (a == 'X')
        mp[x][y] = true;
      if (a == 'S')
        f[0] = make_pair(x, y);
      if ('1' <= a && a <= '9')
        f[a - '0'] = make_pair(x, y);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += calc(f[i - 1], f[i]);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

int h, w, n;
vector<vector<bool>> mp;
vector<pair<int, int>> f;

int bfs(pair<int, int> start, pair<int, int> end) {
  queue<pair<int, pair<int, int>>> q;
  q.push(make_pair(0, start));
  while (q.size()) {
    int c = q.front().first;
    pair<int, int> p = q.front().second;
    q.pop();
    if (p == end)
      return c;
    int x = p.first;
    int y = p.second;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if ((!!dx ^ !!dy) && 0 <= x + dx && x + dx < w && 0 <= y + dy &&
            y + dy < h && !mp[x + dx][y + dy]) {
          q.push(make_pair(c + 1, make_pair(x + dx, y + dy)));
        }
      }
    }
  }
  return -1;
}

int main() {
  cin >> h >> w >> n;
  mp.assign(w, vector<bool>(h, false));
  f.assign(n + 1, make_pair(0, 0));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      char a;
      cin >> a;
      if (a == 'X')
        mp[x][y] = true;
      if (a == 'S')
        f[0] = make_pair(x, y);
      if ('1' <= a && a <= '9')
        f[a - '0'] = make_pair(x, y);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += bfs(f[i - 1], f[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;

string s[1000];
int h, w, n;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int bfs(int x, int y, int now) {
  int d[h][w] = {};
  queue<pair<int, int>> Q;
  Q.push(make_pair(8, x * 1000 + y));
  while (!Q.empty()) {
    pair<int, int> a = Q.front();
    Q.pop();
    int tx = a.second / 1000, ty = a.second % 1000;
    if (s[tx][ty] - '0' == now) {
      return a.first / 10;
    }
    for (int r = 0; r < 4; r++) {
      if (abs(a.first % 10 - r) == 2)
        continue;
      int sx = tx + dx[r];
      int sy = ty + dy[r];
      if (sx < 0 || sy < 0 || sx >= h || sy >= w || s[sx][sy] == 'X' ||
          d[sx][sy] != 0)
        continue;
      Q.push(make_pair((a.first / 10 + 1) * 10 + r, sx * 1000 + sy));
      d[sx][sy] = 1;
    }
  }
  return 1 << 26;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int c = 0;
  pair<int, int> pr[9];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        pr[0] = make_pair(i, j);
      } else if (s[i][j] >= '0' && s[i][j] <= '8') {
        pr[s[i][j] - '0'] = make_pair(i, j);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    c += bfs(pr[i - 1].first, pr[i - 1].second, i);
  }
  cout << c << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

string s[1000];
int h, w, n;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int bfs(int x, int y, int now) {
  queue<pair<int, int>> Q;
  Q.push(make_pair(8, x * 1000 + y));
  while (!Q.empty()) {
    pair<int, int> a = Q.front();
    Q.pop();
    int tx = a.second / 1000, ty = a.second % 1000;
    if (s[tx][ty] - '0' == now) {
      return a.first / 10;
    }
    for (int r = 0; r < 4; r++) {
      if (abs(a.first % 10 - r) == 2)
        continue;
      int sx = tx + dx[r];
      int sy = ty + dy[r];
      if (sx < 0 || sy < 0 || sx >= h || sy >= w || s[sx][sy] == 'X')
        continue;
      Q.push(make_pair((a.first / 10 + 1) * 10 + r, sx * 1000 + sy));
    }
  }
  return 1 << 26;
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  int c = 0;
  pair<int, int> pr[9];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        pr[0] = make_pair(i, j);
      } else if (s[i][j] >= '0' && s[i][j] <= '8') {
        pr[s[i][j] - '0'] = make_pair(i, j);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    c += bfs(pr[i - 1].first, pr[i - 1].second, i);
  }
  cout << c << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

int H, W, N;
char map[1024][1024];
int dist[1024][1024];
int factories[10];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

int bfs(int from, int to) {
  queue<int> q;
  q.push(from);
  dist[from / W][from % W] = 0;
  int fx = to % W;
  int fy = to / W;

  while (!q.empty()) {
    int x = q.front() % W;
    int y = q.front() / W;
    q.pop();
    if (x == fx && y == fy) {
      return dist[fy][fx];
    }
    for (int d = 0; d < 4; ++d) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      if (0 <= nx && nx < W && 0 <= ny && ny < H && map[ny][nx] != 'X' &&
          dist[ny][nx] == -1) {
        q.push(ny * W + nx);
        dist[ny][nx] = dist[y][x] + 1;
      }
    }
  }
}

void solve() {
  int result = 0;
  for (int i = 0; i < N; ++i) {
    memset(dist, -1, sizeof dist);
    result += bfs(factories[i], factories[i + 1]);
  }
  cout << result << endl;
}

int main() {
  cin >> H >> W >> N;
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      cin >> map[h][w];
      if (isdigit(map[h][w])) {
        factories[map[h][w] - '0'] = h * W + w;
      }
      if (map[h][w] == 'S') {
        factories[0] = h * W + w;
      }
    }
  }
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

int H, W, N;
char map[1024][1024];
bool visited[1024][1024];
int factories[10];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void solve() {
  int result = 0;
  for (int i = 0; i < N; ++i) {
    queue<pair<int, int>> loc_time;
    memset(visited, false, sizeof visited);
    int fx = factories[i + 1] % W;
    int fy = factories[i + 1] / W;
    loc_time.push(make_pair(factories[i], 0));

    while (!loc_time.empty()) {
      int loc = loc_time.front().first;
      int time = loc_time.front().second;
      loc_time.pop();
      int x = loc % W;
      int y = loc / W;
      visited[y][x] = true;
      if (y == fy && x == fx) {
        result += time;
        break;
      }
      for (int i = 0; i < 4; ++i) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (0 <= nx && nx < W && 0 <= ny && ny < H && map[ny][nx] != 'X' &&
            !visited[ny][nx]) {
          loc_time.push(make_pair(ny * W + nx, time + 1));
        }
      }
    }
  }
  cout << result << endl;
}

int main() {
  cin >> H >> W >> N;
  for (int h = 0; h < H; ++h) {
    for (int w = 0; w < W; ++w) {
      cin >> map[h][w];
      if (isdigit(map[h][w])) {
        factories[map[h][w] - '0'] = h * W + w;
      }
      if (map[h][w] == 'S') {
        factories[0] = h * W + w;
      }
    }
  }
  solve();
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define mp make_pair
#define f first
#define s second

using namespace std;

typedef pair<int, int> P;

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h, w;
string grid[1000];
int d[1000][1000];

void bfs(P S) {

  queue<P> que;
  que.push(S);
  d[S.f][S.s] = 0;

  while (!que.empty()) {
    P now = que.front();

    que.pop();
    for (int i = 0; i < 4; i++) {
      if (0 <= now.f + dx[i] && 0 <= now.s + dy[i] && now.f + dx[i] < h &&
          now.s + dy[i] < w && grid[now.f + dx[i]][now.s + dy[i]] != 'X') {

        if (d[now.f][now.s] + 1 < d[now.f + dx[i]][now.s + dy[i]]) {
          d[now.f + dx[i]][now.s + dy[i]] = d[now.f][now.s] + 1;
          que.push(P(now.f + dx[i], now.s + dy[i]));
        }
      }
    }
  }
}

int main(void) {
  int n;
  P v[1001];

  cin >> h >> w >> n;

  for (int i = 0; i < h; i++)
    cin >> grid[i];

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == 'S')
        v[0] = mp(i, j);
      if (grid[i][j] != 'X' && grid[i][j] != '.')
        v[grid[i][j] - '0'] = mp(i, j);
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < h; j++)
      for (int k = 0; k < w; k++)
        d[j][k] = 100000000;
    bfs(v[i]);
    ans += d[v[i + 1].f][v[i + 1].s];
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define mp make_pair
#define f first
#define s second

using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> T;

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int h, w;
string grid[1000];

int bfs(P S, P G) {

  queue<T> que;
  que.push(T(0, S));

  while (!que.empty()) {
    T now = que.front();
    if (now.s.f == G.f && now.s.s == G.s)
      return now.f;
    grid[now.s.f][now.s.s] = 'X';

    que.pop();
    for (int i = 0; i < 4; i++) {
      if (0 <= now.s.f + dx[i] && 0 <= now.s.s + dy[i] && now.s.f + dx[i] < h &&
          now.s.s + dy[i] < w &&
          grid[now.s.f + dx[i]][now.s.s + dy[i]] != 'X') {
        que.push(T(now.f + 1, P(now.s.f + dx[i], now.s.s + dy[i])));
      }
    }
  }
}

int main(void) {
  int n;
  string in[1000];
  P v[1001];

  cin >> h >> w >> n;

  for (int i = 0; i < h; i++)
    cin >> in[i];

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (in[i][j] == 'S')
        v[0] = mp(i, j);
      if (in[i][j] != 'X' && in[i][j] != '.')
        v[in[i][j] - '0'] = mp(i, j);
    }
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < h; j++)
      grid[j] = in[j];
    ans += bfs(v[i], v[i + 1]);
  }

  cout << ans << endl;

  return 0;
}