Problem p01164 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string A, B;
int n, Ab, Bb;

int Bit(int bit, int i, int x) {
  while ((bit >> i & 1) != x)
    bit ^= 1 << i;
  return bit;
}

int mem[16][1 << 16], used[16][1 << 16];
int dep = 0;
int dfs(short pos, int bit) {
  cout << dep++ << endl;
  if (bit == Bb)
    return 0;
  if (used[pos][bit]++)
    return mem[pos][bit];

  int &res = mem[pos][bit] = 1e9;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int nbit = bit;
      nbit = Bit(nbit, i, bit >> j & 1);
      nbit = Bit(nbit, j, bit >> i & 1);
      res = min(res, abs(pos - i) + abs(i - j) + dfs(j, nbit));
    }
  cout << dep << endl;
  return mem[pos][bit];
}

typedef pair<int, int> P;
typedef pair<int, P> PP;
int bfs() {
  for (int j = 0; j < 16; j++)
    for (int i = 0; i < (1 << 16); i++)
      mem[j][i] = 1e9;

  priority_queue<PP, vector<PP>, greater<PP>> Q;
  for (int i = 0; i < n; i++)
    Q.push(PP(0, P(i, Ab))), mem[i][Ab] = 0;
  while (!Q.empty()) {
    PP t = Q.top();
    Q.pop();
    int cost = t.first;
    int pos = t.second.first;
    int bit = t.second.second;
    if (bit == Bb)
      return cost;
    if (mem[pos][bit] < cost)
      continue;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        int nbit = bit;
        nbit = Bit(nbit, i, bit >> j & 1);
        nbit = Bit(nbit, j, bit >> i & 1);
        int ncost = cost + abs(pos - i) + abs(i - j);
        if (ncost >= mem[j][nbit])
          continue;
        Q.push(PP(ncost, P(j, nbit)));
        mem[j][nbit] = ncost;
      }
  }
  assert(0);
}

int main() {

  while (cin >> n, n) {
    cin >> A >> B;

    Ab = Bb = 0;
    for (int i = 0; i < n; i++)
      Ab |= (A[i] == 'W') << i, Bb |= (B[i] == 'W') << i;

    memset(used, 0, sizeof(used));
    int ans = 1e9;
    // for(int i=0;i<n;i++) ans=min(ans,dfs(i,Ab));
    // cout<<ans<<endl;
    cout << bfs() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string A, B;
int n, Ab, Bb;

/*int dfs(int pos,int bit){
  if(bit ==Bb) return 0;
  if(used[pos][bit]++) return mem[pos][bit];

  int l,r;
  for(int i=0;i<n;i++)   if(digit(Bb,i)!=digit(bit,i)) l = i,i=n;
  for(int i=l+1;i<n;i++) if(digit(Bb,i)!=digit(bit,i) && digit(bit,l) !=
digit(bit,i)) r = i,i=n;
  //cout<<pos<<" "<<l<<" "<<r<<endl;
  int nbit = bit ^ (1<<l) ^ (1<<r);
  int res = 1e9;
  res = min(res,(r-l)+abs(r-pos)+dfs(l,nbit));
  res = min(res,(r-l)+abs(l-pos)+dfs(r,nbit));
  return mem[pos][bit]=res;
}
*/

bool digit(int bit, int i) { return bit >> i & 1; }
int Bit(int bit, int i, int x) {
  while ((bit >> i & 1) != x)
    bit ^= 1 << i;
  return bit;
}

int mem[16][1 << 16], used[16][1 << 16];
int dfs(int pos, int bit) {
  if (bit == Bb)
    return 0;
  if (used[pos][bit]++)
    return mem[pos][bit];

  int res = mem[pos][bit] = 1e9;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {

      int nbit = bit;
      nbit = Bit(nbit, i, bit >> j & 1);
      nbit = Bit(nbit, j, bit >> i & 1);
      res = min(res, abs(pos - i) + abs(i - j) + dfs(j, nbit));
    }

  return mem[pos][bit] = res;
}

int main() {

  while (cin >> n, n) {
    cin >> A >> B;

    Ab = Bb = 0;
    for (int i = 0; i < n; i++)
      Ab |= (A[i] == 'W') << i, Bb |= (B[i] == 'W') << i;

    memset(used, 0, sizeof(used));
    int ans = 1e9;
    for (int i = 0; i < n; i++)
      ans = min(ans, dfs(i, Ab));
    cout << ans << endl;
  }
  return 0;
}