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

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int Dis[3];

void Set_Movable(int ind) {
  if (ind % 4 == 0) {
    Dis[0] = -1;
    Dis[1] = ind + 1;
  } else if (ind % 4 == 3) {
    Dis[0] = ind - 1;
    Dis[1] = -1;
  } else {
    Dis[0] = ind - 1;
    Dis[1] = ind + 1;
  }
  Dis[2] = (ind + 4) % 8;
}

int main(void) {
  map<string, int> all_data;
  queue<string> puzz;
  queue<int> index;
  queue<int> deep;
  puzz.push("01234567");
  index.push(0);
  deep.push(0);
  int ind, d;
  string str;
  string temp;
  while (!puzz.empty()) {
    str = puzz.front();
    ind = index.front();
    d = deep.front();
    Set_Movable(ind);
    puzz.pop();
    index.pop();
    deep.pop();
    for (int i = 0; i < 3; ++i) {
      temp = str;
      if (Dis[i] >= 0) {
        swap(temp[ind], temp[Dis[i]]);
        if (all_data[temp] == 0 && temp != "01234567") {
          all_data[temp] = d + 1;
          puzz.push(temp);
          index.push(Dis[i]);
          deep.push(d + 1);
        }
      }
    }
  }
  while (getline(cin, temp)) {
    string str = "";
    for (int i = 0; i < 8; ++i) {
      str += temp[2 * i];
    }
    cout << all_data[str] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;

int Dis[3];

void Set_Movable(int ind) {
  if (ind % 4 == 0) {
    Dis[0] = -1;
    Dis[1] = ind + 1;
  } else if (ind % 4 == 3) {
    Dis[0] = ind - 1;
    Dis[1] = -1;
  } else {
    Dis[0] = ind - 1;
    Dis[1] = ind + 1;
  }
  Dis[2] = (ind + 4) % 8;
}

int Change_CtoI(char c) { return (int)c - (int)'0'; }

int Cal_Cost(string str) {
  int ret = 0;
  for (int i = 0; i < 8; ++i) {
    if ((int)str[i] < 4) {
      if (i < 4) {
        ret += abs(Change_CtoI(str[i]) - i);
      } else {
        ret += abs(Change_CtoI(str[i]) - i) + 1;
      }
    } else {
      if (i >= 4) {
        ret += abs(Change_CtoI(str[i]) - i);
      } else {
        ret += abs(Change_CtoI(str[i]) - i) + 1;
      }
    }
  }
  return ret;
}

int main(void) {
  string temp;
  while (getline(cin, temp)) {
    string str = "";
    int ind;
    for (int i = 0; i < 8; ++i) {
      str += temp[2 * i];
      if (temp[2 * i] == '0') {
        ind = i;
      }
    }

    if (str == "01234567") {
      cout << 0 << endl;
    } else {
      int ret, cn, mc, cost;
      bool find = false;
      string ss;
      queue<string> puzz;
      queue<int> count;
      queue<int> index;
      puzz.push(str);
      count.push(0);
      index.push(ind);
      map<string, bool> ob;
      ob[str] = true;
      while (!puzz.empty() && !find) {
        str = puzz.front();
        ind = index.front();
        cn = count.front();
        puzz.pop();
        count.pop();
        index.pop();
        Set_Movable(ind);
        mc = Cal_Cost(str);
        for (int i = 0; i < 3; ++i) {
          ss = str;
          if (!(Dis[i] < 0)) {
            swap(ss[ind], ss[Dis[i]]);
            if (!ob[ss]) {
              ob[ss] = true;
              cost = Cal_Cost(ss);
              if (mc + 4 >= cost) {
                if (mc > cost) {
                  mc = cost;
                }
                puzz.push(ss);
                count.push(cn + 1);
                index.push(Dis[i]);
              }
            }
            if (ss == "01234567") {
              ret = cn + 1;
              find = true;
            }
          }
        }
      }
      cout << ret << endl;
    }
  }
  return 0;
}