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

// AOJ 0121 http:/judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0121/

#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>

using namespace std;

string swp(string s, const int &a, const int &b) {
  swap(s[a], s[b]);
  return s;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s = "01234567";

  queue<string> q;
  map<string, int> m;

  q.push(s);
  m[s] = 0;

  while (q.size()) {
    string t = q.front();
    q.pop();
    int idx = t.find('0');
    string u;
    if (idx - 1 >= 0 && idx != 4) {
      u = swp(t, idx, idx - 1);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
    if (idx + 1 < 8 && idx != 3) {
      u = swp(t, idx, idx + 1);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
    if (idx - 4 >= 0) {
      u = swp(t, idx, idx - 4);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
    if (idx + 4 < 8) {
      u = swp(t, idx, idx + 4);
      if (m.find(u) == m.end()) {
        m[u] = m[t] + 1;
        q.push(u);
      }
    }
  }

  string line;
  stringstream ss;
  while (getline(cin, line)) {
    ss << line;
    string t = "";
    for (int i = 0; i < 8; ++i) {
      char c;
      ss >> c;
      t += c;
    }
    cout << m[t] << endl;
  }
}

Error: TLE

Fixed submission:

// AOJ 0121 http:/judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0121/

#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>

using namespace std;

int main() {
  string goal = "01234567";
  string line;
  while (getline(cin, line)) {
    stringstream ss;
    ss << line;

    string s = "";

    for (int i = 0; i < 8; ++i) {
      char c;
      ss >> c;
      s += c;
    }

    queue<string> q;
    q.push(s);

    map<string, int> m;
    m[s] = 1;

    while (q.size()) {
      string t = q.front();
      q.pop();

      if (t == goal) {
        cout << m[t] - 1 << endl;
        break;
      }

      int idx = t.find('0');
      string a = t;
      if (idx == 3) {
        swap(a[idx], a[idx - 1]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx - 1]);
        swap(a[idx], a[idx + 4]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx + 4]);
        continue;
      }
      if (idx == 4) {
        swap(a[idx], a[idx - 4]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx - 4]);
        swap(a[idx], a[idx + 1]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx + 1]);
        continue;
      }
      if (idx - 1 >= 0) {
        swap(a[idx], a[idx - 1]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx - 1]);
      }
      if (idx + 1 < 8) {
        swap(a[idx], a[idx + 1]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx + 1]);
      }
      if (idx - 4 >= 0) {
        swap(a[idx], a[idx - 4]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx - 4]);
      }
      if (idx + 4 < 8) {
        swap(a[idx], a[idx + 4]);
        if (!m[a]) {
          m[a] = m[t] + 1;
          q.push(a);
        }
        swap(a[idx], a[idx + 4]);
      }
    }

  nxt:;
  }
}