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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 1005

char JOI[MAX][MAX];
int h, w, n;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};

pair<int, int> goal[10];
bool check(int a, int b) { return (a >= 1 && a <= h && b >= 1 && b <= w); }

int step[MAX][MAX];
int bfs(pair<int, int> st, pair<int, int> en) {
  queue<pair<int, int>> q;
  q.push(st);
  memset(step, -1, sizeof(step));
  step[st.first][st.second] = 0;
  int stp = 0;
  while (!q.empty()) {
    pair<int, int> S = q.front();
    q.pop();
    int x = S.first;
    int y = S.second;
    stp = step[x][y];
    for (int i = 0; i < 4; i++) {
      int cx = x + dx[i];
      int cy = y + dy[i];
      if (check(cx, cy)) {
        if (JOI[cx][cy] != 'X' && step[cx][cy] == -1) {
          if (cx == en.first && cy == en.second)
            return stp + 1;
          q.push(make_pair(cx, cy));
          step[cx][cy] = stp + 1;
        }
      }
    }
  }
  return stp;
}
int main() {
  cin >> h >> w >> n;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> JOI[i][j];
      if (JOI[i][j] == 'S')
        goal[0] = make_pair(i, j);
      else if (JOI[i][j] >= '0' && JOI[i][j] <= '9') {
        int x = JOI[i][j] - '0';
        goal[x] = make_pair(i, j);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += bfs(goal[i], goal[i + 1]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 1005

char JOI[MAX][MAX];
int h, w, n;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};

pair<int, int> goal[10];
bool check(int a, int b) { return (a >= 1 && a <= h && b >= 1 && b <= w); }

int step[MAX][MAX];
int bfs(pair<int, int> st, pair<int, int> en) {
  queue<pair<int, int>> q;
  q.push(st);
  memset(step, -1, sizeof(step));
  step[st.first][st.second] = 0;
  int stp = 0;
  while (!q.empty()) {
    pair<int, int> S = q.front();
    q.pop();
    int x = S.first;
    int y = S.second;
    stp = step[x][y];
    for (int i = 0; i < 4; i++) {
      int cx = x + dx[i];
      int cy = y + dy[i];
      if (check(cx, cy)) {
        if (JOI[cx][cy] != 'X') {
          if (cx == en.first && cy == en.second)
            return stp + 1;
          q.push(make_pair(cx, cy));
          step[cx][cy] = stp + 1;
        }
      }
    }
  }
}
int main() {
  cin >> h >> w >> n;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cin >> JOI[i][j];
      if (JOI[i][j] == 'S')
        goal[0] = make_pair(i, j);
      else if (JOI[i][j] >= '0' && JOI[i][j] <= '9') {
        int x = JOI[i][j] - '0';
        goal[x] = make_pair(i, j);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += bfs(goal[i], goal[i + 1]);
  }
  cout << ans << endl;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

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

int h, w, n;
char map[1000][1000];
struct P {
  int t, i, j;
  P() {
    t = 0;
    i = 0;
    j = 0;
  };
  P(int time, int x, int y) {
    t = time;
    i = x;
    j = y;
  };
};
int cal(int a, int b) {
  char start = a == 0 ? 'S' : '0' + a;
  bool flags[1000][1000];
  int si, sj;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      flags[i][j] = false;
      if (map[i][j] == start) {
        si = i;
        sj = j;
      }
    }

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

  queue<P> q;
  q.push(P(0, si, sj));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.i < 0 || p.j < 0)
      continue;
    if (p.i >= h || p.j >= w)
      continue;

    if (flags[p.i][p.j])
      continue;
    flags[p.i][p.j] = true;

    if (map[p.i][p.j] == '0' + b)
      return p.t;
    if (map[p.i][p.j] == 'X')
      continue;

    p.t++;
    for (int d = 0; d < 4; d++) {
      int ni = p.i + dx[d], nj = p.j + dy[d];
      //      if( flags[ni][nj] ) continue;
      q.push(P(p.t, ni, nj));
    }
  }

  return -1;
}

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

  int time = 0, r;
  for (int i = 0; i < n; i++) {
    r = cal(i, i + 1);
    time += r;
    //    cout << i << " => " << (i+1) << "\\t | " << r << endl;
  }
  cout << time << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int h, w, n;
