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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <queue>

using namespace std;

struct S {
  int p[3][2];
  int t;
};

int main() {
  for (int w, h, n; cin >> w >> h >> n, w;) {
    cin.ignore();
    char g[16][16];
    S is = {{}, 0};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin.get(g[i][j]);
        if (islower(g[i][j])) {
          auto x = g[i][j] - 'a';
          is.p[x][0] = i;
          is.p[x][1] = j;
        }
      }
      cin.ignore();
    }
    queue<S> que;
    que.push(is);
    static bool p[1 << 24];
    fill(begin(p), end(p), false);
    for (;;) {
      S cs = que.front();
      bool f = false;
      for (int i = 0; i < n; i++) {
        f |= g[cs.p[i][0]][cs.p[i][1]] != 'A' + i;
      }
      if (!f)
        break;
      que.pop();
      // int b=0;
      // for(int i=0;i<n;i++){
      // 	b=b<<8|cs.p[i][0]<<4|cs.p[i][1];
      // }
      // if(!p.insert(b).second)continue;
      static int dy[] = {-1, 0, 0, 0, 1};
      static int dx[] = {0, -1, 0, 1, 0};
      for (int i = 0; i < 5; i++) {
        int y1, x1, y2, x2, y3, x3;
        auto enqueue = [&]() {
          S ns{{y1, x1, y2, x2, y3, x3}, cs.t + 1};
          int b = 0;
          for (int i = 0; i < n; i++) {
            b = b << 8 | ns.p[i][0] << 4 | ns.p[i][1];
          }
          if (!p[b]++) {
            que.push(ns);
          }
        };
        y1 = cs.p[0][0] + dy[i];
        x1 = cs.p[0][1] + dx[i];
        y2 = cs.p[1][0];
        x2 = cs.p[1][1];
        y3 = cs.p[2][0];
        x3 = cs.p[2][1];
        if (g[y1][x1] == '#')
          continue;
        if (n == 1) {
          enqueue();
        } else {
          for (int j = 0; j < 5; j++) {
            y2 = cs.p[1][0] + dy[j];
            x2 = cs.p[1][1] + dx[j];
            if (g[y2][x2] == '#' || y2 == y1 && x2 == x1 ||
                y1 == cs.p[1][0] && x1 == cs.p[1][1] && y2 == cs.p[0][0] &&
                    x2 == cs.p[0][1])
              continue;
            if (n == 2) {
              enqueue();
            } else {
              for (int k = 0; k < 5; k++) {
                y3 = cs.p[2][0] + dy[k];
                x3 = cs.p[2][1] + dx[k];
                if (g[y3][x3] == '#' || y3 == y1 && x3 == x1 ||
                    y3 == y2 && x3 == x2 ||
                    y3 == cs.p[0][0] && x3 == cs.p[0][1] && y1 == cs.p[2][0] &&
                        x1 == cs.p[2][1] ||
                    y3 == cs.p[1][0] && x3 == cs.p[1][1] && y2 == cs.p[2][0] &&
                        x2 == cs.p[2][1])
                  continue;
                enqueue();
              }
            }
          }
        }
      }
    }
    cout << que.front().t << endl;
  }
}


Error: MLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

struct S {
  int p[3][2];
  int t;
};

int main() {
  for (int w, h, n; cin >> w >> h >> n, w;) {
    cin.ignore();
    char g[16][16];
    S is = {{}, 0};
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin.get(g[i][j]);
        if (islower(g[i][j])) {
          auto x = g[i][j] - 'a';
          is.p[x][0] = i;
          is.p[x][1] = j;
        }
      }
      cin.ignore();
    }
    queue<S> que;
    que.push(is);
    set<int> p;
    for (;;) {
      S cs = que.front();
      bool f = false;
      for (int i = 0; i < n; i++) {
        f |= g[cs.p[i][0]][cs.p[i][1]] != 'A' + i;
      }
      if (!f)
        break;
      que.pop();
      // int b=0;
      // for(int i=0;i<n;i++){
      // 	b=b<<8|cs.p[i][0]<<4|cs.p[i][1];
      // }
      // if(!p.insert(b).second)continue;
      static int dy[] = {-1, 0, 0, 0, 1};
      static int dx[] = {0, -1, 0, 1, 0};
      for (int i = 0; i < 5; i++) {
        int y1, x1, y2, x2, y3, x3;
        auto enqueue = [&]() {
          S ns{{y1, x1, y2, x2, y3, x3}, cs.t + 1};
          int b = 0;
          for (int i = 0; i < n; i++) {
            b = b << 8 | ns.p[i][0] << 4 | ns.p[i][1];
          }
          if (p.insert(b).second) {
            que.push(ns);
          }
        };
        y1 = cs.p[0][0] + dy[i];
        x1 = cs.p[0][1] + dx[i];
        y2 = cs.p[1][0];
        x2 = cs.p[1][1];
        y3 = cs.p[2][0];
        x3 = cs.p[2][1];
        if (g[y1][x1] == '#')
          continue;
        if (n == 1) {
          enqueue();
        } else {
          for (int j = 0; j < 5; j++) {
            y2 = cs.p[1][0] + dy[j];
            x2 = cs.p[1][1] + dx[j];
            if (g[y2][x2] == '#' || y2 == y1 && x2 == x1 ||
                y1 == cs.p[1][0] && x1 == cs.p[1][1] && y2 == cs.p[0][0] &&
                    x2 == cs.p[0][1])
              continue;
            if (n == 2) {
              enqueue();
            } else {
              for (int k = 0; k < 5; k++) {
                y3 = cs.p[2][0] + dy[k];
                x3 = cs.p[2][1] + dx[k];
                if (g[y3][x3] == '#' || y3 == y1 && x3 == x1 ||
                    y3 == y2 && x3 == x2 ||
                    y3 == cs.p[0][0] && x3 == cs.p[0][1] && y1 == cs.p[2][0] &&
                        x1 == cs.p[2][1] ||
                    y3 == cs.p[1][0] && x3 == cs.p[1][1] && y2 == cs.p[2][0] &&
                        x2 == cs.p[2][1])
                  continue;
                enqueue();
              }
            }
          }
        }
      }
    }
    cout << que.front().t << endl;
  }
}