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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  // 01234567
  string final = "01234567";

  map<string, int> M;
  set<string> S;
  queue<pair<string, int>> Q;
  Q.push(mp(final, 0));
  while (!Q.empty()) {
    string s = Q.front().first;
    int ini = Q.front().second;
    Q.pop();
    if (S.find(s) != S.end())
      continue;
    S.insert(s);
    M[s] = ini;
    int zerop = 0;
    rep(i, 8) if (s[i] == '0') zerop = i;
    rep(i, 4) {
      int newp = zerop + dx[i];
      newp += 4 * dy[i];
      if (zerop == 3 and newp == 4)
        continue;
      if (zerop == 4 and newp == 3)
        continue;
      if (0 <= newp and newp <= 7) {
        string news = s;
        swap(news[zerop], news[newp]);
        Q.push(mp(news, ini + 1));
      }
    }
  }
  string s;
  while (getline(cin, s)) {
    stringstream ss;
    ss << s;
    string inp;
    rep(i, 8) {
      char c;
      ss >> c;
      inp.push_back(c);
    }
    cout << M[inp] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1, 0, 1, 0};
const int dx[] = {0, 1, 0, -1};

const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  // 01234567
  string final;
  rep(i, 8) final.push_back('0' + i);

  vector<int> V;
  string s;
  while (getline(cin, s)) {
    stringstream ss;
    ss << s;
    string first;
    rep(i, 8) {
      char c;
      ss >> c;
      first.push_back(c);
    }
    set<string> AL;
    queue<pair<pair<string, int>, int>> Q;
    int z = 0;
    for (int i = 0; i < 8; i++)
      if (first[i] == '0')
        z = i;
    Q.push(mp(mp(first, 0), z));
    int ok = 0;
    while (!Q.empty()) {
      string inp = Q.front().first.first;
      int ini = Q.front().first.second;
      int zerop = Q.front().second;

      Q.pop();
      if (!AL.insert(inp).second)
        continue;

      if (inp == final) {
        ok = ini;
        break;
      }
      // up right down left
      for (int i = 0; i < 4; i++) {
        int newp = zerop;
        newp += dy[i] * 4;
        newp += dx[i];
        if (zerop == 4 and newp == 3)
          continue;
        if (zerop == 3 and newp == 4)
          continue;
        if (0 <= newp and newp <= 7) {
          string news = inp;
          swap(news[zerop], news[newp]);
          Q.push(mp(mp(news, ini + 1), newp));
        }
      }
    }
    cout << ok << endl;
  }
  return 0;
}