Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef struct {
  int x;
  int y;
} P; // 表示坐?

char graph[1000][1000];
P aim[10];
P s, g; // s起点，g?点
int N, W, H;
int vis[1000][1000]; // 用来存?距?
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

int bfs(P s, P g);

int main() {
  int i, j, t, step = 0;
  cin >> H >> W >> N;

  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++) {
      cin >> graph[i][j];
      if (graph[i][j] <= '9' && graph[i][j] >= '1') {
        t = graph[i][j] - '0';
        aim[t].x = i;
        aim[t].y = j;
      } // 把1-N的坐?保存在 aim数?中
      else if (graph[i][j] == 'S') {
        s.x = i;
        s.y = j;
        graph[i][j] = '.';
      } // 得到最初的起点
    }

  for (i = 1; i <= N; i++) {
    g.x = aim[i].x;
    g.y = aim[i].y; // 1-N ，从小到大，?次把一个 作??点
    step += bfs(s, g);
    s.x = g.x;
    s.y = g.y; // 到??个?点后??点?起点
    graph[g.x][g.y] = '.';
  }

  cout << step << endl;
}

int bfs(P s, P g) {
  queue<P> que;
  P n;

  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      vis[i][j] = -1;

  que.push(s);
  vis[s.x][s.y] = 0;

  while (que.size()) {

    P p = que.front();
    que.pop();
    if (p.x == g.x && p.y == g.y)
      break;

    for (int i = 0; i < 4; i++) {
      n.x = p.x + dx[i];
      n.y = p.y + dy[i]; // 向四方漫射
      if (0 <= n.x && n.x < H && 0 <= n.y && n.y < W &&
          graph[n.x][n.y] != 'X' && vis[n.x][n.y] == -1) {
        que.push(n);
        vis[n.x][n.y] = vis[p.x][p.y] + 1;
      }
    }
  }
  return vis[g.x][g.y];
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <stdlib.h>
#define MAX_NUM 1010
using namespace std;
int H, W, N;
int flag[MAX_NUM][MAX_NUM] = {0};

class Node {
public:
  Node();
  Node(int x, int y);
  ~Node();
  int x;
  int y;
};

Node::Node() {}

Node::Node(int x, int y) {
  this->x = x;
  this->y = y;
}

Node::~Node() {}
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int BFS(char map[][MAX_NUM], int sx, int sy, int ex, int ey) {
  queue<Node *> que;
  que.push(new Node(sx, sy));
  flag[sx][sy] = 0; // 将改位置???已遍?
  while (!que.empty()) {
    Node *cur = que.front();
    que.pop();
    if (cur->x == ex && cur->y == ey) {
      return flag[cur->x][cur->y];
    } else {
      for (int i = 0; i < 4; i++) // 将?位置的四个方向可以
      {
        int k = cur->x + dir[i][0];
        int v = cur->y + dir[i][1];
        if (k >= 0 && k < H && v >= 0 && v < W && map[k][v] != 'X' &&
            !flag[k][v]) {
          que.push(new Node(k, v));
          flag[k][v] = flag[cur->x][cur->y] + 1;
        }
      }
    }
  }
  return -1;
}

int main() {
  int sx, sy, arr[10][2];
  char map[MAX_NUM][MAX_NUM];
  while (cin >> H >> W >> N && H && W) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> map[i][j];
        if (map[i][j] == 'S') {
          map[i][j] = '.';
          sx = i;
          sy = j;
        }
        if (isdigit(map[i][j])) {
          int index = map[i][j] - '0';
          arr[index][0] = i;
          arr[index][1] = j;
        }
      }
    }
    memset(flag, 0, sizeof(flag));
    int res = 0;
    res += BFS(map, sx, sy, arr[1][0], arr[1][1]);
    for (int i = 1; i + 1 <= N; i++) {
      memset(flag, 0, sizeof(flag));
      res += BFS(map, arr[i][0], arr[i][1], arr[i + 1][0], arr[i + 1][1]);
    }
    cout << res << endl;
  }
  return 0;
}