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

#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
long long pow16[11];
int from[9];
int to[9];
long long cur;
void sw(int a, int b) {
  int p = cur % pow16[a + 1] / pow16[a];
  int q = cur % pow16[b + 1] / pow16[b];
  cur -= pow16[a] * p;
  cur -= pow16[b] * q;
  cur += pow16[b] * p;
  cur += pow16[a] * q;
}
int main() {
  int a, b;
  pow16[0] = 1;
  for (int i = 1; i < 11; i++)
    pow16[i] = pow16[i - 1] * 16;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < 9; i++)
      scanf("%d", to + i);
    for (int i = 0; i < 9; i++)
      scanf("%d", from + i);
    long long S = 0;
    for (int i = 0; i < 9; i++)
      S += pow16[i] * from[i];
    long long T = 0;
    for (int i = 0; i < 9; i++)
      T += pow16[i] * to[i];
    priority_queue<pair<int, long long>> Q;
    map<long long, int> ijk;
    set<long long> v;
    Q.push(make_pair(0, S));
    int best = 999999999;
    if (S == T) {
      printf("0\\n");
      continue;
    }
    while (Q.size()) {
      int cost = -Q.top().first;
      cur = Q.top().second;
      Q.pop();
      if (best <= cost)
        continue;
      if (v.count(cur))
        continue;
      if (cur == T) {
        break;
      }
      v.insert(cur);
      int at = 0;
      for (int i = 0; i < 9; i++)
        if (cur % pow16[i + 1] / pow16[i] == 0)
          at = i;
      //	printf("%d %d\\n",cost,at);
      sw(at, (at + 1) % 9);
      if ((!ijk.count(cur) || ijk[cur] > cost + a)) {
        ijk[cur] = cost + a;
        if (cur == T)
          best = min(best, cost + a);
        Q.push(make_pair(-cost - a, cur));
      }
      sw(at, (at + 1) % 9);
      sw(at, (at + 8) % 9);
      if ((!ijk.count(cur) || ijk[cur] > cost + a)) {
        ijk[cur] = cost + a;
        if (cur == T)
          best = min(best, cost + a);
        Q.push(make_pair(-cost - a, cur));
      }
      sw(at, (at + 8) % 9);
      sw(at, (at + 3) % 9);
      if ((!ijk.count(cur) || ijk[cur] > cost + b)) {
        ijk[cur] = cost + b;
        if (cur == T)
          best = min(best, cost + b);
        Q.push(make_pair(-cost - b, cur));
      }
      sw(at, (at + 3) % 9);
      sw(at, (at + 6) % 9);
      if ((!ijk.count(cur) || ijk[cur] > cost + b)) {
        ijk[cur] = cost + b;
        if (cur == T)
          best = min(best, cost + b);
        Q.push(make_pair(-cost - b, cur));
      }
    }
    printf("%d\\n", best);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
struct wolf {
  char m[9];
  wolf() {}
};
inline bool operator<(const wolf &a, const wolf &b) {
  for (int i = 0; i < 9; i++)
    if (a.m[i] != b.m[i])
      return a.m[i] < b.m[i];
  return false;
}
inline bool operator==(const wolf &a, const wolf &b) {
  for (int i = 0; i < 9; i++)
    if (a.m[i] != b.m[i])
      return false;
  return true;
}
int from[9];
int to[9];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < 9; i++)
      scanf("%d", to + i);
    for (int i = 0; i < 9; i++)
      scanf("%d", from + i);
    wolf S;
    for (int i = 0; i < 9; i++)
      S.m[i] = from[i];
    wolf T;
    for (int i = 0; i < 9; i++)
      T.m[i] = to[i];
    priority_queue<pair<int, wolf>> Q;
    map<wolf, int> ijk;
    set<wolf> v;
    Q.push(make_pair(0, S));
    int best = 999999999;
    if (S == T)
      best = 0;
    while (Q.size()) {
      int cost = -Q.top().first;
      wolf cur = Q.top().second;
      Q.pop();
      if (best <= cost)
        continue;
      if (v.count(cur))
        continue;
      if (cur == T) {
        break;
      }
      v.insert(cur);
      int at = 0;
      for (int i = 0; i < 9; i++)
        if (cur.m[i] == 0)
          at = i;
      //	printf("%d %d\\n",cost,at);
      swap(cur.m[at], cur.m[(at + 1) % 9]);
      if ((!ijk.count(cur) || ijk[cur] > cost + a)) {
        ijk[cur] = cost + a;
        if (cur == T)
          best = min(best, cost + a);
        Q.push(make_pair(-cost - a, cur));
      }
      swap(cur.m[at], cur.m[(at + 1) % 9]);
      swap(cur.m[at], cur.m[(at + 8) % 9]);
      if ((!ijk.count(cur) || ijk[cur] > cost + a)) {
        ijk[cur] = cost + a;
        if (cur == T)
          best = min(best, cost + a);
        Q.push(make_pair(-cost - a, cur));
      }
      swap(cur.m[at], cur.m[(at + 8) % 9]);
      swap(cur.m[at], cur.m[(at + 3) % 9]);
      if ((!ijk.count(cur) || ijk[cur] > cost + b)) {
        ijk[cur] = cost + b;
        if (cur == T)
          best = min(best, cost + b);
        Q.push(make_pair(-cost - b, cur));
      }
      swap(cur.m[at], cur.m[(at + 3) % 9]);
      swap(cur.m[at], cur.m[(at + 6) % 9]);
      if ((!ijk.count(cur) || ijk[cur] > cost + b)) {
        ijk[cur] = cost + b;
        if (cur == T)
          best = min(best, cost + b);
        Q.push(make_pair(-cost - b, cur));
      }
    }
    printf("%d\\n", best);
  }
}