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

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;
char maze[1005][1005];
int d[1005][1005];
int dirx[4] = {0, -1, 0, 1};
int diry[4] = {1, 0, -1, 0};
int h, w, n, hard;
int si, sj;
const int INF = 1 << 30;
int tot = 0;

void BFS(int ai, int aj) {
  queue<P> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = INF;
    }
  }
  que.push(P(ai, aj));
  d[ai][aj] = 0;
  while (!que.empty()) {
    P a = que.front();
    que.pop();
    int x = a.first, y = a.second;
    if (maze[x][y] - '0' == hard) {
      tot += d[x][y];
      si = x;
      sj = y;
      break;
    }
    for (int i = 0; i < 4; i++) {
      int dx = x + dirx[i], dy = y + diry[i];
      if (dx >= 0 && dx < h && dy >= 0 && dy < w && maze[dx][dy] != 'X' &&
          d[dx][dy] == INF) {
        d[dx][dy] = d[x][y] + 1;
        que.push(P(dx, dy));
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  while (scanf("%d%d%d", &h, &w, &n) != EOF) {
    getchar();
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%c", &maze[i][j]);
        if (maze[i][j] == 'S') {
          si = i;
          sj = j;
        }
      }
      getchar();
    }
    tot = 0;
    hard = 1;
    for (int i = 0; i < n; i++) {
      BFS(si, sj);
      hard++;
    }
    cout << tot << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const double eps = 1e-7;
int dirX[4] = {1, 0, -1, 0};
int dirY[4] = {0, 1, 0, -1};
struct point {
  int x;
  int y;
  int depth;
  point() { x = y = depth = 0; }
  point(int X, int Y, int D) {
    x = X;
    y = Y;
    depth = D;
  }
  point(const point &p) {
    x = p.x;
    y = p.y;
    depth = p.depth;
  }
  bool operator==(const point &C) { return {x == C.x && y == C.y}; }
};
struct cheese : public point {
  int hard;
  cheese() { x = y = hard = 0; }
  cheese(int X, int Y, int H) {
    x = X;
    y = Y;
    hard = H;
  }
  bool operator<(const cheese &C) { return hard < C.hard; }
};
int main() {
  // ifstream ifs;
  // ifs.open("D:\\\\documents\\\\programDatas\\\\AOJ0558.txt");
  int H, W, N;
  cin >> H >> W >> N;
  bool a[1001][1001];
  bool v[1001][1001]; // here???
  memset(a, 0, sizeof(a));
  memset(v, 0, sizeof(v));
  vector<cheese> goal;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char temp;
      cin >> temp;
      if (temp == 'S')
        goal.push_back(cheese(i, j, 0));
      else if (temp >= '1' && temp <= '9')
        goal.push_back(cheese(i, j, (int)(temp - 48)));
      else if (temp == '.')
        a[i][j] = 0;
      else if (temp == 'X')
        a[i][j] = 1;
    }
  }
  sort(goal.begin(), goal.end());
  // for debug
  /*for (int i = 1; i <= H; i++) {
          for (int j = 1; j <= W; j++) {
                  cout << setw(3) << a[i][j];
          }
          cout << endl;
  }
  for (int i = 0; i <= N; i++) {
          cout << goal[i].x << " " << goal[i].y << " " << goal[i].hard << " " <<
  endl;
  }
  cout << endl;
  */
  // BFS
  int total = 0;                // total steps
  for (int t = 0; t < N; t++) { // x for start point and x+1 for end point
    deque<point> que;
    que.push_back(point(goal[t].x, goal[t].y, 0));
    // for (int s = t + 2; s <= N; s++) {//mark harder cheese as visited !!
    //	v[goal[s].x][goal[s].y] = 1;
    // }
    while (!que.empty()) {

      point curP = que.front();
      que.pop_front();
      v[curP.x][curP.y] = 1; // as visited
      if (curP == goal[t + 1]) {
        total = total + curP.depth;
        memset(v, 0, sizeof(v));
        break;
      } else {
        for (int i = 0; i < 4; i++) {
          int tx = curP.x + dirX[i];
          int ty = curP.y + dirY[i];
          if (tx >= 1 && tx <= H && ty >= 1 && ty <= W && a[tx][ty] == 0 &&
              v[tx][ty] == 0)
            que.push_back(point(tx, ty, curP.depth + 1));
        }
      }
    }
  }
  cout << total << endl;
  return 0;
}