char map[1000][1000];
struct P {
  int t, i, j;
  P() {
    t = 0;
    i = 0;
    j = 0;
  };
  P(int time, int x, int y) {
    t = time;
    i = x;
    j = y;
  };
};
int cal(int a, int b) {
  char start = a == 0 ? 'S' : '0' + a;
  bool flags[1000][1000];
  int si, sj;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      flags[i][j] = false;
      if (map[i][j] == start) {
        si = i;
        sj = j;
      }
    }

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

  queue<P> q;
  q.push(P(0, si, sj));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    if (p.i < 0 || p.j < 0)
      continue;
    if (p.i >= h || p.j >= w)
      continue;

    //    if( flags[p.i][p.j] ) continue;
    flags[p.i][p.j] = true;

    if (map[p.i][p.j] == '0' + b)
      return p.t;
    if (map[p.i][p.j] == 'X')
      continue;

    p.t++;
    for (int d = 0; d < 4; d++) {
      int ni = p.i + dx[d], nj = p.j + dy[d];
      if (flags[ni][nj])
        continue;
      q.push(P(p.t, ni, nj));
    }
  }

  return -1;
}

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

  int time = 0, r;
  for (int i = 0; i < n; i++) {
    r = cal(i, i + 1);
    time += r;
    //    cout << i << " => " << (i+1) << "\\t | " << r << endl;
  }
  cout << time << endl;

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

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

using namespace std;

struct P {
  int s;
  int t;
  int c;
};

int ds[4] = {0, 1, 0, -1}, dt[4] = {1, 0, -1, 0};

int bfs(char ch[1001][1001], P st, P go, int w, int h) {
  bool fa[1001][1001];
  fill(&fa[0][0], &fa[1000][1000], true);
  P p, q;
  queue<P> que;
  que.push(st);
  int c = 0;
  while (!que.empty()) {
    p = que.front();
    que.pop();

    if (p.s == go.s && p.t == go.t) {
      c = p.c;
      break;
    }

    for (int i = 0; i < 4; i++) {
      q.s = p.s + ds[i];
      q.t = p.t + dt[i];
      if (q.s >= 0 && q.s < h && q.t >= 0 && q.t < w) {
        if (fa[q.s][q.t] && ch[q.s][q.t] != 'X') {
          fa[q.s][q.t] = false;
          q.c = p.c + 1;
          que.push(q);
        }
      }
    }
  }

  return c;
}

int main() {
  int w, h, n, c;
  char ch[1001][1001];
  P pa[10];

  while (cin >> h >> w >> n && h && w && n) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> ch[i][j];
        if (ch[i][j] == 'S') {
          pa[0].s = i;
          pa[0].t = j;
          pa[0].c = 0;
        } else if (ch[i][j] != '.' && ch[i][j] != 'X') {
          pa[(int)(ch[i][j] - '0')].s = i;
          pa[(int)(ch[i][j] - '0')].t = j;
          pa[(int)(ch[i][j] - '0')].c = 0;
        }
      }
    }
    c = 0;

    for (int i = 0; i < n; i++) {
      c += bfs(ch, pa[i], pa[i + 1], w, h);
    }

    cout << c << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;

struct P {
  int s;
  int t;
};

struct P2 {
  int s;
  int t;
  int c;
};

int ds[4] = {0, 1, 0, -1}, dt[4] = {1, 0, -1, 0};

