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

#include <bits/stdc++.h>
#define inf 1 << 29
#define linf (1e16)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0 + sqrt(5.0)) / 2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pd(a) printf("%.10f\\n", (double)(a))
#define pld(a) printf("%.10Lf\\n", (ld)(a))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--)
#define Unique(v) v.erase(unique(all(v)), v.end())
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;

string s = "";
string ans = "123456780";
int d[4] = {-1, 1, -3, 3};

typedef pair<string, pair<int, int>> state;

int solve() {
  int ind = 0;
  FOR(i, 0, 9) if (s[i] == '0') ind = i;
  queue<state> q;
  set<string> st;
  st.insert(s);
  q.push(mp(s, mp(0, ind)));
  while (!q.empty()) {
    state u = q.front();
    q.pop();
    if (u.f == ans)
      return u.s.f;
    FOR(i, 0, 4) {
      int index = u.s.s;
      if (index % 3 == 0 && d[i] == -1)
        continue;
      if ((index + 1) % 3 == 0 && d[i] == 1)
        continue;
      if (index <= 2 && d[i] == -3)
        continue;
      if (6 <= index && d[i] == 3)
        continue;
      string v = u.f;
      swap(v[index], v[index + d[i]]);
      if (st.count(v) == 0) {
        q.push(state(v, mp(u.s.f + 1, index + d[i])));
        st.insert(v);
      }
    }
  }
  return inf;
}

int main() {
  FOR(i, 0, 9) {
    string a;
    cin >> a;
    s += a;
  }
  cout << solve() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 1 << 29
#define linf (1e16)
#define eps (1e-8)
#define Eps (1e-12)
#define mod 1000000007
#define pi acos(-1.0)
#define phi (1.0 + sqrt(5.0)) / 2.0
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pd(a) printf("%.10f\\n", (double)(a))
#define pld(a) printf("%.10Lf\\n", (ld)(a))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--)
#define Unique(v) v.erase(unique(all(v)), v.end())
#define equals(a, b) (fabs((a) - (b)) < eps)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<pii> vpi;

string s = "";
string ans = "123456780";
int d[4] = {-1, 1, -3, 3};

bool rec(string v, int index, int step, int limit) {
  if (limit < step)
    return false;
  if (v == ans)
    return true;
  FOR(i, 0, 4) {
    if (index % 3 == 0 && d[i] == -1)
      continue;
    if ((index + 1) % 3 == 0 && d[i] == 1)
      continue;
    if (index <= 2 && d[i] == -3)
      continue;
    if (6 <= index && d[i] == 3)
      continue;
    string u = v;
    swap(u[index], u[index + d[i]]);
    if (rec(u, index + d[i], step + 1, limit))
      return true;
  }
  return false;
}

int solve() {
  int ans = 0;
  int ind = 0;
  FOR(i, 0, 9) if (s[i] == '0') ind = i;
  while (!rec(s, ind, 0, ans))
    ans++;
  return ans;
}

int main() {
  FOR(i, 0, 9) {
    string a;
    cin >> a;
    s += a;
  }
  cout << solve() << endl;
  return 0;
}