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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

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

bool memo[64][16][4][64][16][4];

vector<vector<bool>> mp;

struct aa {
  int fx;
  int fy;
  int fway;
  int tx;
  int ty;
  int tway;
};
int gx, gy;

vector<aa> astack;

bool cangoal;

int gway;

bool check(int fx, int fy, int fway, int tx, int ty, int tway) {

  if (memo[fx][fy][fway][tx][ty][tway])
    return false;
  memo[fx][fy][fway][tx][ty][tway] = true;

  // else{

  int nfy = fy + dy[fway];
  int nfx = fx + dx[fway];
  while (1) {

    if (mp[nfy][nfx]) {
      nfx -= dx[fway];
      nfy -= dy[fway];
      break;
    }
    nfy = nfy + dy[fway];
    nfx = nfx + dx[fway];
  }

  int nty = ty;
  int ntx = tx;
  while (1) {

    if (mp[nty][ntx]) {
      ntx -= dx[tway];
      nty -= dy[tway];
      break;
    }

    if (mp[nty + dy[tway + 1]][ntx + dx[tway + 1]]) {
      int afway = (fway + 3) % 4;
      int atway = (tway + 3) % 4;
      if (nfx == gx && nfy == gy && ntx == gx && nty == gy) {
        if (tway == (gway + 2) % 4)
          return true;
      }
      if (fx == gx && fy == gy) {
        cangoal = true;
      }
      if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
        astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
      }
    }

    if (mp[nty + dy[tway + 3]][ntx + dx[tway + 3]]) {
      int afway = (fway + 1) % 4;
      int atway = (tway + 1) % 4;
      if (nfx == gx && nfy == gy && ntx == gx && nty == gy) {
        if (tway == (gway + 2) % 4)
          return true;
      }
      if (fx == gx && fy == gy) {
        cangoal = true;
      }
      if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
        astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
      }
    }

    nty = nty + dy[tway];
    ntx = ntx + dx[tway];
  }
  if (mp[nty + dy[tway + 1]][ntx + dx[tway + 1]]) {
    int afway = (fway + 3) % 4;
    int atway = (tway + 3) % 4;
    if (nfx == gx && nfy == gy && ntx == gx && nty == gy) {
      if (tway == (gway + 2) % 4)
        return true;
    }
    if (fx == gx && fy == gy) {
      cangoal = true;
    }
    if (!memo[nfx][nfy][afway][ntx][nty][atway]) {

      astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
    }
  }

  if (mp[nty + dy[tway + 3]][ntx + dx[tway + 3]]) {
    int atway = (tway + 1) % 4;
    int afway = (fway + 1) % 4;
    if (nfx == gx && nfy == gy && ntx == gx && nty == gy) {
      if (tway == (gway + 2) % 4)
        return true;
    }
    if (fx == gx && fy == gy) {
      cangoal = true;
    }
    if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
      astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
    }
  }

  //}
  return false;
}

bool memo2[64][16][4];

bool check2(int fx, int fy, int fway) {
  if (memo2[fx][fy][fway])
    return false;
  memo2[fx][fy][fway] = true;

  if (fx == gx && fy == gy)
    return true;
  int nfy = fy + dy[fway];
  int nfx = fx + dx[fway];
  while (1) {

    if (mp[nfy][nfx]) {
      nfy -= dy[fway];
      nfx -= dx[fway];
      break;
    }
    nfy = nfy + dy[fway];
    nfx = nfx + dx[fway];
  }
  return (check2(nfx, nfy, (fway + 1) % 4) || check2(nfx, nfy, (fway + 3) % 4));
}

