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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct T {
  ll first, second, third;
};
typedef double db;
string c[1001];
const ll INF = 0xffffffffff;
P s[10], g[10];
ll vx[4] = {1, 0, -1, 0};
ll vy[4] = {0, 1, 0, -1};
ll n, h, w;
bool visited[1001][1001];

ll bfs(ll sx, ll sy, ll gx, ll gy) {
  queue<T> q;
  while (!q.empty())
    q.pop();
  q.push((T){sx, sy, 0});
  visited[sy][sx] = 1;
  while (1) {
    T t = q.front();
    q.pop();
    if (t.first == gx && t.second == gy)
      return t.third;
    for (ll i = 0; i < 4; i++) {
      ll nx = t.first + vx[i];
      ll ny = t.second + vy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == '.' &&
          !visited[ny][nx]) {
        q.push((T){nx, ny, t.third + 1});
        visited[ny][nx] = 1;
      }
    }
  }
}

int main() {
  scanf("%lld%lld%lld", &h, &w, &n);
  for (ll i = 0; i < h; i++)
    cin >> c[i];
  for (ll i = 0; i < h; i++)
    for (ll j = 0; j < w; j++) {
      if (c[i][j] == 'S') {
        s[0] = P(i, j); // y x
        c[i][j] = '.';
      }
      if ('1' <= c[i][j] && c[i][j] <= '9') {
        s[c[i][j] - '0'] = P(i, j);
        g[c[i][j] - '0' - 1] = P(i, j);
        c[i][j] = '.';
      }
    }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    fill(visited[0], visited[1000], 0);
    ans += bfs(s[i].second, s[i].first, g[i].second, g[i].first);
  }
  printf("%lld\\n", ans);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
struct T {
  ll first, second, third;
};
typedef double db;
string c[1001];
const ll INF = 0xffffffffff;
P s[10], g[10];
ll vx[4] = {1, 0, -1, 0};
ll vy[4] = {0, 1, 0, -1};
ll n, h, w;
bool visited[1001][1001];

ll bfs(ll sx, ll sy, ll gx, ll gy) {
  queue<T> q;
  q.push((T){sx, sy, 0});
  while (1) {
    T t = q.front();
    q.pop();
    visited[t.second][t.first] = 1;
    if (t.first == gx && t.second == gy)
      return t.third;
    for (ll i = 0; i < 4; i++) {
      ll nx = t.first + vx[i];
      ll ny = t.second + vy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h && c[ny][nx] == '.' &&
          !visited[ny][nx])
        q.push((T){nx, ny, t.third + 1});
    }
  }
}

