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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
const int INF = INT_MAX / 2;

class C {
public:
  string str;
  int step;
  C(string _str, int _step);
};

C::C(string _str, int _step) {
  str = _str;
  step = _step;
}

int main() {
  queue<C> open;
  open.push(C("01234567", 0));
  map<string, int> ans;

  while (!open.empty()) {
    C now = open.front();
    open.pop();

    if (ans.find(now.str) != ans.end() && ans[now.str] <= now.step) {
      continue;
    }

    ans[now.str] = now.step;

    int zero;
    REP(i, now.str.size()) if (now.str[i] == '0') zero = i;
    int mv[] = {1, -1, 4, -4};

    REP(i, 4) {
      if (zero + mv[i] >= now.str.size())
        continue;
      if (zero + mv[i] < 0)
        continue;
      if (zero == 3 && zero + mv[i] == 4)
        continue;
      if (zero == 4 && zero + mv[i] == 3)
        continue;
      string tmp = now.str;
      swap(tmp[zero], tmp[zero + mv[i]]);
      open.push(C(tmp, now.step + 1));
    }
  }
  while (true) {
    string fir_inp, inp = "";
    if (!getline(cin, fir_inp))
      break;
    stringstream ss(fir_inp);
    while (ss >> fir_inp)
      inp += fir_inp;
    cout << ans[inp] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
const int INF = INT_MAX / 2;

class C {
public:
  string str;
  int step;
  C(string _str, int _step);
};

C::C(string _str, int _step) {
  str = _str;
  step = _step;
}

int main() {
  while (true) {
    string inp, fir = "";
    if (!getline(cin, inp))
      break;
    stringstream ss(inp);
    while (ss >> inp)
      fir += inp;

    queue<C> open;
    open.push(C(fir, 0));
    set<string> closed;

    while (!open.empty()) {
      C now = open.front();
      open.pop();

      if (now.str == "01234567") {
        cout << now.step << endl;
        break;
      }

      //            if(closed.find(now.str) != closed.end()){
      //                continue;
      //            }

      closed.insert(now.str);

      int zero;
      REP(i, now.str.size()) if (now.str[i] == '0') zero = i;
      int mv[] = {1, -1, 4, -4};

      REP(i, 4) {
        if (zero + mv[i] >= now.str.size())
          continue;
        if (zero + mv[i] < 0)
          continue;
        if (zero == 3 && zero + mv[i] == 4)
          continue;
        if (zero == 4 && zero + mv[i] == 3)
          continue;
        if (now.str[zero + mv[i]] - '0' == zero + mv[i])
          continue;
        string tmp = now.str;
        swap(tmp[zero], tmp[zero + mv[i]]);
        if (closed.find(tmp) != closed.end())
          continue;
        open.push(C(tmp, now.step + 1));
      }
    }
  }
  return 0;
}