Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <stdlib.h>
#include <utility>

using namespace std;

typedef struct pair {
  int x;
  int y;
} pair_t;

const int INF = 10000000;

int bfs();

int H, W, N;
char **field;
int **d;

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

int sx, sy;

int main() {
  cin >> H >> W >> N;
  field = (char **)malloc(sizeof(char *) * H);
  d = (int **)malloc(sizeof(int *) * H);

  for (int i = 0; i < H; i++) {
    char *row = (char *)malloc(sizeof(char) * W);
    int *dr = (int *)malloc(sizeof(int) * W);
    cin >> row;
    field[i] = row;
    d[i] = dr;
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {

      if (field[i][j] == 'S') {
        sx = j;
        sy = i;
        field[i][j] = '.';
      }
    }
  }

  int result = bfs();

  cout << result << endl;

  // for(int i=0; i<H; i++){
  //     free(field[i]);
  // }

  // free(field);

  return 0;
}

void init_d(int x, int y, int n) {

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }

  d[y][x] = n;
}

void clear(queue<pair_t> &q) {
  queue<pair_t> empty;
  swap(q, empty);
}

int bfs() {
  int cheeze = 1;

  queue<pair_t> que;

  pair_t first = {sx, sy};
  que.push(first);
  d[sy][sx] = 0;

  init_d(sx, sy, 0);

  while (que.size()) {
  NEXT:
    pair_t p = que.front();
    que.pop();

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

      if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] != 'X' &&
          d[ny][nx] == INF) {
        if (field[ny][nx] != '.') {
          if ((int)(field[ny][nx] - '0') == cheeze) {

            if (cheeze == N) {
              return d[p.y][p.x] + 1;
            }

            init_d(nx, ny, d[p.y][p.x] + 1);
            clear(que);

            pair_t n = {nx, ny};
            que.push(n);

            cheeze++;
            goto NEXT;
          }
        }

        pair_t n = {nx, ny};
        d[ny][nx] = d[p.y][p.x] + 1;
        que.push(n);
      }
    }
  }
  return -1;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <stdlib.h>
#include <utility>

using namespace std;

typedef struct pair {
  int x;
  int y;
} pair_t;

const int INF = 10000000;

int bfs();

int H, W, N;
char **field;
int **d;

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

int sx, sy;

int main() {
  cin >> H >> W >> N;
  field = (char **)malloc(sizeof(char *) * H);
  d = (int **)malloc(sizeof(int *) * H);

  for (int i = 0; i < H; i++) {
    char *row = (char *)malloc(sizeof(char) * W);
    int *dr = (int *)malloc(sizeof(int) * W);
    cin >> row;
    field[i] = row;
    d[i] = dr;
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {

      if (field[i][j] == 'S') {
        sx = j;
        sy = i;
        field[i][j] = '.';
      }
    }
  }

  int result = bfs();

  cout << result << endl;

  for (int i = 0; i < H; i++) {
    free(field[i]);
  }

  free(field);

  return 0;
}

void init_d(int x, int y, int n) {

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INF;
    }
  }

  d[y][x] = n;
}

void clear(queue<pair_t> &q) {
  queue<pair_t> empty;
  swap(q, empty);
}

int bfs() {
  int cheeze = 1;

  queue<pair_t> que;

  pair_t first = {sx, sy};
  que.push(first);
  d[sy][sx] = 0;

  init_d(sx, sy, 0);

  while (que.size()) {
  NEXT:
    pair_t p = que.front();
    que.pop();

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

      if (0 <= nx && nx < W && 0 <= ny && ny < H && field[ny][nx] != 'X' &&
          d[ny][nx] == INF) {
        if (field[ny][nx] != '.') {
          if ((int)(field[ny][nx] - '0') == cheeze) {

            if (cheeze == N) {
              return d[p.y][p.x] + 1;
            }

            init_d(nx, ny, d[p.y][p.x] + 1);
            clear(que);

            pair_t n = {nx, ny};
            que.push(n);

            cheeze++;
            goto NEXT;
          }
        }

        pair_t n = {nx, ny};
        d[ny][nx] = d[p.y][p.x] + 1;
        que.push(n);
      }
    }
  }
  return -1;
}