int main() {
  while (1) {
    cangoal = false;
    memset(memo, 0, sizeof(memo));
    memset(memo2, 0, sizeof(memo2));
    astack.clear();
    int W, H;
    cin >> W >> H;
    if (!W)
      break;
    vector<string> stmp;
    for (int i = 0; i < H; ++i) {
      string st;
      cin >> st;
      stmp.push_back(st);
    }
    mp.resize(H);
    for (int i = 0; i < H; ++i) {
      mp[i].resize(W);
    }

    int sx, sy;

    for (int i = 0; i < W; ++i) {
      for (int j = 0; j < H; ++j) {
        if (stmp[j][i] == '.') {
          mp[j][i] = 0;
        } else if (stmp[j][i] == '#') {
          mp[j][i] = 1;
        } else if (stmp[j][i] == 'K') {
          sx = i;
          sy = j;
          mp[j][i] = 0;
        } else {
          gx = i;
          gy = j;
          mp[j][i] = 0;
        }
      }
    }
    int sway;
    for (int i = 0; i < 4; ++i) {
      if (mp[sy + dy[i]][sx + dx[i]] == 0) {
        sway = i;
      }
      if (mp[gy + dy[i]][gx + dx[i]] == 0) {
        gway = i;
      }
    }
    bool perf = false;
    for (int i = 0; i < 4; ++i)
      astack.push_back(aa{sx, sy, sway, sx, sy, i});

    while (!astack.empty()) {
      aa atop(astack.back());
      astack.pop_back();
      if (check(atop.fx, atop.fy, atop.fway, atop.tx, atop.ty, atop.tway)) {
        perf = true;
        break;
      }
    }

    if (perf) {
      cout << "He can accomplish his mission." << endl;
    } else if (check2(sx, sy, sway)) {
      cout << "He cannot return to the kitchen." << endl;

    } else {
      cout << "He cannot bring tea to his master." << endl;
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

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

int memo[64][16][4][64][16][4];

vector<vector<bool>> mp;

struct aa {
  int fx;
  int fy;
  int fway;
  int tx;
  int ty;
  int tway;
};
int gx, gy;

vector<aa> astack;

bool cangoal;

int gway;

bool check(int fx, int fy, int fway, int tx, int ty, int tway) {

  if (memo[fx][fy][fway][tx][ty][tway])
    return false;
  memo[fx][fy][fway][tx][ty][tway] = 1;

  // else{

  int nfy = fy + dy[fway];
  int nfx = fx + dx[fway];
  while (1) {

    if (mp[nfy][nfx]) {
      nfx -= dx[fway];
      nfy -= dy[fway];
      break;
    }
    nfy = nfy + dy[fway];
    nfx = nfx + dx[fway];
  }

  int nty = ty;
  int ntx = tx;
  while (1) {

    if (mp[nty][ntx]) {
      ntx -= dx[tway];
      nty -= dy[tway];
      break;
    }

    if (mp[nty + dy[tway + 1]][ntx + dx[tway + 1]]) {
      int atway = (tway + 3) % 4;
      int afway = (fway + 3) % 4;
      astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
    }

    if (mp[nty + dy[tway + 3]][ntx + dx[tway + 3]]) {
      int atway = (tway + 1) % 4;
      int afway = (fway + 1) % 4;
      astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
    }

    nty = nty + dy[tway];
    ntx = ntx + dx[tway];
  }
  if (mp[nty + dy[tway + 1]][ntx + dx[tway + 1]]) {
    int atway = (tway + 3) % 4;
    int afway = (fway + 3) % 4;
    if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
      if (nfx == gx && nfy == gy && ntx == gx && nty == gy) {
        if (tway == (gway + 2) % 4)
          return true;
      } else if (fx == gx && fy == gy) {
        cangoal = true;
      }

      astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
    }
  }

  if (mp[nty + dy[tway + 3]][ntx + dx[tway + 3]]) {
    int atway = (tway + 1) % 4;
    int afway = (fway + 1) % 4;

    if (!memo[nfx][nfy][afway][ntx][nty][atway]) {
      if (nfx == gx && nfy == gy && ntx == gx && nty == gy) {
        if (tway == (gway + 2) % 4)
          return true;
      } else if (fx == gx && fy == gy) {
        cangoal = true;
      }
      astack.push_back(aa{nfx, nfy, afway, ntx, nty, atway});
    }
  }

  //}
  return false;
}

int main() {
  while (1) {
    memset(memo, 0, sizeof(memo));
    astack.clear();
    int W, H;
    cin >> W >> H;
    if (!W)
      break;
    vector<string> stmp;
    for (int i = 0; i < H; ++i) {
      string st;
      cin >> st;
      stmp.push_back(st);
    }
    mp.resize(H);
    for (int i = 0; i < H; ++i) {
      mp[i].resize(W);
    }

    int sx, sy;

    for (int i = 0; i < W; ++i) {
      for (int j = 0; j < H; ++j) {
        if (stmp[j][i] == '.') {
          mp[j][i] = 0;
        } else if (stmp[j][i] == '#') {
          mp[j][i] = 1;
        } else if (stmp[j][i] == 'K') {
          sx = i;
          sy = j;
          mp[j][i] = 0;
        } else {
          gx = i;
          gy = j;
          mp[j][i] = 0;
        }
      }
    }
    int sway;
    for (int i = 0; i < 4; ++i) {
      if (mp[sy + dy[i]][sx + dx[i]] == 0) {
        sway = i;
      }
      if (mp[gy + dy[i]][gx + dx[i]] == 0) {
        gway = i;
      }
    }
    bool perf = false;
    for (int i = 0; i < 4; ++i)
      astack.push_back(aa{sx, sy, sway, sx, sy, i});

    while (!astack.empty()) {
      aa atop(astack.back());
      astack.pop_back();
      if (check(atop.fx, atop.fy, atop.fway, atop.tx, atop.ty, atop.tway)) {
        perf = true;
        break;
      }
    }

    if (perf) {
      cout << "He can accomplish his mission." << endl;
    } else if (cangoal) {
      cout << "He cannot return to the kitchen." << endl;

    } else {
      cout << "He cannot bring tea to his master." << endl;
    }
  }

  return 0;
}