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

#include <climits>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
int d[1111][1111];
char input[1111][1111];
bool visited[1111][1111];
int H, W, N, S_x, S_y, answer = 0;
const int dirx[] = {0, -1, 0, 1};
const int diry[] = {1, 0, -1, 0};
int now = '1';
int D[11];
queue<int> q;
void BFS();
void clean();
int main() {
  int i, j;
  char ch;
  cin >> H >> W >> N;
  for (i = 0; i < H * W; i++) {
    cin >> ch;
    input[i % W][i / W] = ch;
    d[i % W][i / W] = 0;
    if (ch == 'S') {
      S_x = i % W;
      S_y = i / W;
    }
  }
  while (now <= N + 48) {
    BFS();
    now++;
  }
  for (i = 1; i <= N; i++) {
    answer += D[i];
  }
  cout << answer << endl;
}
void BFS() {
  int x, y, k, mx, my;
  q.push(S_x);
  q.push(S_y);
  visited[S_x][S_y] = true;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    y = q.front();
    q.pop();
    for (k = 0; k < 4; k++) {
      mx = x + dirx[k];
      my = y + diry[k];
      if (mx < W && mx >= 0 && my < H && my >= 0) {
        if (input[mx][my] != 'X' && !visited[mx][my]) {
          q.push(mx);
          q.push(my);
          d[mx][my] = d[x][y] + 1;
          if (input[mx][my] == (char)now) {
            S_x = mx;
            S_y = my;
            D[now - 48] = d[mx][my];
          }
          visited[mx][my] = true;
        }
      }
    }
  }
  clean();
}
void clean() {
  int i, j;
  for (i = 0; i < W; i++)
    for (j = 0; j < H; j++) {
      visited[i][j] = false;
      d[i][j] = 0;
    }
}

Error: 0

Fixed submission:

#include <climits>
#include <cstdlib>
#include <iostream>
#include <queue>
using namespace std;
int d[1000][1000];
char input[1000][1000];
bool visited[1000][1000];
int H, W, N, S_x, S_y, answer = 0;
const int dirx[] = {0, -1, 0, 1};
const int diry[] = {1, 0, -1, 0};
int now = '1';
int D[10];
queue<char> q;
void BFS();
void clean();
int main() {
  int i, j;
  char ch;
  cin >> H >> W >> N;
  for (i = 0; i < H * W; i++) {
    cin >> ch;
    input[i % W][i / W] = ch;
    d[i % W][i / W] = 0;
    if (ch == 'S') {
      S_x = i % W;
      S_y = i / W;
    }
  }
  while (now <= N + 48) {
    BFS();
    now++;
  }
  for (i = 1; i <= N; i++)
    answer += D[i];
  cout << answer << endl;
}
void BFS() {
  int x, y, k, mx, my;
  q.push(S_x);
  q.push(S_y);
  visited[S_x][S_y] = true;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    y = q.front();
    q.pop();
    for (k = 0; k < 4; k++) {
      mx = x + dirx[k];
      my = y + diry[k];
      if (mx < W && mx >= 0 && my < H && my >= 0) {
        if (input[mx][my] != 'X' && !visited[mx][my]) {
          q.push(mx);
          q.push(my);
          d[mx][my] = d[x][y] + 1;
          if (input[mx][my] == (char)now) {
            S_x = mx;
            S_y = my;
            D[now - 48] = d[mx][my];
          }
          visited[mx][my] = true;
        }
      }
    }
  }
  clean();
}
void clean() {
  int i, j;
  for (i = 0; i <= W; i++)
    for (j = 0; j <= H; j++) {
      visited[i][j] = false;
      d[i][j] = 0;
    }
}
Problem p00481 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct P {
  int x, y;
};

using namespace std;

char map[1005][1005];
int h, w, n;

int distance(char start, char goal) {
  bool visited[1005][1005];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      visited[i][j] = false;

  queue<pair<P, int>> q;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (map[i][j] == start)
        q.push(pair<P, int>({j, i}, 0));

  while (!q.empty()) {
    pair<P, int> t = q.front();
    q.pop();
    struct P p = t.first;
    int d = t.second;

    if (p.x < 0 || p.y < 0 || p.x >= w || p.y >= h)
      continue;
    if (map[p.y][p.x] == 'X')
      continue;
    if (visited[p.y][p.x])
      continue;
    if (map[p.y][p.x] == goal)
      return d;
    visited[p.y][p.x] = true;

    q.push(pair<P, int>({p.x + 1, p.y}, d + 1));
    q.push(pair<P, int>({p.x, p.y + 1}, d + 1));
    q.push(pair<P, int>({p.x - 1, p.y}, d + 1));
    q.push(pair<P, int>({p.x, p.y - 1}, d + 1));
  }
  return -1;
}

