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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma comment(linker, "/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define FRE(i, a, b) for (i = a; i <= b; i++)
#define FREE(i, a, b) for (i = a; i >= b; i--)
#define FRL(i, a, b) for (i = a; i < b; i++)
#define FRLL(i, a, b) for (i = a; i > b; i--)
#define mem(t, v) memset((t), v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a, b) scanf("%d %d", &a, &b)
#define sfff(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;

struct Node {
  int x, y, step;
  int g[2][4];
};

int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};
int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int a[10];
int vis[50000];

bool isok(int x, int y) {
  if (x >= 0 && x < 2 && y >= 0 && y < 4)
    return true;
  return false;
}

int cantor(int w[][4]) {
  int s[8], cc = 0;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 4; j++)
      s[cc++] = w[i][j];
  int sum = 0;
  for (int i = 0; i < 8; i++) {
    int t = 0;
    for (int j = i + 1; j < 8; j++) {
      if (s[i] > s[j])
        t++;
    }
    sum += (t * fac[8 - i - 1]);
  }
  return sum;
}

void bfs() {
  Node st, now;
  queue<Node> Q;
  for (int i = 0; i < 8; i++)
    st.g[i / 4][i % 4] = i;
  st.step = 0;
  st.x = st.y = 0;
  vis[cantor(st.g)] = 0;
  Q.push(st);
  while (!Q.empty()) {
    st = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      now.x = st.x + dir[i][0];
      now.y = st.y + dir[i][1];
      now.step = st.step + 1;
      if (isok(now.x, now.y)) {
        for (int j = 0; j < 2; j++)
          for (int t = 0; t < 4; t++)
            now.g[j][t] = st.g[j][t];
        swap(now.g[now.x][now.y], now.g[st.x][st.y]);
        int cc = cantor(now.g);
        if (vis[cc] != -1)
          continue;
        vis[cc] = now.step;
        Q.push(now);
      }
    }
  }
  return;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //     freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
  // #endif
  int i, j;
  memset(vis, -1, sizeof(vis));
  bfs();
  while (~scanf("%d", &a[0])) {
    for (i = 1; i < 8; i++)
      scanf("%d", &a[i]);
    Node st;
    for (int i = 0; i < 8; i++)
      st.g[i / 4][i % 4] = a[i];
    int p = cantor(st.g);
    printf("%d\\n", vis[p]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype> //tolower也是?里的
#include <cmath>
#include <cstdio>  //
#include <cstdlib> //rand()  atoi
#include <cstring> // 主要有str??和 memset
#include <ctime>   //??不住怎?? (double)clock() / CLOCKS_PER_SEC
#include <iostream>
#include <map>
#include <queue>
#include <set> //??就没用?set
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int inf = 0x3f3f3f3f;
#define all(x) x.begin(), x.end()
#define ins(x) inserter(x, x.begin())
// key0:不能理解?是干嘛的，照抄?佳汝的

int mp[2][4];
int m, n, dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};

bool check(int x, int y) {
  if (x < 0 || x > 1)
    return false;
  if (y < 0 || y > 3)
    return false;
  return true;
}
struct state {
  int l, x, y;
  int m[2][4];
};
int compute(int m[2][4]) {
  int *p = m[0] + 0;
  //	printf("%dhaha\\n", *(m[1] + 3));
  int sum = 0;
  for (int i = 0; i < 8; ++i) {
    sum *= 10;
    //		printf("%d ", *(p-i));
    sum += *(p + i);
  }
  //	printf("\\n%d\\n", sum);
  return sum;
}
queue<state> q;
set<int> vis;
int bfs() {
  while (q.size())
    q.pop();
  int i, j;
  state temp;
  vis.clear();
  for (i = 0; i < 2; ++i) {
    for (j = 0; j < 4; ++j) {
      if (mp[i][j] == 0) {
        temp.x = i;
        temp.y = j;
        break;
      }
    }
  }

  memcpy(temp.m, mp, sizeof mp);
  if (compute(temp.m) == 1234567)
    return 0;
  //	printf("haha\\n");
  vis.insert(compute(temp.m));
  temp.l = 0;
  q.push(temp);
  while (q.size()) {
    temp = q.front();
    q.pop();
    //	printf("%d\\n", compute(temp.m));
    for (i = 0; i < 4; ++i) {
      int nx = temp.x + dir[i][0];
      int ny = temp.y + dir[i][1];
      if (!check(nx, ny))
        continue;
      //	printf("%d %d\\n", nx, ny);
      //	getchar(); getchar();
      state newtemp;
      memcpy(newtemp.m, temp.m, sizeof temp.m);
      swap(newtemp.m[nx][ny], newtemp.m[temp.x][temp.y]);
      int num = compute(newtemp.m);
      if (num == 1234567) {
        return temp.l + 1;
      } else if (vis.count(num))
        continue;
      //	printf("%dere  %d\\n", num, temp.l+1);
      //	getchar(); getchar();
      vis.insert(num);
      newtemp.l = temp.l + 1;
      newtemp.x = nx;
      newtemp.y = ny;
      q.push(newtemp);
    }
  }
}
int main() {
//  ios::sync_with_stdio(false);
#ifdef LOCAL
//	freopen("output.txt", "w", stdout);
//    freopen("input.txt", "r", stdin);
#endif
  int i, j, kase;
  //	printf("%d\\n", sum);
  while (scanf("%d", &mp[0][0]) == 1) {
    int *p = mp[0] + 1;
    for (i = 0; i < 7; ++i) {
      scanf("%d", (p + i));
    }
    //		for (i = 0; i < 2; ++i){
    //			for (j = 0; j < 4; ++j) printf("%d ", mp[i][j]);
    //		}
    //		puts("");
    printf("%d\\n", bfs());
  }
  return 0;
}