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

#include "bits/stdc++.h"
using namespace std;

typedef vector<vector<int>> Graph;

vector<int> solve(const Graph &g) {
  vector<int> nums(g.size());
  map<int, vector<int>> revg;
  for (int i = 0; i < g.size(); ++i) {
    auto es(g[i]);
    nums[i] = es.size();
    for (auto e : es) {
      revg[e].push_back(i);
    }
  }

  // 1 : win   0 : draw   -1 :loss
  vector<int> anss(g.size());
  queue<int> que;
  for (int i = 0; i < g.size(); ++i) {
    if (!nums[i]) {
      que.push(i);
      anss[i] = -1;
    }
  }
  while (!que.empty()) {
    int now(que.front());
    que.pop();
    if (anss[now] == 1) {
      for (auto reve : revg[now]) {
        nums[reve]--;
        if (!nums[reve] && anss[reve] == 0) {
          anss[reve] = -1;
          que.push(reve);
        }
      }
    } else if (anss[now] == -1) {
      if (revg.find(now) != revg.end()) {
        for (auto reve : revg[now]) {
          if (anss[reve] == 0) {
            anss[reve] = 1;
            que.push(reve);
          }
        }
      }

    } else {
      assert(false);
    }
  }
  return anss;
}
int SIZE = 30;
int gethash(int ax, int ay, int bx, int by, int c) {
  return (ax - 1) * SIZE * SIZE * SIZE * 2 + (ay - 1) * SIZE * SIZE * 2 +
         (bx - 1) * SIZE * 2 + (by - 1) * 2 + c + 1;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (!W)
      break;
    vector<vector<int>> field(H + 2, vector<int>(W + 2, -1));
    int sqx, sqy, sax, say;
    for (int i = 0; i < H; ++i) {
      string st;
      cin >> st;
      for (int j = 0; j < W; ++j) {
        switch (st[j]) {
        case 'Q':
          field[i + 1][j + 1] = 0;
          sqx = j + 1;
          sqy = i + 1;
          break;
        case 'A':
          field[i + 1][j + 1] = 0;
          sax = j + 1;
          say = i + 1;
          break;
        case '#':
          break;
        case '.':
          field[i + 1][j + 1] = 0;
          break;
        case 'E':
          field[i + 1][j + 1] = 2;
          break;
        default:
          assert(false);
        }
      }
    }
    SIZE = max(H, W);
    Graph g(SIZE * SIZE * SIZE * SIZE * 2 + 1);
    int dx[] = {-1, 0, 1, 0, 0};
    int dy[] = {0, 1, 0, -1, 0};
    for (int qx = 1; qx < W + 1; ++qx) {
      for (int qy = 1; qy < H + 1; ++qy) {
        if (field[qy][qx] == -1)
          continue;
        for (int ax = 1; ax < W + 1; ++ax) {
          for (int ay = 1; ay < H + 1; ++ay) {
            if (field[ay][ax] == -1)
              continue;
            for (int nt = 0; nt < 2; ++nt) {
              int nowhash = gethash(qx, qy, ax, ay, nt);
              if (qx == ax && qy == ay) {
                if (nt) {
                  continue;
                } else {
                  g[nowhash].push_back(0);
                }
              }
              if (field[qy][qx] == 2 && nt) {
                g[nowhash].push_back(0);
              }
              for (int way = 0; way < 5; ++way) {
                int nqx(qx), nqy(qy), nax(ax), nay(ay);
                if (nt) {
                  nqx += dx[way];
                  nqy += dy[way];
                  if (field[nqy][nqx] == -1)
                    continue;
                } else {
                  nax += dx[way];
                  nay += dy[way];
                  if ((field[nay][nax] == -1))
                    continue;
                }
                if (nax == nqx && nay == nqy) {
                  if (nt) {
                    continue;
                  } else {
                    g[nowhash].push_back(0);
                  }
                }
                int nexthash = gethash(nqx, nqy, nax, nay, !nt);
                g[nowhash].push_back(nexthash);
              }
            }
          }
        }
      }
    }
    auto anss = solve(g);
    int ans = anss[gethash(sqx, sqy, sax, say, 1)];
    if (ans == 0)
      cout << "Queen can not escape and Army can not catch Queen." << endl;
    else if (ans == 1)
      cout << "Queen can escape." << endl;
    else
      cout << "Army can catch Queen." << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

typedef vector<vector<int>> Graph;

vector<int> solve(const Graph &g) {
  vector<int> nums(g.size());
  map<int, vector<int>> revg;
  for (int i = 0; i < g.size(); ++i) {
    auto es(g[i]);
    nums[i] = es.size();
    for (auto e : es) {
      revg[e].push_back(i);
    }
  }

  // 1 : win   0 : draw   -1 :loss
  vector<int> anss(g.size());
  queue<int> que;
  for (int i = 0; i < g.size(); ++i) {
    if (!nums[i]) {
      que.push(i);
      anss[i] = -1;
    }
  }
  while (!que.empty()) {
    int now(que.front());
    que.pop();
    if (anss[now] == 1) {
      for (auto reve : revg[now]) {
        nums[reve]--;
        if (!nums[reve] && anss[reve] == 0) {
          anss[reve] = -1;
          que.push(reve);
        }
      }
    } else if (anss[now] == -1) {
      for (auto reve : revg[now]) {
        if (anss[reve] == 0) {
          anss[reve] = 1;
          que.push(reve);
        }
      }
    } else {
      assert(false);
    }
  }
  return anss;
}
int SIZE = 30;
int gethash(int ax, int ay, int bx, int by, int c) {
  return (ax - 1) * SIZE * SIZE * SIZE * 2 + (ay - 1) * SIZE * SIZE * 2 +
         (bx - 1) * SIZE * 2 + (by - 1) * 2 + c + 1;
}

int main() {
  while (true) {
    int W, H;
    cin >> W >> H;
    if (!W)
      break;
    vector<vector<int>> field(H + 2, vector<int>(W + 2, -1));
    int sqx, sqy, sax, say;
    for (int i = 0; i < H; ++i) {
      string st;
      cin >> st;
      for (int j = 0; j < W; ++j) {
        switch (st[j]) {
        case 'Q':
          field[i + 1][j + 1] = 0;
          sqx = j + 1;
          sqy = i + 1;
          break;
        case 'A':
          field[i + 1][j + 1] = 0;
          sax = j + 1;
          say = i + 1;
          break;
        case '#':
          break;
        case '.':
          field[i + 1][j + 1] = 0;
          break;
        case 'E':
          field[i + 1][j + 1] = 2;
          break;
        default:
          assert(false);
        }
      }
    }
    SIZE = max(H, W);
    Graph g(SIZE * SIZE * SIZE * SIZE * 2 + 1);
    int dx[] = {-1, 0, 1, 0, 0};
    int dy[] = {0, 1, 0, -1, 0};
    for (int qx = 1; qx < W + 1; ++qx) {
      for (int qy = 1; qy < H + 1; ++qy) {
        if (field[qy][qx] == -1)
          continue;
        for (int ax = 1; ax < W + 1; ++ax) {
          for (int ay = 1; ay < H + 1; ++ay) {
            if (field[ay][ax] == -1)
              continue;
            for (int nt = 0; nt < 2; ++nt) {
              int nowhash = gethash(qx, qy, ax, ay, nt);
              if (qx == ax && qy == ay) {
                if (nt) {
                  continue;
                } else {
                  g[nowhash].push_back(0);
                }
              }
              if (field[qy][qx] == 2 && nt) {
                g[nowhash].push_back(0);
              }
              for (int way = 0; way < 5; ++way) {
                int nqx(qx), nqy(qy), nax(ax), nay(ay);
                if (nt) {
                  nqx += dx[way];
                  nqy += dy[way];
                  if (field[nqy][nqx] == -1)
                    continue;
                } else {
                  nax += dx[way];
                  nay += dy[way];
                  if ((field[nay][nax] == -1))
                    continue;
                }
                if (nax == nqx && nay == nqy) {
                  if (nt) {
                    continue;
                  } else {
                    g[nowhash].push_back(0);
                  }
                }
                int nexthash = gethash(nqx, nqy, nax, nay, !nt);
                g[nowhash].push_back(nexthash);
              }
            }
          }
        }
      }
    }
    auto anss = solve(g);
    int ans = anss[gethash(sqx, sqy, sax, say, 1)];
    if (ans == 0)
      cout << "Queen can not escape and Army can not catch Queen." << endl;
    else if (ans == 1)
      cout << "Queen can escape." << endl;
    else
      cout << "Army can catch Queen." << endl;
  }

  return 0;
}