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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

map<string, int> dp;
int direction[4] = {1, -1, 4, -4};

void bfs() {
  queue<string> que;
  que.push("01234567");
  dp["01234567"] = 0;
  while (!que.empty()) {
    string now = que.front();
    que.pop();
    // p???'0'?????????
    int p = 0;
    for (int j = 0; j < 8; ++j) {
      if (now[j] == '0') {
        p = j;
        break;
      }
    }

    for (int i = 0; i < 4; ++i) {
      int n = p + direction[i];
      if (0 <= n && n < 8 &&
          !(p == 3 && i == 0) && // ???????§???????????????????
          !(p == 4 && i == 1))   // ???????§???????????????????
      {
        string next = now;
        swap(next[p], next[n]);
        if (dp.find(next) == dp.end()) {
          dp[next] = dp[now] + 1;
          que.push(next);
        }
      }
    }
  }
}

int main(int argc, char *argv[]) {

  bfs();
  string line;
  while (getline(cin, line)) {
    line.erase(remove(line.begin(), line.end(), ' '), line.end());
    cout << dp[line] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

struct node {
  int number;
  int dist;

  node(int number, int dist) {
    this->number = number;
    this->dist = dist;
  }
};

int input[2][4];
bool vis[100010];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
queue<node> q;

// ??¶???
int fact(int n) {
  int ans = 1;
  for (int i = 1; i <= n; i++)
    ans = ans * i;
  return ans;
}

// ????????¬?????°???
int toNumber() {
  // ?????¬????????´??°???
  int temp[8];
  int i, j, next = 0, number = 0;
  for (i = 0; i < 2; i++)
    for (j = 0; j < 4; j++) {
      temp[next] = input[i][j];
      next++;
    }

  // ????????????
  for (i = 0; i < 8; i++) {
    int tot = 0;
    for (j = i + 1; j < 8; j++) {
      if (temp[j] < temp[i])
        tot++;
    }
    number += tot * fact(8 - i - 1);
  }
  return number + 1;
}

// ??°?????¬???2 x 4?????°???
void toArray(int number) {
  number--;

  int temp[8];
  bool vis[8];
  memset(vis, false, sizeof(vis));

  int next = 0;
  for (int i = 0; i < 8; i++) {
    int f = fact(8 - i - 1), count = 0, v;
    int value = number / f; // ?¬¬value??????
    number = number % f;
    for (int j = 0; j < 8; j++) {
      if (!vis[j]) {
        if (count == value) {
          v = j;
          vis[j] = true;
          break;
        }
        count++;
      }
    }
    temp[i] = v;
  }
  // temp??¬???input
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 4; j++) {
      input[i][j] = temp[next];
      next++;
    }
}

bool inArea(int x, int y) { return x >= 0 && x < 2 && y >= 0 && y < 4; }

// ??????bfs????´¢
int bfs() {
  while (!q.empty())
    q.pop();

  // input??¬???number
  int start = toNumber();
  q.push(node(start, 0));
  vis[start] = true;

  while (!q.empty()) {
    node no = q.front();
    q.pop();

    if (no.number == 1)
      return no.dist;

    toArray(no.number);

    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 4; j++) {
        for (int k = 0; k < 4; k++) {
          int x1 = i, y1 = j, x2 = x1 + dx[k], y2 = y1 + dy[k], temp, number;
          // ????????¨?????´??????
          if (inArea(x2, y2)) {
            // ?????¢(x1,y1)???(x2,y2)??????
            temp = input[x1][y1];
            input[x1][y1] = input[x2][y2];
            input[x2][y2] = temp;

            number = toNumber();

            if (!vis[number]) {
              vis[number] = true;
              q.push(node(number, no.dist + 1));
            }

            temp = input[x1][y1];
            input[x1][y1] = input[x2][y2];
            input[x2][y2] = temp;
          }
        }
      }
  }
  return -1;
}

int main() {
  int i, j;
  while (scanf("%d", &input[0][0]) != EOF) {
    memset(vis, 0, sizeof(vis));

    for (i = 1; i < 4; i++)
      scanf("%d", &input[0][i]);
    for (i = 0; i < 4; i++)
      scanf("%d", &input[1][i]);

    printf("%d\\n", bfs());
  }
  return 0;
}