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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> paii;
typedef long long ll;

typedef pair<int, paii> tupl;

int n;
string a, b;
int vtx[18][66000];

int solve() {

  int x = 0, y = 0, res = 100000;

  for (int i = 0; i < n; i++) {
    x = x << 1;
    y = y << 1;
    x += (a[i] == 'W' ? 0 : 1);
    y += (b[i] == 'W' ? 0 : 1);
  }

  for (int k = 0; k < n; ++k) {

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < 66000; ++j)
        vtx[i][j] = (j != x ? 100000 : 0);

    priority_queue<tupl, vector<tupl>, greater<tupl>> qu;
    qu.push(make_pair(0, make_pair(k, x)));
    vtx[k][x] = 0;

    while (!qu.empty()) {

      int m = qu.top().second.first;
      int w = qu.top().second.second;
      qu.pop();

      for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {

          if (i != j && ((w >> i & 1) != (w >> j & 1)) &&
              ((w >> i & 1) != (y >> i & 1)) &&
              ((w >> j & 1) != (y >> j & 1))) {

            int z = w ^ ((1 << i) | (1 << j));
            int dsta = abs(j - m) + abs(i - j);
            int dstb = abs(i - m) + abs(j - i);

            if (vtx[i][z] > vtx[m][w] + dsta) {

              vtx[i][z] = vtx[m][w] + dsta;
              qu.push(make_pair(vtx[i][z], make_pair(i, z)));
            }
            if (vtx[j][z] > vtx[m][w] + dstb) {

              vtx[j][z] = vtx[m][w] + dstb;
              qu.push(make_pair(vtx[j][z], make_pair(j, z)));
            }
          }
        }
      }
    }

    for (int i = 0; i < n; ++i) {
      res = min(res, vtx[i][y]);
    }
  }

  return res;
}

int main() {

  while (cin >> n, n) {

    cin >> a;
    cin >> b;

    cout << solve() << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef pair<int, int> paii;
typedef long long ll;

typedef pair<int, paii> tupl;

int n;
string a, b;
int vtx[18][66000];

int solve() {

  int x = 0, y = 0, res = 100000;

  for (int i = 0; i < n; i++) {
    x = x << 1;
    y = y << 1;
    x += (a[i] == 'W' ? 0 : 1);
    y += (b[i] == 'W' ? 0 : 1);
  }

  for (int k = 0; k < n; ++k) {

    for (int i = 0; i < n; ++i)
      for (int j = 0; j < 66000; ++j)
        vtx[i][j] = (j != x ? 100000 : 0);

    priority_queue<tupl, vector<tupl>, greater<tupl>> qu;
    qu.push(make_pair(0, make_pair(k, x)));
    vtx[k][x] = 0;

    while (!qu.empty()) {

      int m = qu.top().second.first;
      int w = qu.top().second.second;
      qu.pop();

      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {

          if (i != j && ((w >> i & 1) != (w >> j & 1)) &&
              ((w >> i & 1) != (y >> i & 1)) &&
              ((w >> j & 1) != (y >> j & 1))) {

            int z = w ^ ((1 << i) | (1 << j));
            int dsta = abs(j - m) + abs(i - j);
            // int dstb = abs(i-m)+abs(j-i);

            if (vtx[i][z] > vtx[m][w] + dsta) {

              vtx[i][z] = vtx[m][w] + dsta;
              qu.push(make_pair(vtx[i][z], make_pair(i, z)));
            }
            /*
            if( vtx[j][z] > vtx[m][w] + dstb){

                    vtx[j][z] = vtx[m][w] + dstb;
                    qu.push(make_pair(vtx[j][z], make_pair(j, z)));
            }
            */
          }
        }
      }
    }

    for (int i = 0; i < n; ++i) {
      res = min(res, vtx[i][y]);
    }
  }

  return res;
}

int main() {

  while (cin >> n, n) {

    cin >> a;
    cin >> b;

    cout << solve() << endl;
  }

  return 0;
}