int main() {
  scanf("%lld%lld%lld", &h, &w, &n);
  for (ll i = 0; i < h; i++)
    cin >> c[i];
  for (ll i = 0; i < h; i++)
    for (ll j = 0; j < w; j++) {
      if (c[i][j] == 'S') {
        s[0] = P(i, j); // y x
        c[i][j] = '.';
      }
      if ('1' <= c[i][j] && c[i][j] <= '9') {
        s[c[i][j] - '0'] = P(i, j);
        g[c[i][j] - '1'] = P(i, j);
        c[i][j] = '.';
      }
    }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    fill(visited[0], visited[1000], 0);
    ans += bfs(s[i].second, s[i].first, g[i].second, g[i].first);
  }
  printf("%lld\\n", ans);
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
struct Data {
  int x;
  int y;
  int t;
};
bool vis[1005][1005];
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  string fi[1020];
  Data init;
  for (int i = 0; i < h; i++) {
    cin >> fi[i];
    for (int j = 0; j < w; j++) {
      if (fi[i][j] == 'S') {
        init.x = j;
        init.y = i;
      }
    }
  }
  init.t = 0;

  queue<Data> Q;
  Q.push(init);
  int now = 1;
  while (!Q.empty()) {
    Data D = Q.front();
    Q.pop();
    D.t++;
    if (vis[D.y][D.x])
      continue;
    if (fi[D.y][D.x] == now + '0') {
      now++;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          vis[i][j] = false;
        }
      }
      Q = queue<Data>();
      Q.push(D);
    }
    vis[D.y][D.x] = true;
    if (now == n + 1) {
      cout << D.t - 1 << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int x = D.x + dx[i];
      int y = D.y + dy[i];
      if (0 <= x && x < w && 0 <= y && y < h) {
        if (fi[y][x] == 'X')
          continue;
        if (vis[y][x])
          continue;
        Data E = D;
        E.x = x, E.y = y;
        Q.push(E);
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
struct Data {
  int x;
  int y;
  int t;
};
bool vis[1005][1005];
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  string fi[1020];
  Data init;
  for (int i = 0; i < h; i++) {
    cin >> fi[i];
    for (int j = 0; j < w; j++) {
      if (fi[i][j] == 'S') {
        init.x = j;
        init.y = i;
      }
    }
  }
  init.t = 0;

  queue<Data> Q;
  Q.push(init);
  int now = 1;
  while (!Q.empty()) {
    Data D = Q.front();
    Q.pop();
    D.t++;
    vis[D.y][D.x] = true;
    if (fi[D.y][D.x] == now + '0') {
      now++;
      for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
          vis[i][j] = false;
        }
      }
      Q = queue<Data>();
      Q.push(D);
    }
    if (now == n + 1) {
      cout << D.t - 1 << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int x = D.x + dx[i];
      int y = D.y + dy[i];
      if (0 <= x && x < w && 0 <= y && y < h) {
        if (fi[y][x] == 'X')
          continue;
        if (vis[y][x])
          continue;
        Data E = D;
        E.x = x, E.y = y;
        Q.push(E);
      }
    }
  }
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>

using namespace std;

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

typedef long long ll;
// 各地点の行と列をPairで持たせる
typedef pair<int, int> P;
int y, x, n, nx, ny, nextx, nexty;
int ans = 0;
int counter = 0;
int d[1100][1100];
char c[1100][1100];
int d1[] = {0, 0, -1, 1};
int d2[] = {1, -1, 0, 0};
int dc = 0;
char key[] = "S123456789";
P st[10];

int bfs(P, P);

int main() {
  cin >> y >> x >> n;
  REP(i, y) { cin >> c[i]; }
  REP(i, y) {
    REP(j, x) {
      REP(k, n + 1) {
        if (c[i][j] == key[k])
          st[k] = P(i, j);
      }
    }
  }

  REP(i, n) {
    memset(d, -1, sizeof(d));

    ans += bfs(st[i], st[i + 1]);
  }
  cout << ans << endl;
  return 0;
}