int main(void) {
  int res = 0;

  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++)
    scanf("%s", map[i]);
  res += distance('S', '1');
  for (int i = 1; i < n; i++)
    res += distance('0' + i, '1' + i);

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>

char map[1000][1000];
int h, w, res;

void dfs(int x, int y, char c) {
  if (x < 0 || y < 0 || x >= w || y >= h)
    return;
  if (map[y][x] == c) {
    map[y][x] = '.';
    dfs(x - 1, y, c);
    dfs(x, y - 1, c);
    dfs(x + 1, y, c);
    dfs(x, y + 1, c);
  }
}

int main(void) {
  while (true) {
    res = 0;
    memset(map, 0, sizeof(map));

    scanf("%d%d", &h, &w);
    if (!h && !w)
      break;
    for (int i = 0; i < h; i++)
      scanf("%s", map[i]);

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (map[i][j] != '.') {
          res++;
          dfs(j, i, map[i][j]);
        }
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int INF = 2000000;
char fl[1010][1010];
int d[1010][1010];

int bfs(int sx, int sy, int gx, int gy, int mx, int my) {
  typedef pair<int, int> P;
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

  for (int i = 0; i < mx; i++)
    for (int j = 0; j < my; j++)
      d[i][j] = INF;

  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop();

    if (p.first == gx && p.second == gy)
      break;

    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < mx && 0 <= ny && ny < my && fl[nx][ny] != 'X' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  int al = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> fl[i][j];
    }
  }
  char a[10] = {'S', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  for (int i = 0; i < n; i++) {
    int sx, sy;
    int gx, gy;
    for (int j = 0; j < h; j++) {
      for (int k = 0; k < w; k++) {

        if (fl[j][k] == a[i]) {
          sx = j;
          sy = k;
        }
        if (fl[j][k] == a[i + 1]) {
          gx = j;
          gy = k;
        }
      }
    }

    al += bfs(sx, sy, gx, gy, h, w);
  }

  cout << al << endl;
  getchar();
  getchar();
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#define INF 5000000
using namespace std;

char fl[1010][1010];

int bfs(int sx, int sy, int gx, int gy, int mx, int my) {
  typedef pair<int, int> P;
  int d[1010][1010];
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

  for (int i = 0; i < mx; i++)
    for (int j = 0; j < my; j++)
      d[i][j] = INF;

  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop();

    if (p.first == gx && p.second == gy)
      break;

    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < my && 0 <= ny && ny < mx && fl[nx][ny] != 'X' &&
          d[nx][ny] != INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}

int main() {
  int h, w, n;
  cin >> h >> w >> n;
  int al = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> fl[i][j];
    }
  }
  char a[10] = {'S', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  for (int i = 0; i < n; i++) {
    int sx, sy;
    int gx, gy;
    for (int j = 0; j < h; j++) {
      for (int k = 0; k < w; k++) {

        if (fl[j][k] == a[i]) {
          sx = j;
          sy = k;
        }
        if (fl[j][k] == a[i + 1]) {
          gx = j;
          gy = k;
        }
      }
    }

    al += bfs(sx, sy, gx, gy, h, w);
  }
  cout << al << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;
int w, h, n;
char map[1024][1024];
int d[1024][1024];
const int direction[4][2] = {
    {-1, 0},
    {1, 0},
    {0, -1},
    {0, 1},
};

int factory[16][2];
typedef pair<int, int> P;

int bfs(const int &sx, const int &sy, const int &gx, const int &gy) {
  // memset(d, -1, sizeof(d));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      d[j][i] = -1;
    }
  }
  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;
  while (que.size()) {
    const P p = que.front();
    que.pop();
    // 如果是?点就?束
    if (p.first == gx && p.second == gy) {
      break;
    }

    // 四方向漫游
    for (int i = 0; i < 4; ++i) {
      int nx = p.first + direction[i][0];
      int ny = p.second + direction[i][1];
      // 是否可以移?，并且?点没有???
      if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx][ny] != 'X' &&
          d[nx][ny] == -1) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }

  return d[gx][gy];
}