int bfs(char ch[1001][1001], P st, P go, int w, int h) {
  bool fa[1001][1001];
  fill(&fa[0][0], &fa[1000][1000], true);
  P2 p, q;
  queue<P2> que;
  p.s = st.s;
  p.t = st.t;
  p.c = 0;
  que.push(p);
  int c = 0;
  while (!que.empty()) {
    p = que.front();
    que.pop();

    if (p.s == go.s && p.t == go.t) {
      c = p.c;
      break;
    }

    for (int i = 0; i < 4; i++) {
      q.s = p.s + ds[i];
      q.t = p.t + dt[i];
      if (q.s >= 0 && q.s < h && q.t >= 0 && q.t < w) {
        if (fa[q.s][q.t] && ch[q.s][q.t] != 'X') {
          fa[q.s][q.t] = false;
          q.c = p.c + 1;
          que.push(q);
        }
      }
    }
  }

  return c;
}

int main() {
  int w, h, n, c;
  char ch[1001][1001];
  P pa[10];

  while (cin >> h >> w >> n && h && w && n) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> ch[i][j];
        if (ch[i][j] == 'S') {
          pa[0].s = i;
          pa[0].t = j;
        } else if (ch[i][j] != '.' || ch[i][j] != 'X') {
          pa[(int)(ch[i][j] - '0')].s = i;
          pa[(int)(ch[i][j] - '0')].t = j;
        }
      }
    }
    c = 0;

    for (int i = 0; i < n; i++) {
      c += bfs(ch, pa[i], pa[i + 1], w, h);
    }

    cout << c << endl;
  }

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

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

int h, w, n;
char m[1010][1010];
int d[1010][1010];
bool f[1010][1010];

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

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9 + 9;
      f[i][j] = false;
    }
  }
}

int main() {
  int sx, sy;

  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> m[i][j];
      if (m[i][j] == 'S')
        sx = j, sy = i;
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    queue<pair<int, int>> q;
    q.push(make_pair(sy, sx));

    init();
    f[sy][sx] = true;
    d[sy][sx] = 0;
    while (!q.empty()) {
      pair<int, int> nv = q.front();
      q.pop();

      if (m[nv.first][nv.second] == '0' + i) {
        sy = nv.first;
        sx = nv.second;
        ans += d[sy][sx];
        while (!q.empty())
          q.pop();
        break;
      }

      for (int j = 0; j < 4; j++) {
        int ny = nv.first + dy[j];
        int nx = nv.second + dx[j];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        if (f[ny][nx])
          continue;
        if (m[ny][nx] == 'X')
          continue;
        d[ny][nx] = d[nv.first][nv.second] + 1;
        q.push(make_pair(ny, nx));
        f[ny][nx] = true;
      }
    }
  }

  cout << ans << endl;
}

Error: MLE

Fixed submission:

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

int h, w, n;
char m[1010][1010];
int d[1010][1010];
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

void init() {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9 + 9;
    }
  }
}

