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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
typedef pair<int, int> P;
int h, w, qx, qy, ax, ay;
string s[30];
short int dp[30][30][30][30][2]; // 1->Qwin,-1->Awin.0->?
// qx,qy,ax,ay,turn=0->queen
int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1};
vector<P> exits;
bool is(int x, int y) {
  if (x < 0 || h <= x || y < 0 || w <= y || s[x][y] == '#')
    return false;
  return true;
}
void init() {
  exits.clear();
  rep(i, h) rep(j, w) rep(k, h) rep(l, w) rep(turn, 2) dp[i][j][k][l][turn] = 0;
}
void showdp(int i, int j, int k, int l, int turn, int value) {
  printf("dp[%d][%d][%d][%d][%d]=%d\\n", i, j, k, l, turn, value);
}
int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    init();
    rep(i, h) cin >> s[i];
    rep(i, h) rep(j, w) {
      if (s[i][j] == 'Q')
        qx = i, qy = j;
      if (s[i][j] == 'A')
        ax = i, ay = j;
      if (s[i][j] == 'E')
        exits.pb(P(i, j));
    }
    bool update = true;
    for (P ex : exits) {
      int x = ex.fs, y = ex.sc;
      rep(i, h) rep(j, w) dp[x][y][i][j][0] = 1;
    }
    rep(i, h) rep(j, w) rep(turn, 2) dp[i][j][i][j][turn] = -1;
    while (update) {
      update = false;
      // turn=0
      rep(i, h) rep(j, w) rep(k, h) rep(l, w) {
        if (dp[i][j][k][l][0] != 0)
          continue;
        bool allcant = true, canwin = false;
        rep(di, 5) {
          int nx = i + dx[di], ny = j + dy[di];
          if (!is(nx, ny))
            continue;
          if (dp[nx][ny][k][l][1] == 1)
            canwin = true;
          if (dp[nx][ny][k][l][1] != -1)
            allcant = false;
        }
        if (canwin) {
          dp[i][j][k][l][0] = 1;
          //					showdp(i,j,k,l,0,1);
          update = true;
          continue;
        }
        if (allcant) {
          dp[i][j][k][l][0] = -1;
          //					showdp(i,j,k,l,0,-1);
          update = true;
          continue;
        }
      }
      // turn=1
      rep(i, h) rep(j, w) rep(k, h) rep(l, w) {
        if (dp[i][j][k][l][1] != 0)
          continue;
        bool allcant = true, canwin = false;
        rep(di, 5) {
          int nx = k + dx[di], ny = l + dy[di];
          if (!is(nx, ny))
            continue;
          if (dp[i][j][nx][ny][0] == -1)
            canwin = true;
          if (dp[i][j][nx][ny][0] != 1)
            allcant = false;
        }
        if (canwin) {
          dp[i][j][k][l][1] = -1;
          //					showdp(i,j,k,l,1,-1);
          update = true;
          continue;
        }
        if (allcant) {
          dp[i][j][k][l][1] = 1;
          //					showdp(i,j,k,l,1,1);
          update = true;
          continue;
        }
      }
    }
    //		rep(i,h) rep(j,w) rep(k,h) rep(l,w) rep(turn,2)
    //showdp(i,j,k,l,turn,dp[i][j][k][l][turn]);
    switch (dp[qx][qy][ax][ay][0]) {
    case -1:
      puts("Army can catch Queen.");
      break;
    case 0:
      puts("Queen can not escape and Army can not catch Queen.");
      break;
    case 1:
      puts("Queen can escape.");
      break;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
typedef pair<int, int> P;
int h, w, qx, qy, ax, ay;
string s[30];
short int dp[100][100][100][100][2]; // 1->Qwin,-1->Awin.0->?
// qx,qy,ax,ay,turn=0->queen
int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1};
vector<P> exits;
bool is(int x, int y) {
  if (x < 0 || h <= x || y < 0 || w <= y || s[x][y] == '#')
    return false;
  return true;
}
void init() {
  exits.clear();
  rep(i, h) rep(j, w) rep(k, h) rep(l, w) rep(turn, 2) dp[i][j][k][l][turn] = 0;
}
void showdp(int i, int j, int k, int l, int turn, int value) {
  printf("dp[%d][%d][%d][%d][%d]=%d\\n", i, j, k, l, turn, value);
}
int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    init();
    rep(i, h) cin >> s[i];
    rep(i, h) rep(j, w) {
      if (s[i][j] == 'Q')
        qx = i, qy = j;
      if (s[i][j] == 'A')
        ax = i, ay = j;
      if (s[i][j] == 'E')
        exits.pb(P(i, j));
    }
    bool update = true;
    for (P ex : exits) {
      int x = ex.fs, y = ex.sc;
      rep(i, h) rep(j, w) dp[x][y][i][j][0] = 1;
    }
    rep(i, h) rep(j, w) rep(turn, 2) dp[i][j][i][j][turn] = -1;
    while (update) {
      update = false;
      // turn=0
      rep(i, h) rep(j, w) rep(k, h) rep(l, w) {
        if (dp[i][j][k][l][0] != 0)
          continue;
        bool allcant = true, canwin = false;
        rep(di, 5) {
          int nx = i + dx[di], ny = j + dy[di];
          if (!is(nx, ny))
            continue;
          if (dp[nx][ny][k][l][1] == 1)
            canwin = true;
          if (dp[nx][ny][k][l][1] != -1)
            allcant = false;
        }
        if (canwin) {
          dp[i][j][k][l][0] = 1;
          //					showdp(i,j,k,l,0,1);
          update = true;
          continue;
        }
        if (allcant) {
          dp[i][j][k][l][0] = -1;
          //					showdp(i,j,k,l,0,-1);
          update = true;
          continue;
        }
      }
      // turn=1
      rep(i, h) rep(j, w) rep(k, h) rep(l, w) {
        if (dp[i][j][k][l][1] != 0)
          continue;
        bool allcant = true, canwin = false;
        rep(di, 5) {
          int nx = k + dx[di], ny = l + dy[di];
          if (!is(nx, ny))
            continue;
          if (dp[i][j][nx][ny][0] == -1)
            canwin = true;
          if (dp[i][j][nx][ny][0] != 1)
            allcant = false;
        }
        if (canwin) {
          dp[i][j][k][l][1] = -1;
          //					showdp(i,j,k,l,1,-1);
          update = true;
          continue;
        }
        if (allcant) {
          dp[i][j][k][l][1] = 1;
          //					showdp(i,j,k,l,1,1);
          update = true;
          continue;
        }
      }
    }
    //		rep(i,h) rep(j,w) rep(k,h) rep(l,w) rep(turn,2)
    //showdp(i,j,k,l,turn,dp[i][j][k][l][turn]);
    switch (dp[qx][qy][ax][ay][0]) {
    case -1:
      puts("Army can catch Queen.");
      break;
    case 0:
      puts("Queen can not escape and Army can not catch Queen.");
      break;
    case 1:
      puts("Queen can escape.");
      break;
    }
  }
}