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

#include <iostream>
#include <map>
#include <queue>
#include <utility>

using namespace std;

int main() {
  int tmp;
  priority_queue<pair<int, string>, vector<pair<int, string>>,
                 greater<pair<int, string>>>
      P;
  map<string, int> map;
  P.push(make_pair(0, "01234567"));
  map.insert(make_pair("01234567", 0));
  for (; !P.empty();) {
    pair<int, string> tmp = P.top();
    P.pop();
    for (int i = 0; i < 8; i++) {
      if (tmp.second[i] == '0') {
        string ss = tmp.second;
        //		      cout<<"i:"<<i<<" str="<<ss<<endl;
        if (i != 3 && i != 7) {
          ss = tmp.second;
          swap(ss[i], ss[i + 1]);
          //  cout<<"swap:"<<i<<" "<<i+1<<" "<<ss<<endl;
          if (map.insert(make_pair(ss, tmp.first + 1)).second == 1)
            P.push(make_pair(tmp.first + 1, ss));
        }
        if (i != 0 && i != 4) {
          ss = tmp.second;
          swap(ss[i - 1], ss[i]);
          //			  cout<<"swap:"<<i-1<<" "<<i<<" "<<ss<<endl;
          if (map.insert(make_pair(ss, tmp.first + 1)).second == 1)
            P.push(make_pair(tmp.first + 1, ss));
        }
        if (i < 4) {
          ss = tmp.second;
          swap(ss[i], ss[i + 4]);
          //			  cout<<"swap:"<<i<<" "<<i+4<<" "<<ss<<endl;
          if (map.insert(make_pair(ss, tmp.first + 1)).second == 1)
            P.push(make_pair(tmp.first + 1, ss));
        } else if (i > 3) {
          ss = tmp.second;
          swap(ss[i - 4], ss[i]);
          //			  cout<<"swap:"<<i-4<<" "<<i<<" "<<ss<<endl;
          if (map.insert(make_pair(ss, tmp.first + 1)).second == 1)
            P.push(make_pair(tmp.first + 1, ss));
        }
      }
    }
    //	  	  cout<<tmp.first<<" "<<tmp.second<<endl;
  }

  for (; cin >> tmp;) {
    string s;
    s += tmp + '0';
    for (int i = 0; i < 7; i++) {
      cin >> tmp;
      s += tmp + '0';
    }
    int ans = -1;
    cout << map[s] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <utility>

using namespace std;

int main() {
  int tmp;
  for (; cin >> tmp;) {
    string s;
    s += tmp + '0';
    for (int i = 0; i < 7; i++) {
      cin >> tmp;
      s += tmp + '0';
    }
    int ans = -1;
    priority_queue<pair<int, string>, vector<pair<int, string>>,
                   greater<pair<int, string>>>
        P;
    map<string, bool> map;

    P.push(make_pair(0, s));
    for (; !P.empty();) {
      pair<int, string> tmp = P.top();
      P.pop();
      if (tmp.second == "01234567") {
        ans = tmp.first;
        break;
      } else {
        for (int i = 0; i < 8; i++) {
          if (tmp.second[i] == '0') {
            string ss = tmp.second;
            //		      cout<<"i:"<<i<<" str="<<ss<<endl;
            if (i != 3 && i != 7) {
              ss = tmp.second;
              swap(ss[i], ss[i + 1]);
              //  cout<<"swap:"<<i<<" "<<i+1<<" "<<ss<<endl;
              if (map.insert(make_pair(ss, true)).second == 1)
                P.push(make_pair(tmp.first + 1, ss));
            }
            if (i != 0 && i != 4) {
              ss = tmp.second;
              swap(ss[i - 1], ss[i]);
              //			  cout<<"swap:"<<i-1<<" "<<i<<"
              //"<<ss<<endl;
              if (map.insert(make_pair(ss, true)).second == 1)
                P.push(make_pair(tmp.first + 1, ss));
            }
            if (i < 4) {
              ss = tmp.second;
              swap(ss[i], ss[i + 4]);
              //			  cout<<"swap:"<<i<<" "<<i+4<<"
              //"<<ss<<endl;
              if (map.insert(make_pair(ss, true)).second == 1)
                P.push(make_pair(tmp.first + 1, ss));
            } else if (i > 3) {
              ss = tmp.second;
              swap(ss[i - 4], ss[i]);
              //			  cout<<"swap:"<<i-4<<" "<<i<<"
              //"<<ss<<endl;
              if (map.insert(make_pair(ss, true)).second == 1)
                P.push(make_pair(tmp.first + 1, ss));
            }
          }
        }
      }
      //	  	  cout<<tmp.first<<" "<<tmp.second<<endl;
    }
    cout << ans << endl;
  }
}

/*
                  string ss=tmp.second;
                  if(ss[i]=='0')
                    {
                      if(i!=3 && i!=7)
                        {
                          ss=tmp.second;
                          swap(ss[i],ss[i+1]);
                          if(map.insert(make_pair(ss,true)).second==1)
                            P.push(make_pair(tmp.first+1,ss));
                        }
                      if(i!=0 && i!=4)
                        {
                          ss=tmp.second;
                          swap(ss[i],ss[i-1]);
                          if(map.insert(make_pair(ss,true)).second==1)
                            P.push(make_pair(tmp.first+1,ss));
                        }
                      if(i<4)
                        {
                          ss=tmp.second;
                          swap(ss[i],ss[i+4]);
                          if(map.insert(make_pair(ss,true)).second==1)
                            P.push(make_pair(tmp.first+1,ss));
                        }
                      if(i>3)
                        {
                          ss=tmp.second;
                          swap(ss[i],ss[i-4]);
                          if(map.insert(make_pair(ss,true)).second==1)
                            P.push(make_pair(tmp.first+1,ss));
                        }
                    }
                }
*/