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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct state {
  int s;
  int x;
  int mark;
  int dis;
  state(int s, int x, int mark, int dis) : s(s), x(x), mark(mark), dis(dis) {}
};
// const bool operator<(const state &a, const state &b) {
//   return a.s!=b.s?a.s<b.s:a.x!=b.x?a.x<b.x:a.mark<b.mark;
// }

int dx[2] = {-1, 1};
bool visited[1 << 16][16][16];

int main() {
  int n;
  while (cin >> n, n) {
    string ss, tt;
    cin >> ss >> tt;
    int s = 0, t = 0;
    REP(i, n) {
      if (ss[i] == 'W')
        s |= 1 << i;
      if (tt[i] == 'W')
        t |= 1 << i;
    }

    queue<state> Q;
    REP(i, n) Q.push(state(s, i, -1, 0));
    int res;
    memset(visited, 0, sizeof(visited));
    while (!Q.empty()) {
      state now = Q.front();
      Q.pop();

      if (visited[now.s][now.x][now.mark])
        continue;
      visited[now.s][now.x][now.mark] = 1;

      // cout << now.s << " " << now.x << " " << now.mark << " " << now.dis <<
      // endl;
      if (now.s == t) {
        res = now.dis - 1;
        break;
      }

      int nd = now.dis + 1;
      REP(i, 2) {
        int nx = now.x + dx[i];
        if (nx < 0 || nx >= n)
          continue;

        if (now.mark == -1) {
          Q.push(state(now.s, nx, now.x, nd));
          Q.push(state(now.s, nx, -1, nd));
        } else {
          Q.push(state(now.s, nx, now.mark, nd));
          int tmp = now.s;
          if (now.s >> now.mark & 1) {
            tmp |= 1 << now.x;
          } else {
            if (now.s >> now.x & 1)
              tmp ^= 1 << now.x;
          }
          if (now.s >> now.x & 1) {
            tmp |= 1 << now.mark;
          } else {
            if (now.s >> now.mark & 1)
              tmp ^= 1 << now.mark;
          }
          Q.push(state(tmp, nx, -1, nd));
        }
      }
    }
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct state {
  string s;
  int x;
  int mark;
  int dis;
  state(string s, int x, int mark, int dis)
      : s(s), x(x), mark(mark), dis(dis) {}
};
const bool operator<(const state &a, const state &b) {
  return a.s != b.s ? a.s < b.s : a.x != b.x ? a.x < b.x : a.mark < b.mark;
}

int dx[2] = {-1, 1};

int main() {
  int n;
  while (cin >> n, n) {
    string s, t;
    cin >> s >> t;
    queue<state> Q;
    REP(i, n) Q.push(state(s, i, -1, 0));
    map<state, bool> visited;
    int res;
    while (!Q.empty()) {
      state now = Q.front();
      Q.pop();

      if (visited[now])
        continue;
      visited[now] = 1;

      // cout << now.s << " " << now.x << " " << now.mark << " " << now.dis <<
      // endl;
      if (now.s == t) {
        res = now.dis - 1;
        break;
      }

      int nd = now.dis + 1;
      REP(i, 2) {
        int nx = now.x + dx[i];
        if (nx < 0 || nx >= n)
          continue;

        if (now.mark == -1) {
          Q.push(state(now.s, nx, now.x, nd));
          Q.push(state(now.s, nx, -1, nd));
        } else {
          Q.push(state(now.s, nx, now.mark, nd));
          string tmp = now.s;
          swap(tmp[now.mark], tmp[now.x]);
          Q.push(state(tmp, nx, -1, nd));
        }
      }
    }
    cout << res << endl;
  }
}