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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

int main() {
  // ifstream cin("/Volumes/env1/work/aoj/2040/a");
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      return 0;
    }

    string init;
    string final;
    cin >> init;
    cin >> final;

    auto encode = [](const string &s) {
      int r = 0b0;
      for (int i = 0; i < s.size(); ++i) {
        if (i != 0) {
          r <<= 1;
        }
        if (s[i] == 'B') {
          r |= 0b1;
        }
      }
      return r;
    };

    auto pow = [](int b, int e) {
      int r = 1;
      for (int i = 0; i < e; ++i) {
        r *= b;
      }
      return r;
    };

    auto target = encode(final);
    auto start = encode(init);

    vector<vector<int>> memo(n, vector<int>(pow(2, n), -1));
    // p ??????????????´?????¶?????? s ?????¨??????????°???????????????????
    function<int(int, int)> f = [&f, &memo, n, &pow, start](int p,
                                                            int s) -> int {
      // cout << static_cast<bitset<8>>(s) << endl;
      if (memo[p][s] != -1) {
        // cout << "memo1" << p << ' ' << s << endl;
        return memo[p][s];
      }

      // ??????????????´????????????
      if (s == 0) {
        return 0;
      }

      int min_ = numeric_limits<int>::max();
      int pp = -1;
      for (int i = 0; i < n; ++i) {
        // i ??????????????§????????´????????????
        if (((s >> i) & 1) == 0) {
          continue;
        }
        for (int j = 0; j < n; ++j) {
          if (i == j) {
            continue;
          }
          // j ??????????????§????????´????????????
          else if ((s >> j & 1) == 0) {
            continue;
          }
          // ?????????????????\\????????????????°???????????????????
          else if ((((start >> i) & 1) ^ ((start >> j) & 1)) == 0) {
            continue;
          } else {
            // p ?????? i ?????§?????£???, i ?????? j ?????§??????
            auto cost = abs(i - p) + abs(j - i);
            auto ss = s;
            ss ^= 1 << i;
            ss ^= 1 << j;
            // cout << "pos: " << p << " swap " << i << ", " << j
            //      << ": " << static_cast<bitset<8>>(s) << " to "
            //      << static_cast<bitset<8>>(ss) << " cost " << cost
            //      << endl;
            auto min__ = min_;
            min_ = min(min_, cost + f(j, ss));
            if (min__ != min_) {
              pp = p;
            }
          }
        }
      }
      // cout << "write memo " << pp << " " << static_cast<bitset<8>>(s) <<
      // endl;
      memo[pp][s] = min_;
      assert(min_ != -1);

      return min_;
    };

    int min_ = numeric_limits<int>::max();
    for (int i = 0; i < n; ++i) {
      min_ = min(min_, f(i, start ^ target));
    }
    cout << min_ << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

int main() {
  // ifstream cin("/Volumes/env1/work/aoj/2040/a");
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      return 0;
    }

    string init;
    string final;
    cin >> init;
    cin >> final;

    auto encode = [](const string &s) {
      int r = 0b0;
      for (int i = 0; i < s.size(); ++i) {
        if (i != 0) {
          r <<= 1;
        }
        if (s[i] == 'B') {
          r |= 0b1;
        }
      }
      return r;
    };

    auto pow = [](int b, int e) {
      int r = 1;
      for (int i = 0; i < e; ++i) {
        r *= b;
      }
      return r;
    };

    auto target = encode(final);
    auto start = encode(init);

    vector<vector<int>> memo(n, vector<int>(pow(2, n), -1));
    // p ??????????????´?????¶?????? s ?????¨??????????°???????????????????
    function<int(int, int)> f = [&f, &memo, n, &pow, start](int p,
                                                            int s) -> int {
      // cout << static_cast<bitset<8>>(s) << endl;
      if (memo[p][s] != -1) {
        return memo[p][s];
      }

      // ??????????????´????????????
      if (s == 0) {
        return 0;
      }

      int min_ = numeric_limits<int>::max();
      int pp = -1;
      for (int i = 0; i < n; ++i) {
        // i ??????????????§????????´????????????
        if (((s >> i) & 1) == 0) {
          continue;
        }
        for (int j = 0; j < n; ++j) {
          if (i == j) {
            continue;
          }
          // j ??????????????§????????´????????????
          else if ((s >> j & 1) == 0) {
            continue;
          }
          // ?????????????????\\????????????????°???????????????????
          else if ((((start >> i) & 1) ^ ((start >> j) & 1)) == 0) {
            continue;
          } else {
            // p ?????? i ?????§?????£???, i ?????? j ?????§??????
            auto cost = abs(i - p) + abs(j - i);
            auto ss = s;
            ss ^= 1 << i;
            ss ^= 1 << j;
            // cout << "pos: " << p << " swap " << i << ", " << j
            //     << ": " << static_cast<bitset<8>>(s) << " to "
            //     << static_cast<bitset<8>>(ss) << " cost " << cost
            //     << endl;
            auto min__ = min_;
            min_ = min(min_, cost + f(j, ss));
            if (min__ != min_) {
              // cout << "change" << endl;
              pp = j;
            }
          }
        }
      }
      memo[pp][s] = min_;
      assert(min_ != -1);

      return min_;
    };

    int min_ = numeric_limits<int>::max();
    for (int i = 0; i < n; ++i) {
      min_ = min(min_, f(i, start ^ target));
    }
    cout << min_ << endl;
  }
}