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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
map<string, int> dp;
int dx[4] = {1, -1, 4, -4};
void bfs() {
  queue<string> que;
  string p;
  p = "01234567";
  dp[p] = 0;
  que.push(p);
  while (que.size()) {
    string q;
    q = que.front();
    que.pop();
    int k;
    for (int i = 0; i < 8; i++) {
      if (q[i] == '0') {
        k = i;
        break;
      }
    }
    for (int i = 0; i < 4; i++) {
      int x;
      x = k + dx[i];
      if (x >= 0 && x <= 7 && !(k == 3 && i == 0) && !(k == 4 && i == 1)) {
        string pp;
        pp = q;
        swap(pp[x], pp[k]);
        if (dp.find(pp) == dp.end()) {
          dp[pp] = dp[q] + 1;
          que.push(pp);
        }
      }
    }
  }
}
int main() {
  bfs();
  string s;
  while (getline(cin, s)) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
    cout << dp[s] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// Problem:
// Date:
// Skill:

#define CLR(a) memset((a), 0, sizeof(a))
#define MAX 2140000000;
#define RE(i, n) for (i = 0; i < n; i++)
#define RE2(i, n) for (i = 1; i <= n; i++)
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <string>
#include <time.h>
using namespace std;
const int shift[4] = {-4, -1, 1, 4};
class node {
public:
  int paces;
  int pos;
  string arrange;
};
inline bool change(int i, node &sour) {
  if (sour.pos < 4 && shift[i] == -4)
    return 0;
  if (sour.pos >= 4 && shift[i] == 4)
    return 0;
  if (sour.pos % 4 == 3 && shift[i] == 1)
    return 0;
  if (sour.pos % 4 == 0 && shift[i] == -1)
    return 0;

  int temp = sour.pos + shift[i];
  //	if (temp < 0 || temp >= 8)return false;
  { swap(sour.arrange[sour.pos], sour.arrange[temp]); }

  sour.pos = temp;
  sour.paces++;
  return 1;
}

int bfs(node end, node star) {
  map<string, int> ma;
  queue<node> Q;
  node store;
  Q.push(end);
  //
  store = Q.front();
  if (store.arrange == star.arrange)
    return store.paces;
  ma[store.arrange] = store.paces;

  //	ma[end.arrange] = end.paces;
  while (!Q.empty()) {
    for (int i = 0; i < 4; i++) {
      store = Q.front();
      if (change(i, store) == 0)
        continue; // 越界
      // 是否重复
      if (ma.find(store.arrange) != ma.end())
        continue; // 重复

      if (store.arrange == star.arrange)
        return store.paces;

      ma[store.arrange] = store.paces;
      Q.push(store);
    }
    Q.pop();
  }

  return -1;
}

// #define LOCAL
int main() {
#ifdef LOCAL

  freopen("C:\\\\Users\\\\VULCAN\\\\Desktop\\\\data.in", "r", stdin);
  //	freopen("C:\\\\Users\\\\VULCAN\\\\Desktop\\\\data.out", "w", stdout);
#endif
  // 这是一条帅气的分界线，写程序从这里开始吧
  const node end = {0, 0, "01234567"};
  node star;
  while (getline(cin, star.arrange)) {
    for (int i = 1; i < 8; i++) {
      if (star.arrange[i] == 0)
        star.pos = i;
    }
    //	star.arrange.erase(' ');
    star.arrange.erase(remove(star.arrange.begin(), star.arrange.end(), ' '),
                       star.arrange.end());
    star.paces = 0;

    cout << bfs(end, star) << endl;
  }

  // 这是一条可爱的分界线
#ifdef LOCAL
  printf("用时%.3f秒\\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}