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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
};
int n;
int w, h;
vector<P> o[2700];
// int dp[11111][11111];
int dp[2700][2700];
// int **dp;
// vector<int> dp[11111];

int solve(int m, int j) {
  if (m > n)
    return 0;
  if (dp[m][j]) {
    return dp[m][j];
  }

  int ret = 1 << 22;
  for (int i = 0; i < (int)o[m].size(); i++) {
    ret = min(ret, solve(m + 1, i) + abs(o[m][i].x - o[m - 1][j].x) +
                       abs(o[m][i].y - o[m - 1][j].y));
  }
  return dp[m][j] = ret;
}

int main(void) {
  while (1) {
    scanf("%d%d", &w, &h);
    if (!w)
      break;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < 2700; i++) {
      o[i].clear(); /*dp[i].clear();*/
    };

    n = 0;
    int sx, sy;
    int gx, gy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char s[256];
        scanf("%s", s);
        if ('0' <= s[0] && s[0] <= '9') {
          int num = atoi(s);
          // printf("%d\\n",num);
          o[num].push_back(P(j, i));
          // dp[num].push_back(0);
          n = max(n, num);
          continue;
        }
        if (s[0] == 'S') {
          sx = j;
          sy = i;
        } else if (s[0] == 'G') {
          gx = j;
          gy = i;
        }
      }
    }
    // printf("s:%d,%d\\ng:%d,%d\\n",sx,sy,gx,gy);
    n++;
    o[n].push_back(P(gx, gy));
    // dp[n].push_back(0);
    o[0].push_back(P(sx, sy));
    // dp[0].push_back(0);
    /*
    dp = (int **)malloc(sizeof(int *) * (n + 1));
    for(int i = 0; i <= n; i++){
      dp[i] = (int *)calloc(o[i].size(),sizeof(int));
    }
    */
    printf("%d\\n", solve(1, 0));
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct P {
  int x, y;
  P(int x, int y) : x(x), y(y) {}
};
int n;
int w, h;
vector<P> o[3000];
// int dp[11111][11111];
int dp[3000][3000];
// int **dp;
// vector<int> dp[11111];

int solve(int m, int j) {
  if (m > n)
    return 0;
  if (dp[m][j]) {
    return dp[m][j];
  }

  int ret = 1 << 22;
  for (int i = 0; i < (int)o[m].size(); i++) {
    ret = min(ret, solve(m + 1, i) + abs(o[m][i].x - o[m - 1][j].x) +
                       abs(o[m][i].y - o[m - 1][j].y));
  }
  return dp[m][j] = ret;
}

int main(void) {
  while (1) {
    scanf("%d%d", &w, &h);
    if (!w)
      break;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < 3000; i++) {
      o[i].clear(); /*dp[i].clear();*/
    };

    n = 0;
    int sx, sy;
    int gx, gy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        char s[256];
        scanf("%s", s);
        if ('0' <= s[0] && s[0] <= '9') {
          int num = atoi(s);
          // printf("%d\\n",num);
          o[num].push_back(P(j, i));
          // dp[num].push_back(0);
          n = max(n, num);
          continue;
        }
        if (s[0] == 'S') {
          sx = j;
          sy = i;
        } else if (s[0] == 'G') {
          gx = j;
          gy = i;
        }
      }
    }
    // printf("s:%d,%d\\ng:%d,%d\\n",sx,sy,gx,gy);
    n++;
    o[n].push_back(P(gx, gy));
    // dp[n].push_back(0);
    o[0].push_back(P(sx, sy));
    // dp[0].push_back(0);
    /*
    dp = (int **)malloc(sizeof(int *) * (n + 1));
    for(int i = 0; i <= n; i++){
      dp[i] = (int *)calloc(o[i].size(),sizeof(int));
    }
    */
    printf("%d\\n", solve(1, 0));
  }
}