int bfs(P s, P g) {
  queue<P> que;
  que.push(s);
  d[s.first][s.second] = 0;
  while (que.size()) {
    P p = que.front();
    que.pop();
    ny = p.first;
    nx = p.second;
    dc = d[ny][nx];

    if (p == g)
      break;

    REP(i, 4) {
      nextx = nx + d1[i];
      nexty = ny + d2[i];

      if (nexty < 0 || nexty >= y || nextx < 0 || nextx >= x)
        continue;

      if (c[nexty][nextx] == 'X' || d[nexty][nextx] != -1)
        continue;

      que.push(P(nexty, nextx));

      d[nexty][nextx] = dc + 1;
    }
  }
  return d[g.first][g.second];
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>

using namespace std;

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

typedef long long ll;
// 各地点の行と列をPairで持たせる
typedef pair<int, int> P;
int y, x, n, nx, ny, nextx, nexty;
int ans = 0;
int counter = 0;
int d[1000][1000] = {0};
char c[1100][1100];
int d1[] = {0, 0, -1, 1};
int d2[] = {1, -1, 0, 0};
int dc = 0;

int main() {
  cin >> y >> x >> n;
  queue<P> que;
  REP(i, y) { cin >> c[i]; }
  REP(i, y) {
    bool isFoundStart = false;
    REP(j, x) {
      if (c[i][j] == 'S') {
        isFoundStart = true;
        que.push(P(i, j));
        d[i][j] = 0;
        break;
      }
    }
    if (isFoundStart)
      break;
  }

  while (counter < n) {
    int memo[1100][1100] = {0};
    while (que.size()) {
      P p = que.front();
      que.pop();
      ny = p.first;
      nx = p.second;
      dc = d[ny][nx];

      if (c[ny][nx] - '0' == counter + 1) {
        counter++;

        if (counter == n) {
          cout << dc << endl;
          return 0;
        }
        queue<P> empty;
        swap(que, empty);
        que.push(P(ny, nx));
        c[ny][nx] = '.';

        break;
      }

      REP(i, 4) {
        nextx = nx + d1[i];
        nexty = ny + d2[i];

        if (nexty < 0 || nexty >= y || nextx < 0 || nextx >= x)
          continue;
        if (c[nexty][nextx] == 'X' || memo[nexty][nextx] != 0)
          continue;

        que.push(P(nexty, nextx));

        d[nexty][nextx] = dc + 1;
      }
      memo[ny][nx] = 1;
    }
  }
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
  LL to, cost;
};

int H, W, N;
std::string field[1123];
int Sx, Sy, x[11], y[11], ans;

int d[1123][1123];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void bfs(LL sx, LL sy, LL gx, LL gy) {
  std::queue<P> que;
  memset(d, -1, sizeof(d));
  que.push({sx, sy});
  d[sx][sy] = 0;
  while (que.size() > 0) {
    auto pos = que.front();
    que.pop();
    if (pos.first == gx && pos.second == gy) {
      break;
    }
    rep(i, 4) {
      auto toX = pos.first + dx[i], toY = pos.second + dy[i];
      if (0 <= toX && toX < H && 0 <= toY && toY < W) {
        if (field[toX][toY] != 'X' && d[toX][toY] == -1) {
          que.push({toX, toY});
          d[toX][toY] = d[pos.first][pos.second] + 1;
        }
      }
    }
  }
}

int main() {
  in >> H >> W >> N;
  rep(i, H) { in >> field[i]; }

  rep(i, H) {
    rep(j, W) {
      if (field[i][j] == 'X') {
        continue;
      }
      if (field[i][j] == 'S') {
        Sx = i;
        Sy = j;
      } else if (field[i][j] != '.') {
        x[field[i][j] - '0'] = i;
        y[field[i][j] - '0'] = j;
      }
    }
  }
  x[0] = Sx;
  y[0] = Sy;
  rep(i, N) {
    bfs(x[i], y[i], x[i + 1], y[i + 1]);
    ans += d[x[i + 1]][y[i + 1]];
  }
  out << ans << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define MOD 1000000007
#define INF 11234567890
#define in std::cin
#define out std::cout
#define rep(i, N) for (LL i = 0; i < N; ++i)
typedef long long int LL;
typedef std::pair<LL, LL> P;
struct edge {
  LL to, cost;
};

int H, W, N;
std::string field[1123];
int Sx, Sy, x[11], y[11], ans;

int d[1123][1123];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void bfs(LL sx, LL sy, LL gx, LL gy) {
  std::queue<P> que;
  memset(d, -1, sizeof(d));
  que.push({sx, sy});
  d[sx][sy] = 0;
  while (que.size() > 0) {
    auto pos = que.front();
    que.pop();
    if (pos.first == gx && pos.second == gy) {
      break;
    }
    rep(i, 4) {
      auto toX = pos.first + dx[i], toY = pos.second + dy[i];
      if (0 <= toX && toX < H && 0 <= toY && toY < W) {
        if (field[toX][toY] != 'X' && d[toX][toY] == -1) {
          que.push({toX, toY});
          d[toX][toY] = d[pos.first][pos.second] + 1;
        }
      }
    }
  }
}

int main() {
  in >> H >> W >> N;
  rep(i, H) { in >> field[i]; }

  rep(i, H) {
    rep(j, W) {
      if (field[i][j] == 'S') {
        Sx = i;
        Sy = j;
      } else if (field[i][j] != '.') {
        x[field[i][j] - '0'] = i;
        y[field[i][j] - '0'] = j;
      }
    }
  }
  x[0] = Sx;
  y[0] = Sy;
  rep(i, N) {
    bfs(x[i], y[i], x[i + 1], y[i + 1]);
    ans += d[x[i + 1]][y[i + 1]];
  }
  out << ans << std::endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <iostream>
#include <queue>
typedef std::pair<int, int> P;

const int INF = 100000;
const int MAX_H = 1000;
const int MAX_W = 1000;
const int MAX_N = 9;

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
int h, w, n;
int d[MAX_H][MAX_W];
char ch[MAX_H][MAX_W];
P p[MAX_N + 1];

int bfs(P s, P g) {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j)
      d[i][j] = INF;
  }
  std::queue<P> que;
  que.push(s);
  d[s.second][s.first] = 0;
  while (!que.empty()) {
    P x = que.front();
    que.pop();
    if (x == g)
      break;
    for (int i = 0; i < 4; ++i) {
      int nx = x.first + dx[i];
      int ny = x.second + dy[i];
      if (nx >= 0 && ny >= 0 && nx < w && ny < h && d[ny][nx] == INF &&
          ch[ny][nx] != 'X') {
        que.push(P(nx, ny));
        d[ny][nx] = d[x.second][x.first] + 1;
      }
    }
  }
  return d[g.second][g.first];
}

int solve() {
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    res += bfs(p[i - 1], p[i]);
  }
  return res;
}

