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

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

map<string, int> m;

class State {
public:
  string str;
  int posz;
  int depth;
};

void solve(void) {
  State s, t;
  deque<State> q;
  string back;

  s.str = "01234567";
  s.posz = 0;
  s.depth = 0;

  q.push_back(s);

  while (!q.empty()) {
    s = q.front();
    q.pop_front();

    // cout << s.str;
    if (m.find(s.str) != m.end()) {
      // cout << " -> passed" << endl;
      continue;
    }
    m[s.str] = s.depth;
    // cout << " = " << s.depth << endl;
    t = s;

    if (s.posz != 0 && s.posz != 4) {
      swap(s.str[s.posz], s.str[s.posz - 1]);
      s.posz = s.posz - 1;
      s.depth++;
      q.push_back(s);
    }
    s = t;
    if (s.posz != 3 && s.posz != 7) {
      swap(s.str[s.posz], s.str[s.posz + 1]);
      s.posz = s.posz + 1;
      s.depth++;
      q.push_back(s);
    }
    s = t;
    if (0 <= s.posz && s.posz <= 3) {
      swap(s.str[s.posz], s.str[s.posz + 4]);
      s.posz = s.posz + 4;
      s.depth++;
      q.push_back(s);
    }
    s = t;
    if (4 <= s.posz && s.posz <= 7) {
      swap(s.str[s.posz], s.str[s.posz - 4]);
      s.posz = s.posz - 4;
      s.depth++;
      q.push_back(s);
    }
  }
}

int main(void) {
  string str;
  string query;
  char tmp;

  solve();

  while (getline(cin, str)) {
    istringstream iss(str);

    query.clear();
    while (iss >> tmp) {
      query += tmp;
    }

    cout << m[query] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <sstream>
#include <string>

using namespace std;

class State {
public:
  string str;
  int posz;
  int times;
};

int main(void) {
  string str;

  while (getline(cin, str)) {
    string tmps = "";
    istringstream iss(str);
    char tmp;
    map<string, bool> visited;
    deque<State> q;
    State s, t;
    int i = 0;

    while (iss >> tmp) {
      tmps += tmp;
      if (tmp == '0') {
        s.posz = i;
      }
      i++;
    }

    s.str = tmps;
    s.times = 0;
    q.push_back(s);

    while (!q.empty()) {
      s = q.front();
      q.pop_front();
      // cout << s.str << " " << s.posz << endl;
      if (s.str == "01234567") {
        cout << s.times << endl;
        break;
      }

      if (visited[s.str] == true) {
        // cout << " passed!" << endl;
        continue;
      }
      visited[s.str] = true;

      t = s;
      if (t.posz == 0) {
        swap(t.str[t.posz], t.str[t.posz + 1]);
        if (visited[t.str] == false) {
          t.posz += 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz + 4]);
        if (visited[t.str] == false) {
          t.posz += 4;
          t.times++;
          q.push_back(t);
        }
      } else if (t.posz == 4) {
        swap(t.str[t.posz], t.str[t.posz + 1]);
        if (visited[t.str] == false) {
          t.posz += 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz - 4]);
        if (visited[t.str] == false) {
          t.posz -= 4;
          t.times++;
          q.push_back(t);
        }
      } else if (t.posz == 3) {
        swap(t.str[t.posz], t.str[t.posz - 1]);
        if (visited[t.str] == false) {
          t.posz -= 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz + 4]);
        if (visited[t.str] == false) {
          t.posz += 4;
          t.times++;
          q.push_back(t);
        }
      } else if (t.posz == 7) {
        swap(t.str[t.posz], t.str[t.posz - 1]);
        if (visited[t.str] == false) {
          t.posz -= 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz - 4]);
        if (visited[t.str] == false) {
          t.posz -= 4;
          t.times++;
          q.push_back(t);
        }
      } else if (t.posz == 1 || t.posz == 2) {
        swap(t.str[t.posz], t.str[t.posz + 1]);
        if (visited[t.str] == false) {
          t.posz += 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz - 1]);
        if (visited[t.str] == false) {
          t.posz -= 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz + 4]);
        if (visited[t.str] == false) {
          t.posz += 4;
          t.times++;
          q.push_back(t);
        }
      } else {
        swap(t.str[t.posz], t.str[t.posz + 1]);
        if (visited[t.str] == false) {
          t.posz += 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz - 1]);
        if (visited[t.str] == false) {
          t.posz -= 1;
          t.times++;
          q.push_back(t);
        }
        t = s;
        swap(t.str[t.posz], t.str[t.posz - 4]);
        if (visited[t.str] == false) {
          t.posz -= 4;
          t.times++;
          q.push_back(t);
        }
      }
    }
  }
}