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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
unsigned char dp[65536][16];
struct st {
  unsigned short haichi;
  unsigned char pos;
};
queue<st> P;
int main() {
  int n;
  while (cin >> n, n) {
    string s, ans_s;
    cin >> s;
    cin >> ans_s;
    unsigned short sh = 0;
    unsigned short ans_sh = 0;
    REP(i, n) {
      sh <<= 1;
      ans_sh <<= 1;
      if (s[i] == 'W')
        sh |= 1;
      if (ans_s[i] == 'W')
        ans_sh |= 1;
    }
    REP(i, 65536 * 16)
    dp[i / 16][i % 16] = 255;

    REP(i, n) {
      P.push({sh, (unsigned char)i});
      dp[sh][i] = 0;
    }

    while (!P.empty()) {
      st p = P.front();
      P.pop();
      unsigned short diff = p.haichi ^ ans_sh;
      vector<unsigned char> db;
      vector<unsigned char> dw;
      REP(i, n) {
        if ((diff >> i) & 1)
          if (!((p.haichi >> i) & 1))
            db.push_back(i);
          else
            dw.push_back(i);
      }
      REP(i, db.size()) {
        REP(j, dw.size()) {
          unsigned short change = (1 << db[i]) + (1 << dw[j]);
          change ^= p.haichi;
          if (dp[change][db[i]] >
              dp[p.haichi][p.pos] + abs(p.pos - dw[j]) + abs(db[i] - dw[j])) {
            dp[change][db[i]] =
                dp[p.haichi][p.pos] + abs(p.pos - dw[j]) + abs(db[i] - dw[j]);
            P.push({change, db[i]});
          }
          if (dp[change][dw[j]] >
              dp[p.haichi][p.pos] + abs(p.pos - db[i]) + abs(db[i] - dw[j])) {
            dp[change][dw[j]] =
                dp[p.haichi][p.pos] + abs(p.pos - db[i]) + abs(db[i] - dw[j]);
            P.push({change, dw[j]});
          }
        }
      }
    }
    int min_ans = 255;
    REP(i, n) { min_ans = min((int)dp[ans_sh][i], min_ans); }
    cout << min_ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
unsigned char dp[65536][16];
struct st {
  short haichi;
  unsigned char pos;
};
queue<st> P;
int main() {
  int n;
  while (cin >> n, n) {
    string s, ans_s;
    cin >> s;
    cin >> ans_s;
    short sh = 0;
    short ans_sh = 0;
    REP(i, n) {
      sh <<= 1;
      ans_sh <<= 1;
      if (s[i] == 'W')
        sh |= 1;
      if (ans_s[i] == 'W')
        ans_sh |= 1;
    }
    REP(i, 65536 * 16)
    dp[i / 16][i % 16] = 255;

    REP(i, n) {
      P.push({sh, (unsigned char)i});
      dp[sh][i] = 0;
    }

    while (!P.empty()) {
      st p = P.front();
      P.pop();
      short diff = p.haichi ^ ans_sh;
      vector<unsigned char> db;
      vector<unsigned char> dw;
      REP(i, n) {
        if ((diff >> i) & 1)
          if (!((p.haichi >> i) & 1))
            db.push_back(i);
          else
            dw.push_back(i);
      }
      REP(i, db.size()) {
        REP(j, dw.size()) {
          short change = (1 << db[i]) + (1 << dw[j]);
          change ^= p.haichi;
          if (dp[change][db[i]] >
              dp[p.haichi][p.pos] + abs(p.pos - dw[j]) + abs(db[i] - dw[j])) {
            dp[change][db[i]] =
                dp[p.haichi][p.pos] + abs(p.pos - dw[j]) + abs(db[i] - dw[j]);
            P.push({change, db[i]});
          }
          if (dp[change][dw[j]] >
              dp[p.haichi][p.pos] + abs(p.pos - db[i]) + abs(db[i] - dw[j])) {
            dp[change][dw[j]] =
                dp[p.haichi][p.pos] + abs(p.pos - db[i]) + abs(db[i] - dw[j]);
            P.push({change, dw[j]});
          }
        }
      }
    }
    int min_ans = 255;
    REP(i, n) { min_ans = min((int)dp[ans_sh][i], min_ans); }
    cout << min_ans << endl;
  }
}