int main() {
  std::cin >> h >> w >> n;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      std::cin >> ch[i][j];
      if (std::isdigit(ch[i][j])) {
        p[atoi(&ch[i][j])].first = j;
        p[atoi(&ch[i][j])].second = i;
      } else if (ch[i][j] == 'S') {
        p[0].first = j;
        p[0].second = i;
      }
    }
  }
  std::cout << solve() << std::endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

typedef std::pair<int, int> P;

int h, w, PX[4] = {1, -1, 0, 0}, PY[4] = {0, 0, 1, -1};
char ch[100][100];
int sum = -1;

void dfs(P p) {
  ch[p.first][p.second] = ',';
  for (int i = 0; i < 4; ++i) {
    if (p.first + PY[i] >= 0 && h > p.first + PY[i] && w > p.second + PX[i] &&
        p.second + PX[i] >= 0) {
      if (ch[p.first][p.second] == ch[p.first + PY[i]][p.second + PX[i]]) {
        dfs(P(p.first + PY[i], p.second + PX[i]));
      }
    }
  }
}

void solve() {
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (ch[i][j] != ',') {
        ++sum;
        dfs(P(i, j));
      }
    }
  }
}

int main() {
  while (1) {
    std::cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        std::cin >> ch[i][j];
      }
    }
    solve();
    std::cout << sum << std::endl;
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct PP {
  int x;
  int y;
  int Z;
  int c;
};
char s[1002][1002], C[11] = {"0123456789"};
int A[1002][1002], d;
int main() {
  PP S;
  queue<PP> P;
  int w, h;
  int d = 0, i = 0, j = 0, l = -1;
  int x[4] = {1, 0, -1, 0};
  int y[4] = {0, 1, 0, -1};
  scanf("%d %d %d", &h, &w, &d);
  // memset(A,0,sizeof(A));
  for (i = 0; i < h; i++) {
    scanf("%s", s[i]);
  }
  for (i = 0; i < h; i++) {
    for (j = 0; j < w; j++) {

      if (s[i][j] == 'S') {
        S.x = j;
        S.y = i;
        S.Z = 0;
        S.c = 1;
        A[S.y][S.x] = 1;
        P.push(S);
      }
    }
  }
  while (1) {
    l++;

    if (l == 4) {
      l = 0;
      P.pop();
    }
    S = P.front();

    S.x += x[l];
    S.y += y[l];
    S.Z++;
    if (s[S.y][S.x] == 'X' || S.y == h || S.x == w || S.x == -1 || S.y == -1) {
      continue;
    }
    if (A[S.y][S.x] == 1) {
      continue;
    }
    A[S.y][S.x] = 1;
    if (C[S.c] == s[S.y][S.x]) {
      S.c++;
      if (S.c == d + 1) {
        break;
      }
      while (!P.empty()) {
        P.pop();
      }
      memset(A, 0, sizeof(A));
      l = -1;
    }
    P.push(S);
  }
  printf("%d\\n", S.Z);
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct PP {
  int x;
  int y;
  int Z;
  int c;
};
char s[1002][1002], C[11] = {"0123456789"};
int A[1002][1002], d;
int main() {
  PP S;
  queue<PP> P;
  int w, h;
  int d = 0, i = 0, j = 0, l = -1;
  int x[4] = {1, 0, -1, 0};
  int y[4] = {0, 1, 0, -1};
  scanf("%d %d %d", &h, &w, &d);
  // memset(A,0,sizeof(A));
  for (i = 0; i < h; i++) {
    scanf("%s", s[i]);
  }
  for (i = 0; i <= h; i++) {
    for (j = 0; j <= w; j++) {
      A[i][j] = 0;
      if (s[i][j] == 'S') {
        S.x = j;
        S.y = i;
        S.Z = 0;
        S.c = 1;
        A[S.y][S.x] = 1;
        P.push(S);
      }
    }
  }
  while (1) {
    l++;

    if (l == 4) {
      l = 0;
      P.pop();
    }
    S = P.front();

    S.x += x[l];
    S.y += y[l];
    S.Z++;
    if (A[S.y][S.x] == 1 || s[S.y][S.x] == 'X' || S.y == h || S.x == w ||
        S.x == -1 || S.y == -1) {
      continue;
    }
    A[S.y][S.x] = 1;
    if (C[S.c] == s[S.y][S.x]) {
      S.c++;
      if (S.c == d + 1) {
        break;
      }
      while (!P.empty()) {
        P.pop();
      }
      for (i = 0; i <= h; i++) {
        for (j = 0; j <= h; j++) {
          A[i][j] = 0;
        }
      }
      l = -1;
    }
    P.push(S);
  }
  printf("%d\\n", S.Z);
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

// 14:54
#include <queue>
#include <stdio.h>

using namespace std;

typedef pair<int, int> P;

int h, w, n;
char field[1000][1001];
queue<P> qu;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int d[1000][1000];

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = -1;
    }
  }
  queue<P> empty;
  swap(qu, empty);
}

