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

#include <iostream>

using namespace std;

const int MAX = 9;
const int HP = 7;
const int INF = (1 << 25);
int T[HP][MAX][MAX];
int M[MAX][MAX];
int W, H;
int sx, sy, gx, gy;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

void init() {

  for (int i = 0; i < HP; i++)
    for (int j = 0; j < MAX; j++)
      for (int k = 0; k < MAX; k++)
        T[i][j][k] = INF;
}

void input() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> M[i][j];
      if (M[i][j] == 2) {
        sx = i;
        sy = j;
      }
      if (M[i][j] == 3) {
        gx = i;
        gy = j;
      }
    }
}

void solve(int h, int x, int y, int n) {
  if (T[h][x][y] <= n)
    return;
  T[h][x][y] = n;
  if (M[x][y] == 3 || h == 1)
    return;

  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (0 <= nx && nx < H && 0 <= ny && ny < W && M[nx][ny] != 0) {
      if (M[nx][ny] == 4)
        solve(6, nx, ny, n + 1);
      else
        solve(h - 1, nx, ny, n + 1);
    }
  }
}

void getans() {
  int ans = INF;

  for (int i = 1; i < HP; i++)
    ans = min(ans, T[i][gx][gy]);

  if (ans == INF)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}

int main() {

  while (cin >> W >> H && W + H) {
    init();
    input();
    solve(6, sx, sy, 0);
    getans();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
//  PP(P(time,HP),P(y,x))
const int MAX = 8;
const int INF = (1 << 20);
int map[MAX][MAX];
int T[MAX][MAX][MAX];
//     HP    y   x
int w, h;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int solve(P s, P g) {

  T[6][s.first][s.second] = 0;

  priority_queue<PP, vector<PP>, greater<PP>> Q;

  Q.push(PP(P(0, 6), s));

  while (!Q.empty()) {
    PP pp = Q.top();
    Q.pop();

    int cost = pp.first.first;
    int HP = pp.first.second;
    int nowy = pp.second.first;
    int nowx = pp.second.second;

    if (T[HP][nowy][nowx] < cost)
      continue;

    for (int i = 0; i < 4; i++) {
      int nexx = nowx + dx[i];
      int nexy = nowy + dy[i];
      if (0 <= nexx && nexx < w && 0 <= nexy && nexy < h) {
        if (T[HP - 1][nexy][nexx] > cost + 1 && map[nexy][nexx] != 0) {

          if (HP == 1)
            continue;
          if (map[nexy][nexx] == 4) {
            // food
            T[6][nexy][nexx] = cost + 1;
            Q.push(PP(P(cost + 1, 6), P(nexy, nexx)));

          } else {
            // not food
            T[HP - 1][nexy][nexx] = cost + 1;
            Q.push(PP(P(cost + 1, HP - 1), P(nexy, nexx)));
          }
        }
      }
    }
  }

  int ans = INF;

  for (int i = 1; i < MAX; i++)
    ans = min(ans, T[i][g.first][g.second]);

  if (ans == INF)
    return -1;
  else
    return ans;
}

void init() {
  memset(map, 0, sizeof(map));

  for (int i = 0; i < MAX; i++)
    for (int j = 0; j < MAX; j++)
      for (int k = 0; k < MAX; k++)
        T[i][j][k] = INF;
}

int main() {
  while (cin >> w >> h && w + h) {
    init();

    P s, g;

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
        if (map[i][j] == 2)
          s = P(i, j);
        if (map[i][j] == 3)
          g = P(i, j);
      }

    cout << solve(s, g) << endl;
  }

  return 0;
}