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;

//************************************
// Method:    bfs
// FullName:  bfs
// Access:    public
// Returns:   int
// Qualifier:
// Parameter: const int & sx 起点x
// Parameter: const int & sy 起点y
// Parameter: const int & gx 终点x
// Parameter: const int & gy 终点y
//************************************
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 <cstdio>
#include <iostream>
#include <queue>
using namespace std;

int h, w, n;
const int MAX_N = 1005;
char map[MAX_N][MAX_N];
struct point {
  int x, y;
  int step;
  point() {
    x = 0;
    y = 0;
    step = 0;
  }
  point(int xx, int yy, int ss) {
    x = xx;
    y = yy;
    step = ss;
  }
};
struct point p[16];

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

int ok(int x, int y) {
  if (x >= 0 && x < h && y >= 0 && y < w)
    return 1;
  return 0;
}

int bfs(int next) {
  int nextcheese = next + 1;

  queue<point> q;
  point cur(p[next].x, p[next].y, 0);
  q.push(cur);
  while (q.size()) {
    cur = q.front();
    q.pop();
    int x = cur.x, y = cur.y;
    int nx, ny;
    for (int i = 0; i < 4; i++) {
      nx = x + dx[i];
      ny = y + dy[i];
      if (ok(nx, ny)) {
        if ((map[nx][ny] - '0') == nextcheese)
          return ++cur.step;
        if (map[nx][ny] == '.') {
          q.push(point(nx, ny, cur.step + 1));
        }
      }
    }
  }
}

void solve() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> map[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char ch = map[i][j];
      switch (ch) {
      case '.':
        break;
      case 'X':
        break;
      case 'S':
        p[0].x = i;
        p[0].y = j;
        map[i][j] = '.';
        break;
      default:
        int t = (ch - '0');
        p[t].x = i;
        p[t].y = j;
      }
      /* if(map[i][j]=='S'){
           p[0].x=i; p[0].y = j;
           map[i][j]='.';
       }
       else if(isdigit(map[i][j])){
           int index=map[i][j]-'0';
           p[index].x=i; p[index].y=j;
       }*/
    }
  }
  int step = 0;
  for (int i = 0; i < n; i++) {
    step += bfs(i);
  }
  printf("%d\\n", step);
}

int main() { solve(); }