int solve() {
  int hp = 1;
  int cnt;
  init();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        qu.push(P(i, j));
        d[i][j] = 0;
      }
    }
  }
  while (qu.size()) {
    P p = qu.front();
    qu.pop();
    int y = p.first;
    int x = p.second;
    if (hp == n + 1)
      return d[y][x];
    if (hp == field[y][x] - '0') {
      hp++;
      cnt = d[y][x];
      init();
      d[y][x] = cnt;
    }
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && field[ny][nx] != 'X' &&
          d[ny][nx] == -1) {
        d[ny][nx] = d[y][x] + 1;
        qu.push(P(ny, nx));
      }
    }
  }
  return 0;
}

int main() {
  scanf("%d %d %d", &h, &w, &n);
  for (int i = 0; i < h; i++) {
    scanf("%s", field[i]);
  }
  printf("%d\\n", solve() - 1);
  return 0;
}

Error: 0

Fixed submission:

// 14:54
#include <queue>
#include <stdio.h>

using namespace std;

typedef pair<int, int> P;

int h, w, n;
char field[100][101];
queue<P> qu;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int d[100][100];

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = -1;
    }
  }
  queue<P> empty;
  swap(qu, empty);
}

int solve() {
  int hp = 1;
  int cnt;
  init();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        qu.push(P(i, j));
        d[i][j] = 0;
      }
    }
  }
  while (qu.size()) {
    P p = qu.front();
    qu.pop();
    int y = p.first;
    int x = p.second;
    if (hp == n + 1)
      return d[y][x];
    if (hp == field[y][x] - '0') {
      hp++;
      cnt = d[y][x];
      init();
      d[y][x] = cnt;
    }
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && field[ny][nx] != 'X' &&
          d[ny][nx] == -1) {
        d[ny][nx] = d[y][x] + 1;
        qu.push(P(ny, nx));
      }
    }
  }
  return 0;
}