///////////////////////////SubMain//////////////////////////////////
int main(int argc, char *argv[]) {

  cin >> h >> w >> n;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> map[j][i];
    }
  }

  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (map[j][i] == 'S') {
        factory[0][0] = j;
        factory[0][1] = i;
        map[j][i] = '.';
      } else if (isdigit(map[j][i])) {
        int index = map[j][i] - '0';
        factory[index][0] = j;
        factory[index][1] = i;
      }
    }
  }

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

    step +=
        bfs(factory[i][0], factory[i][1], factory[i + 1][0], factory[i + 1][1]);
  }

  cout << step << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;

char ma[1005][1005];
int vis[1005][1005];
int w, h, n;

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

struct node {
  int x, y, time;
};
node start;

void bfs(node st, char ed) {
  queue<node> que;
  que.push(st);
  vis[st.x][st.y] = 1;
  while (!que.empty()) {
    node no = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int x = no.x + dx[i];
      int y = no.y + dy[i];
      if (vis[x][y] || x >= w || x < 0 || y >= h || y < 0 || ma[x][y] == 'X')
        continue;
      if (ma[x][y] == ed) {
        start.x = x;
        start.y = y;
        start.time = no.time + 1;
        return;
      }
      vis[x][y] = 1;
      node no1;
      no1.x = x;
      no1.y = y;
      no1.time = no.time + 1;
      que.push(no1);
    }
  }
}

int main() {
  while (~scanf("%d %d %d", &w, &h, &n)) {
    for (int i = 0; i < w; i++) {
      cin >> ma[i];
      for (int j = 0; j < h; j++)
        if (ma[i][j] == 'S') {
          start.x = i;
          start.y = j;
          start.time = 0;
          ma[i][j] = '0';
        }
    }
    for (int i = 0; i < n; i++) {
      memset(vis, 0, sizeof(vis));
      bfs(start, i + '1');
    }

    printf("%d\\n", start.time);
  }
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

// AOJ 0558
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define X first
#define Y second
using namespace std;

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

int H, W, N;
const int HMAX = 1008, WMAX = 1008;
char field[HMAX][WMAX];

bool legal(P p) {
  return 0 <= p.X and p.X < W and 0 <= p.Y and p.Y < H and
         field[p.X][p.Y] != 'X';
}

const int INF = 1e7;
int dist[HMAX][WMAX];

int distance(P start, P goal) {
  priority_queue<S, vector<S>, greater<S>> que;
  REP(x, W) REP(y, H) { dist[x][y] = INF; }

  dist[start.X][start.Y] = 0;
  que.push(S(0, start));

  while (!que.empty()) {
    S state = que.top();
    que.pop();
    P cur = state.second;
    if (dist[cur.X][cur.Y] < state.first)
      continue;

    const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    REP(i, 4) {
      P p = P(cur.X + dx[i], cur.Y + dy[i]);
      if (legal(p) and dist[p.X][p.Y] > dist[cur.X][cur.Y] + 1) {
        dist[p.X][p.Y] = dist[cur.X][cur.Y] + 1;
        que.push(S(dist[p.X][p.Y], p));
      }
    }
  }
  return dist[goal.X][goal.Y];
}

P search(char c) {
  REP(y, H) REP(x, W) {
    if (field[x][y] == c)
      return P(x, y);
  }
}

int solve() {
  int ans = 0;
  P start = search('S');
  for (int i = 1; i <= N; i++) {
    P goal = search('0' + i);
    ans += distance(start, goal);
    start = goal;
  }
  return ans;
}

int main() {
  cin >> H >> W >> N;
  REP(y, H) {
    string row;
    cin >> row;
    REP(x, W) { field[x][y] = row[x]; }
  }

  cout << solve() << endl;
}

Error: 0

Fixed submission:

// AOJ 0558
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define X first
#define Y second
using namespace std;

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

int H, W, N;
const int HMAX = 1008, WMAX = 1008;
char field[HMAX][WMAX];

bool legal(P p) { return 0 <= p.X and p.X < W and 0 <= p.Y and p.Y < H; }

int dist(P start, P goal) {
  priority_queue<S, vector<S>, greater<S>> que;
  que.push(S(0, start));
  while (!que.empty()) {
    S s = que.top();
    que.pop();
    P cur = s.second;

    if (cur.X == goal.X and cur.Y == goal.Y) {
      return s.first;
    }

    const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
    REP(i, 4) {
      P p = P(cur.X + dx[i], cur.Y + dy[i]);
      if (legal(p) and field[p.X][p.Y] != 'X') {
        que.push(S(s.first + 1, p));
      }
    }
  }
}

P search(char c) {
  REP(y, H) REP(x, W) {
    if (field[x][y] == c)
      return P(x, y);
  }
}

int solve() {
  int ans = 0;
  P start = search('S');
  for (int i = 1; i <= N; i++) {
    P goal = search('0' + i);
    ans += dist(start, goal);
    start = goal;
  }
  return ans;
}

int main() {
  cin >> H >> W >> N;
  REP(y, H) {
    string row;
    cin >> row;
    REP(x, W) { field[x][y] = row[x]; }
  }

  cout << solve() << endl;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <utility>
using namespace std;

int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

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

void bfs() {
  for (int n = 1; n < N + 1; n++) {
    queue<pair<int, int>> que;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i][j] = INF;
      }
    }
    que.push(start);
    d[start.first][start.second] = 0;

    while (que.size()) {
      pair<int, int> p = que.front();
      que.pop();
      if (field[p.first][p.second] == '0' + (char)n) {
        cheese = p;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
    }
    result += d[cheese.first][cheese.second];
    start = cheese;
  }
}

