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

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

typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 30;
char field[100][100];
int memo[100][100][2];

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

int main() {
  int w, h;
  while (cin >> w >> h && (w || h)) {
    fill(&memo[0][0][0], &memo[99][99][1] + 1, INF);
    priority_queue<PPP, vector<PPP>, greater<PPP>> que;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[j][i];
        if (field[j][i] == 'S') {
          field[j][i] = '0';
          memo[j][i][0] = memo[j][i][1] = 0;
          que.push(PPP(0, PP(0, P(j, i))));
          que.push(PPP(0, PP(1, P(j, i))));
        }
      }
    }

    bool ok = false;
    while (!que.empty()) {
      PPP ppp = que.top();
      que.pop();
      int dis = ppp.first;
      int foot = ppp.second.first;
      int next = foot ^ 1;
      int x = ppp.second.second.first;
      int y = ppp.second.second.second;
      if (dis > memo[x][y][foot])
        continue;
      if (field[x][y] == 'T') {
        cout << memo[x][y][foot] << endl;
        ok = true;
        break;
      }
      for (int i = 0; i < 9; i++) {
        int nx, ny;
        if (foot == 0)
          nx = x + dx[i];
        else
          nx = x - dx[i];
        ny = y + dy[i];
        if (0 <= nx && nx < w && 0 <= ny && ny < h && field[nx][ny] != 'X') {
          int cost;
          if (isdigit(field[nx][ny]))
            cost = field[nx][ny] - '0';
          else
            cost = 0;
          if (memo[nx][ny][next] > memo[x][y][foot] + cost) {
            //	    cout << x << " " << y  << " " << nx << " " << ny << endl;
            memo[nx][ny][next] = memo[x][y][foot] + cost;
            que.push(PPP(memo[nx][ny][next], PP(next, P(nx, ny))));
          }
        }
      }
    }
    if (!ok)
      cout << -1 << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
typedef vector<int> vec;
typedef vector<vec> mat;
typedef complex<double> Point;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;

bool operator<(const Point &a, const Point &b) {
  return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
}

typedef pair<P, P> P2;
typedef pair<int, P2> Ps;
int w, h;
int memo[71][41][71][41];
int dl[61][61];
int dr[61][61];
char field[61][61];

struct state {
  int xl, xr, yl, yr, dis;
  state(int dis, int xl, int yl, int xr, int yr) {
    this->xl = xl;
    this->dis = dis;
    this->xr = xr;
    this->yr = yr;
    this->yl = yl;
  }
};

bool operator<(const state &s1, const state &s2) { return s1.dis < s2.dis; };

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

int main() {
  while (cin >> w >> h && (w || h)) {
    bool ok = false;
    int xl, xr, yl, yr, xl2, xr2, yl2, yr2;
    int dis, cost;
    fill(&memo[0][0][0][0], &memo[70][40][70][40] + 1, INF);
    fill(&dl[0][0], &dl[60][60] + 1, INF);
    fill(&dr[0][0], &dr[60][60] + 1, INF);
    priority_queue<state, vector<state>> que;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> field[j][i];
        if (field[j][i] == 'S') {
          field[j][i] = '0';
          memo[j][i][j][i] = 0;
          dl[j][i] = dr[j][i] = 0;
          que.push(state(0, j, i, j, i));
        }
      }
    }
    while (!que.empty()) {
      state s = que.top();
      que.pop();
      xl = s.xl;
      yl = s.yl;
      xr = s.xr;
      yr = s.yr;
      //		cout << "l " << xl << " " << yl << endl;
      //		cout << "r " << xr << " "  << yr << endl << endl;

      dis = -s.dis;
      if (dis > memo[xl][yl][xr][yr])
        continue;
      if (field[xl][yl] == 'T' || field[xr][yr] == 'T') {
        cout << memo[xl][yl][xr][yr] << endl;
        ok = true;
        break;
      }
      for (int i = 0; i < 9; i++) {
        xr2 = xl + dx[i];
        yr2 = yl + dy[i];
        if (0 <= xr2 && xr2 < w && 0 <= yr2 && yr2 < h &&
            field[xr2][yr2] != 'X') {
          if (isdigit(field[xr2][yr2]))
            cost = field[xr2][yr2] - '0';
          else
            cost = 0;
          if (memo[xl][yl][xr2][yr2] > dis + cost) {
            memo[xl][yl][xr2][yr2] = dis + cost;
            //		dr[xr2][yr2] = min(dr[xr2][yr2],
            //memo[xl][yl][xr2][yr2]);
            que.push(state(-memo[xl][yl][xr2][yr2], xl, yl, xr2, yr2));
          }
        }
      }

      for (int i = 0; i < 9; i++) {
        xl2 = xr - dx[i];
        yl2 = yr + dy[i];
        if (0 <= xl2 && xl2 < w && 0 <= yl2 && yl2 < h &&
            field[xl2][yl2] != 'X') {
          if (isdigit(field[xl2][yl2]))
            cost = field[xl2][yl2] - '0';
          else
            cost = 0;
          if (memo[xl2][yl2][xr][yr] > dis + cost) {
            memo[xl2][yl2][xr][yr] = dis + cost;
            //			dl[xl2][yl2] = min(dl[xl2][yl2],
            //memo[xl2][yl2][xr][yr]);
            que.push(state(-memo[xl2][yl2][xr][yr], xl2, yl2, xr, yr));
          }
        }
      }
    }
    if (!ok)
      cout << -1 << endl;
  }
  return 0;
}