int main() {
  scanf("%d %d %d", &h, &w, &n);
  for (int i = 0; i < h; i++) {
    scanf("%s", field[i]);
  }
  printf("%d\\n", solve() - 1);
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <cstdio>
#include <queue>

#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> pii;

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

int h, w;
char grid[1000][1001];

int shortestDistance(int sx, int sy, int gx, int gy) {
  static bool visited[1000][1000];
  rep(i, h) rep(j, w) visited[i][j] = false;
  visited[sy][sx] = true;

  queue<pair<int, pii>> qu;
  qu.push(mp(0, mp(sy, sx)));
  while (1) {
    pair<int, pii> a = qu.front();
    qu.pop();
    int d = a.first, x = a.second.second, y = a.second.first;
    if (x == gx && y == gy)
      return d;

    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (0 <= xx && xx < w && 0 <= yy && yy < h && grid[yy][xx] != 'X' &&
          !visited[yy][xx]) {
        visited[yy][xx] = true;
        qu.push(mp(d + 1, mp(yy, xx)));
      }
    }
  }
}

int main() {
  int n;
  scanf("%d%d%d", &h, &w, &n);
  int cx[10], cy[10];
  rep(i, h) {
    scanf("%s", grid[i]);
    rep(j, w) {
      if (grid[i][j] == 'S')
        cx[0] = j, cy[0] = i;
      if (isdigit(grid[i][j]))
        cx[grid[i][j] - '0'] = j, cy[grid[i][j] - '0'] = i;
    }
  }

  int cost = 0;
  rep(i, n) cost += shortestDistance(cx[i], cy[i], cx[i + 1], cy[i + 1]);
  printf("%d\\n", cost);

  return 0;
}

Error: TLE

Fixed submission:

#include <cctype>
#include <cstdio>
#include <queue>

#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef pair<int, int> pii;

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

int h, w;
char grid[1000][1001];

int shortestDistance(int sx, int sy, int gx, int gy) {
  static bool visited[1000][1000];
  rep(i, h) rep(j, w) visited[i][j] = false;
  visited[sy][sx] = true;

  queue<pair<int, pii>> qu;
  qu.push(mp(0, mp(sx, sy)));
  while (1) {
    pair<int, pii> a = qu.front();
    qu.pop();
    int d = a.first, x = a.second.first, y = a.second.second;
    if (x == gx && y == gy)
      return d;

    rep(i, 4) {
      int xx = x + dx[i], yy = y + dy[i];
      if (0 <= xx && xx < w && 0 <= yy && yy < h && grid[yy][xx] != 'X' &&
          !visited[yy][xx]) {
        visited[y][x] = true;
        qu.push(mp(d + 1, mp(xx, yy)));
      }
    }
  }
}

int main() {
  int n;
  scanf("%d%d%d", &h, &w, &n);
  int cx[10], cy[10];
  rep(i, h) {
    scanf("%s", grid[i]);
    rep(j, w) {
      if (grid[i][j] == 'S')
        cx[0] = j, cy[0] = i;
      if (isdigit(grid[i][j]))
        cx[grid[i][j] - '0'] = j, cy[grid[i][j] - '0'] = i;
    }
  }

  int cost = 0;
  rep(i, n) cost += shortestDistance(cx[i], cy[i], cx[i + 1], cy[i + 1]);
  printf("%d\\n", cost);

  return 0;
}