alexjercan/bugnet huggingface dataset:
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define MAX 15010
typedef pair<int, int> pii;
typedef pair<pii, int> P;

bool check(int a, int b, int c) {
  if (a >= b + c || b >= a + c || c >= a + b)
    return false;
  return true;
}

int main() {
  vector<int> prime;
  bool p[MAX];
  fill(p, p + MAX, true);
  p[0] = p[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (!p[i])
      continue;
    prime.push_back(i);
    for (int j = 2 * i; j < MAX; j += i) {
      p[j] = false;
    }
  }
  int T;
  while (cin >> T, T) {
    int cnt = 0;
    set<P> st;
    int size = prime.size();
    for (int i = 0; i < size; i++) {
      for (int j = i; j < size; j++) {
        int k = T - prime[i] - prime[j];
        if (k < 0 || k >= MAX || !p[k])
          continue;
        int a[3] = {prime[i], prime[j], k};
        sort(a, a + 3);
        if (st.count(P(pii(a[0], a[1]), a[2])))
          continue;
        st.insert(P(pii(a[0], a[1]), a[2]));
        if (check(a[0], a[1], a[2])) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 30010
typedef pair<int, int> pii;
typedef pair<pii, int> P;

bool check(int a, int b, int c) {
  if (a >= b + c || b >= a + c || c >= a + b)
    return false;
  return true;
}

int main() {
  vector<int> prime;
  bool p[MAX];
  fill(p, p + MAX, true);
  p[0] = p[1] = false;
  for (int i = 2; i < MAX; i++) {
    if (!p[i])
      continue;
    prime.push_back(i);
    for (int j = 2 * i; j < MAX; j += i) {
      p[j] = false;
    }
  }
  int T;
  while (cin >> T, T) {
    int cnt = 0;
    set<P> st;
    int size = prime.size();
    for (int i = 0; i < size; i++) {
      for (int j = i; j < size; j++) {
        int k = T - prime[i] - prime[j];
        if (k < 0 || !p[k])
          continue;
        int a[3] = {prime[i], prime[j], k};
        sort(a, a + 3);
        if (st.count(P(pii(a[0], a[1]), a[2])))
          continue;
        st.insert(P(pii(a[0], a[1]), a[2]));
        if (check(a[0], a[1], a[2])) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int MAX_N = 50000;

// p[i] := i 番目の素数
vector<int> p;
// is_p[i] := i が素数のとき true
bool is_p[MAX_N + 1] = {false, false};
// cnt_p[i] := i 以下の素数の数
int cnt_p[MAX_N + 1];

// エラトステネスのふるい
void sieve() {
  for (int i = 2; i <= MAX_N; i++)
    is_p[i] = true;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (is_p[i]) {
      p.push_back(i);
      for (int j = i + i; j <= MAX_N; j += i) {
        is_p[j] = false;
      }
    }
  }
  // 任意の i 以下の素数の個数を求める(i <= MAX_N)
  int cnt = 0;
  for (int i = 0; i <= MAX_N; i++) {
    if (is_p[i]) {
      cnt++;
    }
    cnt_p[i] = cnt;
  }
}

int main() {
  sieve();
  int T;
  vector<int> v(3);

  while (cin >> T, T) {
    int ans = 0;
    map<vector<int>, bool> m;

    for (int a = 2; a < T; a++) {
      if (!is_p[a])
        continue;
      for (int b = a; a + b < T && (T - a - b) < a + b; b++) {
        if (!is_p[b])
          continue;
        int c = T - a - b;
        if (!is_p[c] || a >= b + c || b >= a + c || c >= a + b)
          continue;

        v[0] = a;
        v[1] = b;
        v[2] = c;
        sort(v.begin(), v.end());
        if (!m.count(v)) {
          ans++;
          m[v] = true;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int MAX_N = 1000000;

// p[i] := i 番目の素数
vector<int> p;
// is_p[i] := i が素数のとき true
bool is_p[MAX_N + 1] = {false, false};
// cnt_p[i] := i 以下の素数の数
int cnt_p[MAX_N + 1];

// エラトステネスのふるい
void sieve() {
  for (int i = 2; i <= MAX_N; i++)
    is_p[i] = true;
  for (int i = 2; i * i <= MAX_N; i++) {
    if (is_p[i]) {
      p.push_back(i);
      for (int j = i + i; j <= MAX_N; j += i) {
        is_p[j] = false;
      }
    }
  }
  // 任意の i 以下の素数の個数を求める(i <= MAX_N)
  int cnt = 0;
  for (int i = 0; i <= MAX_N; i++) {
    if (is_p[i]) {
      cnt++;
    }
    cnt_p[i] = cnt;
  }
}

int main() {
  sieve();
  int T;
  vector<int> v(3);

  while (cin >> T, T) {
    int ans = 0;
    map<vector<int>, bool> m;

    for (int a = 2; a < T; a++) {
      if (!is_p[a])
        continue;
      for (int b = a; a + b < T; b++) {
        if (!is_p[b])
          continue;
        int c = T - a - b;
        if (!is_p[c] || a >= b + c || b >= a + c || c >= a + b)
          continue;

        v[0] = a;
        v[1] = b;
        v[2] = c;
        sort(v.begin(), v.end());
        if (!m.count(v)) {
          ans++;
          m[v] = true;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int T;
  const int M = 30000;
  bool nonprime[M + 1] = {};
  nonprime[0] = nonprime[1] = true;
  vector<int> pv;
  for (int i = 2; i <= M; i++) {
    if (nonprime[i])
      continue;
    pv.push_back(i);
    for (int j = i * i; j <= M; j += i) {
      nonprime[j] = true;
    }
  }
  int N = pv.size();
  while (cin >> T && T) {
    int ans = 0;
    for (int i = 0; i < N && pv[i] < T; i++) {
      for (int j = i; j < N && pv[i] + pv[j] < T; j++) {
        int a = pv[i], b = pv[j];
        int c = T - a - b;
        if (c < b || nonprime[c])
          continue;
        if (abs(a - b) < c && c < a + b)
          ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int T;
  const int M = 30000;
  bool nonprime[M + 1] = {};
  nonprime[0] = nonprime[1] = true;
  for (int i = 2; i <= M; i++) {
    if (nonprime[i])
      continue;
    for (int j = i * i; j <= M; j += i) {
      nonprime[j] = true;
    }
  }
  while (cin >> T && T) {
    int ans = 0;
    for (int a = 1; a < T; a++) {
      for (int b = a; a + b < T; b++) {
        int c = T - a - b;
        if (c < b)
          continue;
        if (!nonprime[a] && !nonprime[b] && !nonprime[c] && abs(a - b) < c &&
            c < a + b)
          ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  vector<int> prime;
  bool e[30001] = {0};
  rep(i, 30001) {
    if (isprime(i))
      prime.push_back(i), e[i] = 1;
    else
      e[i] = 0;
  }
  int M = prime.size();
  while (cin >> n, n) {
    int ret = 0;
    rep(i, M) {
      for (int j = i; j < M; j++) {
        if (prime[i] + prime[j] >= n)
          break;
        if (n - prime[i] - prime[j] < prime[j])
          break;
        if (e[n - prime[i] - prime[j]] &&
            prime[i] + prime[j] > n - prime[i] - prime[j])
          ret++;
      }
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
bool isprime(int n) {
  if (n < 2)
    return false;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0)
      return false;
  return true;
}
int main() {
  int n;
  vector<int> prime;
  rep(i, 30001) if (isprime(i)) prime.push_back(i);
  int M = prime.size();

  while (cin >> n, n) {
    int ret = 0;
    rep(i, M) {
      for (int j = i; j < M; j++) {
        if (prime[i] + prime[j] >= n)
          break;
        if (binary_search(prime.begin() + j, prime.end(),
                          n - prime[i] - prime[j]))
          if (prime[i] + prime[j] > n - prime[i] - prime[j])
            ret++;
      }
    }
    cout << ret << endl;
  }
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define MAX 30001
using namespace std;

void eratos(int n, bool prime[]) {
  for (int i = 0; i <= n; i++)
    prime[i] = false;
  for (int i = 3; i <= n; i += 2)
    prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= limit; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      prime[j] = false;
  }
}

int main(void) {
  bool prime[MAX + 1];
  int P[MAX];
  eratos(MAX, prime);
  int size = 0;
  for (int i = 2; i <= MAX; i++) {
    if (prime[i])
      P[size++] = i;
  }
  int T;

  while (1) {
    scanf("%d", &T);
    if (T == 0)
      break;
    int sum = 0;
    for (int i = 0; i < size; i++) {
      if (P[i] > T) {
        i = size;
        continue;
      }
      for (int j = i; j < size; j++) {
        if (P[i] + P[j] > T) {
          j = size;
          continue;
        }
        int th = T - (P[i] + P[j]);
        if (th >= P[j] && prime[th]) {
          if (th < P[i] + P[j])
            sum++;
        }
      }
    }
    printf("%d\\n", sum);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#define MAX 30001
using namespace std;

void eratos(int n, bool prime[]) {
  for (int i = 0; i <= n; i++)
    prime[i] = false;
  for (int i = 3; i <= n; i += 2)
    prime[i] = true;
  prime[2] = true;
  int limit = (int)sqrt((double)n) + 1;
  for (int i = 3; i <= limit; i += 2) {
    if (!prime[i])
      continue;
    for (int j = i + i; j <= n; j += i)
      prime[j] = false;
  }
}

int main(void) {
  bool prime[MAX + 1];
  int P[MAX];
  eratos(MAX, prime);
  int size = 0;
  for (int i = 2; i <= MAX; i++) {
    if (prime[i])
      P[size++] = i;
  }
  int T;

  while (1) {
    scanf("%d", &T);
    if (T == 0)
      break;
    int sum = 0;
    for (int i = 0; i < size; i++) {
      if (P[i] > T) {
        i = size;
        continue;
      }
      for (int j = i; j < size; j++) {
        if (P[i] + P[j] > T) {
          j = size;
          continue;
        }
        for (int k = j; k < size; k++) {
          if (P[i] + P[j] + P[k] == T && P[k] < P[i] + P[j]) {
            sum++;
          }
          if (P[i] + P[j] + P[k] > T) {
            k = size;
            continue;
          }
        }
      }
    }
    printf("%d\\n", sum);
  }
  return 0;
}
Problem p01163 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

const int MAX = 30000;
int main() {
  vector<int> pr;
  bool p[MAX + 1] = {false};
  for (int i = 2; i <= MAX; i++)
    p[i] = 1;

  for (int i = 2; i * i <= MAX; i++)
    if (p[i])
      for (int k = i + i; k <= MAX; k += i)
        p[k] = 0;

  for (int i = 2; i <= MAX; i++)
    if (p[i])
      pr.push_back(i);

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i < pr.size(); i++)
      for (int j = i; j < pr.size() && pr[i] + pr[j] <= N; j++) {
        int k = N - pr[i] - pr[j];
        if (!p[k])
          continue;
        if (pr[j] > k)
          continue;
        if (pr[i] + pr[j] > k)
          cnt++;
      }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

const int MAX = 30000;
int main() {
  vector<int> pr;
  bool p[MAX + 1] = {false};
  for (int i = 2; i <= MAX; i++)
    p[i] = 1;

  for (int i = 2; i * i <= MAX; i++)
    if (p[i])
      for (int k = i + i; k <= MAX; k += i)
        p[k] = 0;

  for (int i = 2; i <= MAX; i++)
    if (p[i])
      pr.push_back(i);

  int N;
  while (cin >> N, N) {
    int cnt = 0;
    for (int i = 0; i < pr.size(); i++)
      for (int j = i; j < pr.size() && pr[i] + pr[j] <= N; j++)
        for (int k = j; k < pr.size() && pr[i] + pr[j] + pr[k] <= N; k++)
          if (pr[i] + pr[j] + pr[k] == N && pr[i] + pr[j] > pr[k])
            cnt++;
    cout << cnt << endl;
  }
}
Problem p01164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n;
string a, b;

int memo[16][1 << 16];

int f(int pos, int bit) {
  if (~memo[pos][bit])
    return memo[pos][bit];

  if (bit == 0)
    return 0;
  int ans = 99999;
  for (int i = 0; i < n; i++) {
    if (bit >> i & 1) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (bit >> j & 1) {
          if (a[j] != a[i]) {
            int dist1 = abs(pos - i);
            int dist2 = abs(i - j);
            ans = min(ans, f(j, bit & ~((1 << i) | (1 << j))) + dist1 + dist2);
          }
        }
      }
    }
  }
  return memo[pos][bit] = ans;
}
int main() {
  while (cin >> n && n) {
    memset(memo, -1, sizeof(memo));
    cin >> a >> b;
    int bit = 0;
    for (int i = 0; i < n; i++)
      if (a[i] != b[i])
        bit |= (1 << i);
    int ans = 9999;
    for (int i = 0; i < n; i++)
      ans = min(f(i, bit), ans);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int n;
string a, b;

int f(int pos, int bit) {
  if (bit == 0)
    return 0;
  int ans = 99999;
  for (int i = 0; i < n; i++) {
    if (bit >> i & 1) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          continue;
        if (bit >> j & 1) {
          if (a[j] != a[i]) {
            int dist1 = abs(pos - i);
            int dist2 = abs(i - j);
            ans = min(ans, f(j, bit & ~((1 << i) | (1 << j))) + dist1 + dist2);
          }
        }
      }
    }
  }
  return ans;
}
int main() {
  while (cin >> n && n) {
    cin >> a >> b;
    int bit = 0;
    for (int i = 0; i < n; i++)
      if (a[i] != b[i])
        bit |= (1 << i);
    int ans = 9999;
    for (int i = 0; i < n; i++)
      ans = min(f(i, bit), ans);
    cout << ans << endl;
  }
}
Problem p01164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

class State {
public:
  int p, c, b;
  State(int p, int c, int b) : p(p), c(c), b(b) {}

  bool operator<(const State &s) const { return c > s.c; }
};

int toBin(string s) {
  int ret = 0;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == 'B')
      ret += (1 << i);

  return ret;
}

bool v[16][1 << 16];

int main() {
  int N;
  while (cin >> N, N) {
    string S, D;
    cin >> S >> D;

    int T = toBin(D);

    memset(v, 0, sizeof(v));
    priority_queue<State> q;
    for (int i = 0; i < N; i++) {
      q.push(State(i, 0, toBin(S)));
    }

    while (!q.empty()) {
      State s = q.top();
      q.pop();
      if (v[s.p][s.b])
        continue;
      v[s.p][s.b] = 1;

      if (s.b == T) {
        cout << s.c << endl;
        break;
      }

      for (int i = 0; i < N; i++) {
        int f1 = (1 << s.p);
        int f2 = (1 << i);

        bool b1 = s.b & f1;
        bool b2 = s.b & f2;

        int tb = s.b;

        if (b1)
          tb |= f2;
        else
          tb &= (~f2);
        if (b2)
          tb |= f1;
        else
          tb &= (~f1);

        if (!v[i][s.b])
          q.push(State(i, s.c + abs(s.p - i), s.b));

        if (b1 == b2)
          continue;

        if (v[i][tb])
          continue;

        q.push(State(i, s.c + abs(s.p - i), tb));
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

class State {
public:
  int p, c;
  string s;
  State(int p, int c, string s) : p(p), c(c), s(s) {}

  bool operator<(const State &s) const { return c > s.c; }
};

int toBin(string s) {
  int ret = 0;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == 'B')
      ret += (1 << i);

  return ret;
}

bool v[16][1 << 16];

int main() {
  int N;
  while (cin >> N, N) {
    string S, D;
    cin >> S >> D;

    memset(v, 0, sizeof(v));
    priority_queue<State> q;
    for (int i = 0; i < N; i++) {
      q.push(State(i, 0, S));
    }

    while (!q.empty()) {
      State s = q.top();
      q.pop();
      int b = toBin(s.s);
      if (v[s.p][b])
        continue;
      v[s.p][b] = 1;

      if (s.s == D) {
        cout << s.c << endl;
        break;
      }

      for (int i = 0; i < N; i++) {
        if (!v[i][b])
          q.push(State(i, s.c + abs(s.p - i), s.s));
        string ts = s.s;

        swap(ts[s.p], ts[i]);
        int tb = toBin(ts);
        if (!v[i][tb])
          q.push(State(i, s.c + abs(s.p - i), ts));
      }
    }
  }
}
Problem p01164 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

#define INF (1 << 29)
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()

using namespace std;

int dp[16][(1 << 16)];

int main(void) {

  int n;
  string s, t;

  while (cin >> n, n) {
    cin >> s >> t;

    fill(dp[0], dp[n], INF);
    int tmp = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == t[i])
        tmp |= (1 << i);
    }
    for (int i = 0; i < n; i++)
      dp[i][tmp] = 0;

    int ans = INF;
    for (int r = 0; r < 4; r++) {
      for (int i = 0; i < n; i++) {
        if (!(tmp >> i & 1)) {
          for (int k = 0; k < n; k++) {
            for (int j = 0; j < n; j++) {
              if (s[j] != s[k]) {
                for (int S = 0; S < (1 << n); S++) {
                  if (!(S >> j & 1) && !(S >> k & 1)) {
                    int next = S;
                    next |= (1 << j) | (1 << k);
                    if (dp[k][next] > dp[i][S] + abs(j - i) + abs(k - j))
                      dp[k][next] = dp[i][S] + abs(j - i) + abs(k - j);
                  }
                }
              }
            }
          }
        }
        ans = min(ans, dp[i][(1 << n) - 1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

#define INF (1 << 29)
#define D(x) cout << #x " is " << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()

using namespace std;

int dp[16][(1 << 16)];

int main(void) {

  int n;
  string s, t;

  while (cin >> n, n) {
    cin >> s >> t;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < (1 << n); j++)
        dp[i][j] = INF;

    int tmp = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == t[i])
        tmp |= (1 << i);
    }
    for (int i = 0; i < n; i++)
      dp[i][tmp] = 0;

    int ans = INF;
    for (int r = 0; r < 4; r++) {
      for (int i = 0; i < n; i++) {
        if (!(tmp >> i & 1)) {
          for (int k = 0; k < n; k++) {
            for (int j = 0; j < n; j++) {
              if (s[j] != s[k]) {
                for (int S = 0; S < (1 << n); S++) {
                  if ((S >> j & 1) || (S >> k & 1))
                    continue;
                  int next = S;
                  next |= (1 << j) | (1 << k);
                  if (dp[k][next] > dp[i][S] + abs(j - i) + abs(k - j))
                    dp[k][next] = dp[i][S] + abs(j - i) + abs(k - j);
                }
              }
            }
          }
        }
        ans = min(ans, dp[i][(1 << n) - 1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01164 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

const int INF = 1 << 28;
const int MAXN = 16;
int N;
string S, T;
int start, goal;
int cost[MAXN][1 << MAXN];

struct State {
  int pos, bit, cost;
  bool operator<(const State &s) const { return cost > s.cost; }
};

int dijkstra() {
  priority_queue<State> que;
  fill(cost[0], cost[MAXN], INF);
  for (int i = 0; i < N; ++i) {
    que.push((State){i, start, 0});
    cost[i][start] = 0;
  }
  while (que.size()) {
    const State s = que.top();
    que.pop();
    if (cost[s.pos][s.bit] < s.cost)
      continue;
    if (s.bit == goal)
      return s.cost;
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < N; ++j) {
        if ((s.bit & (1 << i)) ^ (s.bit & (1 << j)))
          ;
        else
          continue;
        State t = {j, s.bit ^ (1 << i) ^ (1 << j),
                   s.cost + abs(i - s.pos) + abs(j - i)};
        if (cost[t.pos][t.bit] <= t.cost)
          continue;
        que.push(t);
        cost[t.pos][t.bit] = t.cost;
      }
    }
  }
  return INF;
}

int main() {
  while (cin >> N && N) {
    cin >> S >> T;
    start = goal = 0;
    for (int i = 0; i < N; ++i) {
      if (S[i] == 'W')
        start |= 1 << i;
      if (T[i] == 'W')
        goal |= 1 << i;
    }
    cout << dijkstra() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

const int MAXN = 17;
const int INF = 1 << 28;
int N;
int start, finish;
int dp[1 << MAXN][MAXN];

int rec(int b, int p, int d) {
  int &res = dp[b][p];
  if (res != -1)
    return res;
  if (b == finish)
    return res = 0;
  res = INF;
  if (d == 100000)
    return res;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (i == j)
        continue;
      if (((b >> i) & 1) ^ ((b >> j) & 1))
        ;
      else
        continue;
      int nb = b;
      nb ^= (1 << i);
      nb ^= (1 << j);
      int addcost;
      if (p == N)
        addcost = abs(j - i);
      else
        addcost = abs(i - p) + abs(j - i);
      res = min(res, rec(nb, j, d + 1) + addcost);
    }
  }
  return res;
}

int main() {
  while (cin >> N && N) {
    string a, b;
    cin >> a >> b;
    start = finish = 0;
    for (int i = 0; i < N; ++i) {
      if (a[i] == 'W')
        start |= 1 << i;
      if (b[i] == 'W')
        finish |= 1 << i;
    }
    fill(dp[0], dp[1 << MAXN], -1);
    int res = rec(start, N, 0);
    cout << res << endl;
  }
  return 0;
}