int main() {
  scanf(" %d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c", &field[i][j]);
      if (field[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  bfs();
  printf("%d\\n", result);
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <queue>
#include <utility>
using namespace std;

int H, W, N;
char field[1000][1000];
int d[1000][1000];
pair<int, int> start;
pair<int, int> cheese;
int INF = 1000000;
int result = 0;

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

void bfs() {
  for (int n = 1; n < N + 1; n++) {
    queue<pair<int, int>> que;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        d[i][j] = INF;
      }
    }
    que.push(start);
    d[start.first][start.second] = 0;

    while (que.size()) {
      pair<int, int> p = que.front();
      que.pop();
      if (field[p.first][p.second] == '0' + (char)n) {
        cheese = p;
        break;
      }

      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx <= H && 0 <= ny && ny <= W && field[nx][ny] != 'X' &&
            d[nx][ny] == INF) {
          que.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[p.first][p.second] + 1;
        }
      }
    }
    result += d[cheese.first][cheese.second];
    start = cheese;
  }
}

int main() {
  scanf(" %d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf(" %c", &field[i][j]);
      if (field[i][j] == 'S') {
        start.first = i;
        start.second = j;
      }
    }
  }
  bfs();
  printf("%d\\n", result);
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

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

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

const int vecx[4] = {1, 0, -1, 0};
const int vecy[4] = {0, -1, 0, 1};
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];

void bfs(T _goal, T _start) {
  std::queue<T> q;
  q.push(_start);
  while (q.size()) {
    T tmp = q.front();
    q.pop();
    if (tmp == _goal) {
      ans += dis[_goal.first][_goal.second];
      break;
    }
    int x = tmp.first;
    int y = tmp.second;
    for (int i = 0; i < 4; i++) {
      if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
          y + vecy[i] >= w)
        continue;
      if (filed[x + vecx[i]][y + vecy[i]] != 'X' &&
          !dis[x + vecx[i]][y + vecy[i]]) {
        dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
        q.push(std::make_pair(x + vecx[i], y + vecy[i]));
      }
    }
  }
}