int main() {
  int sx, sy;

  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> m[i][j];
      if (m[i][j] == 'S')
        sx = j, sy = i;
    }
  }

  int ans = 0;
  for (int i = 1; i <= n; i++) {
    queue<pair<int, int>> q;
    q.push(make_pair(sy, sx));

    init();
    d[sy][sx] = 0;
    while (!q.empty()) {
      pair<int, int> nv = q.front();
      q.pop();

      if (m[nv.first][nv.second] == '0' + i) {
        sy = nv.first;
        sx = nv.second;
        ans += d[sy][sx];
        while (!q.empty())
          q.pop();
        break;
      }

      for (int j = 0; j < 4; j++) {
        int ny = nv.first + dy[j];
        int nx = nv.second + dx[j];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        if (d[ny][nx] < d[nv.first][nv.second])
          continue;
        if (m[ny][nx] == 'X')
          continue;
        d[ny][nx] = d[nv.first][nv.second] + 1;
        q.push(make_pair(ny, nx));
      }
    }
  }

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define inf 1000000
using namespace std;
int H, W, N, x0, y0;
char map[1005][1005];
int d[1005][1005];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
typedef pair<int, int> P;
struct position {
  int rx, ry, e;
} xy[9];
bool comp(const position a, const position b) { return a.e < b.e; }
int bfs(int sx, int sy, int gx, int gy) {
  int tx, ty;
  queue<P> q;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      d[i][j] = inf;
  }
  q.push(P(sx, sy));
  d[sx][sy] = 0;
  while (!q.empty()) {
    P p = q.front();
    if (p.first == gx && p.second == gy)
      return d[p.first][p.second];
    q.pop();
    for (int i = 0; i < 4; i++) {
      tx = p.first + dx[i];
      ty = p.second + dy[i];
      if (tx >= 0 && tx < H && ty >= 0 && ty < W && map[tx][ty] != 'X' &&
          d[tx][ty] == inf) {
        d[tx][ty] = d[p.first][p.second] + 1;
        q.push(P(tx, ty));
      }
    }
  }
}
int main(void) {
  while (scanf("%d%d%d", &H, &W, &N) == 3) {
    int x1[9], y1[9];
    for (int i = 0; i < H; i++)
      scanf("%s", &map[i]);
    int t = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (map[i][j] == 'S') {
          x0 = i;
          y0 = j;
        }
        if (map[i][j] - '0' > 0 && map[i][j] - '0' <= 9) {
          xy[t].rx = i;
          xy[t].ry = j;
          xy[t].e = map[i][j] - '0';
          t++;
        }
      }
    }

    sort(xy, xy + N, comp);
    int ans = bfs(x0, y0, xy[0].rx, xy[0].ry);
    for (int i = 0; i < N - 1; i++)
      ans += bfs(xy[i].rx, xy[i].ry, xy[i + 1].rx, xy[i + 1].ry);
    printf("%d\\n", ans);
  }
  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(char ed) {
  queue<node> que;
  que.push(start);
  vis[start.x][start.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 >= h || x < 0 || y >= w || 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", &h, &w, &n)) {
    for (int i = 0; i < h; i++) {
      scanf("%s", &ma[i]);
      for (int j = 0; j < w; j++)
        if (ma[i][j] == 'S') {
          start.x = i;
          start.y = j;
          start.time = 0;
        }
    }
    for (int i = 0; i < n; i++) {
      memset(vis, 0, sizeof(vis));
      bfs(i + '1');
    }

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

#include <bits/stdc++.h>

using namespace std;
struct node {
  int x, y, f;
};
char a[1010][1010];
int book[1010][1010];
int m, n, h, ans, head, tail;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void bfs(int x, int y, int step) {
  int k = 0;
  node s, e;
  s.x = x;
  s.y = y;
  s.f = 0;
  memset(book, 0, sizeof(book));
  book[x][y] = 1;
  queue<node> q;
  q.push(s);
  while (!q.empty()) {
    s = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      e.x = s.x + dir[i][0];
      e.y = s.y + dir[i][1];
      e.f = s.f + 1;
      if (a[e.x][e.y] == 'X' || e.x < 0 || e.y < 0 || e.x >= m || e.y >= n ||
          book[e.x][e.y])
        continue;
      if (a[e.x][e.y] == step + '0') {
        ans += e.f;
        k = 1;
        break;
      }
      book[e.x][e.y] = 1;
      q.push(e);
    }
    if (k)
      break;
  }
  if (k && step == h)
    return;
  bfs(e.x, e.y, step + 1);
}
int main() {
  while (~scanf("%d %d %d", &m, &n, &h)) {
    int i, j;
    ans = 0;
    for (i = 0; i < m; i++)
      scanf("%s", a[i]);
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        if (a[i][j] == 'S') {
          bfs(i, j, 1);
          break;
        }
    printf("%d\\n", ans);
  }
  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(char ed) {
  queue<node> que;
  que.push(start);
  vis[start.x][start.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 >= h || x < 0 || y >= w || 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", &h, &w, &n)) {
    for (int i = 0; i < h; i++) {
      scanf("%s", &ma[i]);
      for (int j = 0; j < w; j++)
        if (ma[i][j] == 'S') {
          start.x = i;
          start.y = j;
          start.time = 0;
        }
    }
    for (int i = 0; i < n; i++) {
      memset(vis, 0, sizeof(vis));
      bfs(i + '1');
    }

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
char field[1000][1000];
struct date {
  int x, y, cnt;
};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  date start;
  int ans = 0;
  for (int i = 0; i < h; i++) {
    cin >> field[i];
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        start.x = j, start.y = i, start.cnt = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    queue<date> Q;
    Q.push(start);
    bool flg[1000][1000] = {{0}};
    while (!Q.empty()) {
      date d = Q.front();
      Q.pop();
      if (field[d.y][d.x] == i + '0') {
        start.x = d.x, start.y = d.y, start.cnt = d.cnt;
        break;
      }
      if (flg[d.y][d.x])
        continue;
      flg[d.y][d.x] = 1;
      int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
      for (int j = 0; j < 4; j++) {
        int px = d.x + dx[j], py = d.y + dy[j];
        if (px < 0 || py < 0 || px >= w || py >= h)
          continue;
        if (field[py][px] == 'X')
          continue;
        date next = {px, py, d.cnt + 1};
        Q.push(next);
      }
    }
    ans = start.cnt;
  }

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
char field[1000][1000];
struct date {
  int x, y, cnt;
};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  date start;
  int ans = 0;
  for (int i = 0; i < h; i++) {
    cin >> field[i];
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        start.x = j, start.y = i, start.cnt = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    queue<date> Q;
    Q.push(start);
    while (!Q.empty()) {
      date d = Q.front();
      Q.pop();
      if (field[d.y][d.x] == i + '0') {
        start.x = d.x, start.y = d.y, start.cnt = d.cnt;
        break;
      }
      int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
      for (int j = 0; j < 4; j++) {
        int px = d.x + dx[j], py = d.y + dy[j];
        if (px < 0 || py < 0 || px >= w || py >= h)
          continue;
        if (field[py][px] == 'X')
          continue;
        date next = {px, py, d.cnt + 1};
        Q.push(next);
      }
    }
    ans = start.cnt;
  }

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

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

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

class Data {
public:
  int x, y, count;
  Data() {}
  Data(int x, int y, int count) : x(x), y(y), count(count) {}
};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  Data start;
  int ans = 0;
  for (int i = 0; i < h; i++) {
    cin >> field[i];
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        start = Data(j, i, 0);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    queue<Data> Q;
    Q.push(start);
    bool flg[1000][1000] = {{0}};
    while (!Q.empty()) {
      Data d = Q.front();
      Q.pop();
      if (field[d.y][d.x] == i + '0') {
        start = Data(d.x, d.y, d.count);
        break;
      }
      if (flg[d.y][d.x])
        continue;
      flg[d.y][d.x] = true;
      for (int j = 0; j < 4; j++) {
        int px = d.x + dx[j], py = d.y + dy[j];
        if (px < 0 || py < 0 || px >= w || py >= h)
          continue;
        if (field[py][px] == 'X')
          continue;
        Q.push(Data(px, py, d.count + 1));
      }
    }
    ans = start.count;
  }

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

Error: 0

Fixed submission:

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

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

class Data {
public:
  int x, y, count;
  Data() {}
  Data(int x, int y, int count) : x(x), y(y), count(count) {}
};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  Data start;
  int ans = 0;
  for (int i = 0; i < h; i++) {
    cin >> field[i];
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        start = Data(j, i, 0);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    queue<Data> Q;
    Q.push(start);
    // bool flg[1000][1000] = {{0}};
    while (!Q.empty()) {
      Data d = Q.front();
      Q.pop();
      if (field[d.y][d.x] == i + '0') {
        start = Data(d.x, d.y, d.count);
        break;
      }
      // if (flg[d.y][d.x]) continue;
      // flg[d.y][d.x] = true;
      for (int j = 0; j < 4; j++) {
        int px = d.x + dx[j], py = d.y + dy[j];
        if (px < 0 || py < 0 || px >= w || py >= h)
          continue;
        if (field[py][px] == 'X')
          continue;
        Q.push(Data(px, py, d.count + 1));
      }
    }
    ans = start.count;
  }

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