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

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

const int M = 1000000007;
int d[] = {0, 1, 0, -1, 0};
int dist[50][50][1 << 10][2];
int isup[50][50][1 << 10];

int main() {
  int w, h, s;
  cin >> w >> h;
  vector<string> m(h);
  int si, sj;
  for (int i = 0; i < h; ++i) {
    cin >> m[i];
    for (int j = 0; j < w; ++j) {
      if (m[i][j] == '%') {
        si = i;
        sj = j;
      }
    }
  }
  cin >> s;
  vector<vector<string>> ms(max(s, 1), vector<string>(h));
  for (int i = 0; i < s; ++i) {
    for (int j = 0; j < h; ++j) {
      cin >> ms[i][j];
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (m[i][j] == '#' || m[i][j] == '|')
        continue;

      for (int k = 0; k < (1 << s); ++k) {
        int up = m[i][j] == '^' || m[i][j] >= 'A' && m[i][j] <= 'Z';
        for (int l = 0; l < s; ++l)
          if (((k >> l) & 1) && ms[l][i][j] == '*')
            up ^= 1;

        isup[i][j][k] = up;
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int k = 0; k < (1 << s); ++k) {
        for (int l = 0; l < 2; ++l)
          dist[i][j][k][l] = M;
      }
    }
  }
  queue<int> qi, qj, qs, qh;
  dist[si][sj][0][0] = 0;
  qi.push(si);
  qj.push(sj);
  qs.push(0);
  qh.push(0);
  while (!qi.empty()) {
    int pi = qi.front(), pj = qj.front(), ps = qs.front(), ph = qh.front();
    qi.pop();
    qj.pop();
    qs.pop();
    qh.pop();
    int dis = dist[pi][pj][ps][ph];
    if (m[pi][pj] == '&') {
      cout << dis << "\\n";
      return 0;
    }
    if (m[pi][pj] == '|' && dist[pi][pj][ps][1 - ph] > dis + 1) {
      dist[pi][pj][ps][1 - ph] = dis + 1;
      qi.push(pi);
      qj.push(pj);
      qs.push(ps);
      qh.push(1 - ph);
    } else if (m[pi][pj] >= 'A' && m[pi][pj] <= 'Z' ||
               m[pi][pj] >= 'a' && m[pi][pj] <= 'z') {
      int sw = (m[pi][pj] | 32) - 'a';
      int ts = ps ^ (1 << sw);
      int th = (ms[sw][pi][pj] == '*' ? 1 - ph : ph);
      if (dist[pi][pj][ts][th] > dis + 1) {
        dist[pi][pj][ts][th] = dis + 1;
        qi.push(pi);
        qj.push(pj);
        qs.push(ts);
        qh.push(th);
      }
    }
    for (int i = 0; i < 4; ++i) {
      int ti = pi + d[i], tj = pj + d[i + 1];
      if (m[ti][tj] != '#' && (m[ti][tj] == '|' || ph == isup[ti][tj][ps]) &&
          dist[ti][tj][ps][ph] > dis + 1) {
        dist[ti][tj][ps][ph] = dis + 1;
        qi.push(ti);
        qj.push(tj);
        qs.push(ps);
        qh.push(ph);
      }
    }
  }
  cout << "-1\\n";
  return 0;
}

Error: MLE

Fixed submission:

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

const int M = 1000000007;
int d[] = {0, 1, 0, -1, 0};

int main() {
  int w, h, s;
  cin >> w >> h;
  vector<string> m(h);
  int si, sj;
  for (int i = 0; i < h; ++i) {
    cin >> m[i];
    for (int j = 0; j < w; ++j) {
      if (m[i][j] == '%') {
        si = i;
        sj = j;
      }
    }
  }
  cin >> s;
  vector<vector<string>> ms(max(s, 1), vector<string>(h));
  for (int i = 0; i < s; ++i) {
    for (int j = 0; j < h; ++j) {
      cin >> ms[i][j];
    }
  }
  vector<vector<vector<bool>>> isup(
      h, vector<vector<bool>>(w, vector<bool>(1 << s)));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (m[i][j] == '#' || m[i][j] == '|')
        continue;

      for (int k = 0; k < (1 << s); ++k) {
        int up = m[i][j] == '^' || m[i][j] >= 'A' && m[i][j] <= 'Z';
        for (int l = 0; l < s; ++l) {
          if (((k >> l) & 1) && ms[l][i][j] == '*')
            up ^= 1;
        }
        isup[i][j][k] = up;
      }
    }
  }

  queue<int> qi, qj, qs, qh;
  vector<vector<vector<vector<int>>>> dist(
      h, vector<vector<vector<int>>>(
             w, vector<vector<int>>(1 << s, vector<int>(2, M))));
  dist[si][sj][0][0] = 0;
  qi.push(si);
  qj.push(sj);
  qs.push(0);
  qh.push(0);
  while (!qi.empty()) {
    int pi = qi.front(), pj = qj.front(), ps = qs.front(), ph = qh.front();
    qi.pop();
    qj.pop();
    qs.pop();
    qh.pop();
    int dis = dist[pi][pj][ps][ph];
    if (m[pi][pj] == '&') {
      cout << dis << "\\n";
      return 0;
    }
    if (m[pi][pj] == '|' && dist[pi][pj][ps][1 - ph] > dis + 1) {
      dist[pi][pj][ps][1 - ph] = dis + 1;
      qi.push(pi);
      qj.push(pj);
      qs.push(ps);
      qh.push(1 - ph);
    } else if (m[pi][pj] >= 'A' && m[pi][pj] <= 'Z' ||
               m[pi][pj] >= 'a' && m[pi][pj] <= 'z') {
      int sw = (m[pi][pj] | 32) - 'a';
      int ts = ps ^ (1 << sw);
      int th = (ms[sw][pi][pj] == '*' ? 1 - ph : ph);
      if (dist[pi][pj][ts][th] > dis + 1) {
        dist[pi][pj][ts][th] = dis + 1;
        qi.push(pi);
        qj.push(pj);
        qs.push(ts);
        qh.push(th);
      }
    }
    for (int i = 0; i < 4; ++i) {
      int ti = pi + d[i], tj = pj + d[i + 1];
      if (m[ti][tj] != '#' && (m[ti][tj] == '|' || ph == isup[ti][tj][ps]) &&
          dist[ti][tj][ps][ph] > dis + 1) {
        dist[ti][tj][ps][ph] = dis + 1;
        qi.push(ti);
        qj.push(tj);
        qs.push(ps);
        qh.push(ph);
      }
    }
  }
  cout << "-1\\n";
  return 0;
}