int main() {
  std::cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> filed[i][j];
      if (filed[i][j] == 'S') {
        goal[0].first = i;
        goal[0].second = j;
      }
      if (filed[i][j] > '0' && filed[i][j] <= '9') {
        goal[filed[i][j] - '0'].first = i;
        goal[filed[i][j] - '0'].second = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    bfs(goal[i + 1], goal[i]);
    for (int j = 0; j < h; j++)
      for (int k = 0; k < w; k++)
        dis[j][k] = 0;
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

const int vecx[4] = {1, 0, -1, 0};
const int vecy[4] = {0, -1, 0, 1};
int h, w, n;
T goal[10];
char filed[1000][1000];
int si, sj;
int ans;
int dis[1000][1000];

void bfs(T _goal, T _start) {
  std::queue<T> q;
  q.push(_start);
  while (q.size()) {
    T tmp = q.front();
    q.pop();
    if (tmp == _goal) {
      ans += dis[_goal.first][_goal.second];
      break;
    }
    int x = tmp.first;
    int y = tmp.second;
    for (int i = 0; i < 4; i++) {
      if (x + vecx[i] < 0 || x + vecx[i] >= h || y + vecy[i] < 0 ||
          y + vecy[i] >= w)
        continue;
      if (filed[x + vecx[i]][y + vecy[i]] != 'X') {
        dis[x + vecx[i]][y + vecy[i]] = dis[x][y] + 1;
        q.push(std::make_pair(x + vecx[i], y + vecy[i]));
      }
    }
  }
}

int main() {
  std::cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      std::cin >> filed[i][j];
      if (filed[i][j] == 'S') {
        goal[0].first = i;
        goal[0].second = j;
      }
      if (filed[i][j] > '0' && filed[i][j] <= '9') {
        goal[filed[i][j] - '0'].first = i;
        goal[filed[i][j] - '0'].second = j;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    bfs(goal[i + 1], goal[i]);
    for (int j = 0; j < h; j++)
      for (int k = 0; k < w; k++)
        dis[j][k] = 0;
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct mouse {
  int y, x, dis;
  mouse() { y = x = dis = 0; }
};

char stage[1000][1000];
int ng[1000][1000] = {};
int main() {
  int h, w, n;
  mouse s;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    cin >> stage[i];
    for (int j = 0; j < w; j++) {
      if (stage[i][j] == 'S') {
        s.y = i;
        s.x = j;
      } else if (stage[i][j] != 'X' && stage[i][j] != '.') {
        stage[i][j] -= '0';
      }
    }
  }

  int ny, nx;
  int situ_y[4] = {-1, 0, 1, 0}, situ_x[4] = {0, 1, 0, -1};
  for (int target = 1; target <= n; target++) {
    queue<mouse> mickey;
    mouse m_c;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        ng[i][j] = 0;
    mickey.push(s);
    while (1) {
      m_c = mickey.front();
      if (stage[m_c.y][m_c.x] == target) {
        s = m_c;
        break;
      }
      if ((m_c.dis != 0 && ng[m_c.y][m_c.x] >= 4) ||
          stage[m_c.y][m_c.x] == 'X') {
        mickey.pop();
        continue;
      }
      ng[m_c.y][m_c.x]++;
      for (int i = 0; i < 4; i++) {
        m_c = mickey.front();
        ny = m_c.y + situ_y[i];
        nx = m_c.x + situ_x[i];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        m_c.y = ny;
        m_c.x = nx;
        m_c.dis++;
        mickey.push(m_c);
      }
      mickey.pop();
    }
  }
  printf("%d\\n", s.dis);
  return 0;
}

Error: TLE

Fixed submission:

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

struct mouse {
  int y, x, dis;
};

char stage[1000][1000];
int ng[1000][1000] = {};
int main() {
  int h, w, n;
  mouse s;
  s.dis = 0;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    cin >> stage[i];
    for (int j = 0; j < w; j++) {
      if (stage[i][j] == 'S') {
        s.y = i;
        s.x = j;
      } else if (stage[i][j] != 'X' && stage[i][j] != '.') {
        stage[i][j] -= '0';
      }
    }
  }

  int ny, nx;
  int situ_y[4] = {-1, 0, 1, 0}, situ_x[4] = {0, 1, 0, -1};
  for (int target = 1; target <= n; target++) {
    queue<mouse> mickey;
    mouse m_c;
    mickey.push(s);
    while (1) {
      m_c = mickey.front();
      if (stage[m_c.y][m_c.x] == target) {
        s.y = m_c.y;
        s.x = m_c.x;
        s.dis = m_c.dis;
        break;
      }
      if ((m_c.dis != 0 && ng[m_c.y][m_c.x] == m_c.dis) ||
          stage[m_c.y][m_c.x] == 'X') {
        mickey.pop();
        continue;
      }
      ng[m_c.y][m_c.x] = m_c.dis;
      for (int i = 0; i < 4; i++) {
        m_c = mickey.front();
        ny = m_c.y + situ_y[i];
        nx = m_c.x + situ_x[i];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        m_c.y = ny;
        m_c.x = nx;
        m_c.dis++;
        mickey.push(m_c);
      }
      mickey.pop();
    }
  }
  printf("%d\\n", s.dis);
  return 0;
}