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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  map<string, int> num;
  num["01234567"] = 0;
  queue<string> q;
  q.push("01234567");
  int k = 0;
  while (!q.empty()) {
    ++k;
    int n = q.size();
    for (int i = 0; i < n; ++i) {
      string s1 = q.front();
      q.pop();

      int j = s1.find('0');
      if (j < 4) {
        string s2 = s1;
        swap(s2[j], s2[j + 4]);
        if (num.find(s2) == num.end()) {
          q.push(s2);
          num[s2] = k;
        }
      } else {
        string s2 = s1;
        swap(s2[j], s2[j - 4]);
        if (num.find(s2) == num.end()) {
          q.push(s2);
          num[s2] = k;
        }
      }
      if (j % 4 != 0) {
        string s2 = s1;
        swap(s2[j], s2[j - 1]);
        if (num.find(s2) == num.end()) {
          q.push(s2);
          num[s2] = k;
        }
      }
      if (j % 4 != 3) {
        string s2 = s1;
        swap(s2[j], s2[j + 1]);
        if (num.find(s2) == num.end()) {
          q.push(s2);
          num[s2] = k;
        }
      }
    }
  }

  for (;;) {
    string s(8, ' ');
    for (int i = 0; i < 8; ++i) {
      if (!(cin >> s[i]))
        return 0;
    }
    cout << num[s] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  for (;;) {
    string s(8, ' ');
    for (int i = 0; i < 8; ++i) {
      if (!(cin >> s[i]))
        return 0;
    }

    set<string> history;
    history.insert(s);
    queue<string> q;
    q.push(s);
    int ret = 0;
    while (!q.empty()) {
      int n = q.size();
      bool ok = false;
      for (int i = 0; i < n; ++i) {
        string s1 = q.front();
        q.pop();
        if (s1 == "01234567") {
          ok = true;
          break;
        }

        int j = s1.find('0');
        if (j < 4) {
          string s2 = s1;
          swap(s2[j], s2[j + 4]);
          if (history.find(s2) == history.end()) {
            q.push(s2);
            history.insert(s2);
          }
        } else {
          string s2 = s1;
          swap(s2[j], s2[j - 4]);
          if (history.find(s2) == history.end()) {
            q.push(s2);
            history.insert(s2);
          }
        }
        if (j % 4 != 0) {
          string s2 = s1;
          swap(s2[j], s2[j - 1]);
          if (history.find(s2) == history.end()) {
            q.push(s2);
            history.insert(s2);
          }
        }
        if (j % 4 != 3) {
          string s2 = s1;
          swap(s2[j], s2[j + 1]);
          if (history.find(s2) == history.end()) {
            q.push(s2);
            history.insert(s2);
          }
        }
      }

      if (ok) {
        cout << ret << endl;
        break;
      }
      ++ret;
    }
  }
}