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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

struct p {
  int num[8];
  int pos; // 0?????????
  int t;
};

bool operator<(const p &a, const p &b) {
  for (int i = 0; i < 8; i++) {
    if (a.num[i] < b.num[i]) {
      return true;
    } else if (a.num[i] > b.num[i]) {
      return false;
    }
  }
  return false;
}

bool operator>(const p &a, const p &b) {
  for (int i = 0; i < 8; i++) {
    if (a.num[i] > b.num[i]) {
      return true;
    } else if (a.num[i] < b.num[i]) {
      return false;
    }
  }
  return false;
}

bool operator==(const p &a, const p &b) {
  for (int i = 0; i < 8; i++) {
    if (a.num[i] != b.num[i]) {
      return false;
    }
  }
  return true;
}

p puzzle; //?????¨??????????????????

set<p> all; //??¨?????????????????????

p comp; //????????¢

int res[1000]; //??????

int solve() {
  //??¨??????????????????????????????
  queue<p> que;
  que.push(comp);
  all.insert(comp);
  while (!que.empty()) {
    p puz = que.front();
    que.pop();
    //?§????
    //???
    p temp;
    if (puz.pos >= 4) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos - 4]);
      temp.pos -= 4;
      temp.t++;
      if (all.find(temp) == all.end()) {
        //????????¨?????????????±???¢?????´???
        que.push(temp);
        all.insert(temp);
      }
    }
    //???
    if (puz.pos <= 3) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos + 4]);
      temp.pos += 4;
      temp.t++;
      if (all.find(temp) == all.end()) {
        que.push(temp);
        all.insert(temp);
      }
    }
    //???
    if (puz.pos != 3 && puz.pos != 7) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos + 1]);
      temp.pos += 1;
      temp.t++;
      if (all.find(temp) == all.end()) {
        que.push(temp);
        all.insert(temp);
      }
    }
    //???
    if (puz.pos != 0 && puz.pos != 4) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos - 1]);
      temp.pos -= 1;
      temp.t++;
      if (all.find(temp) == all.end()) {
        que.push(temp);
        all.insert(temp);
      }
    }
  }
}

int main() {
  for (int i = 0; i < 8; i++) {
    comp.num[i] = i;
  }
  comp.pos = 0;
  comp.t = 0;
  solve();
  int a;
  int k = 0;
  while (cin >> a) {
    puzzle.num[0] = a;
    for (int i = 1; i < 8; i++) {
      cin >> puzzle.num[i];
    }
    set<p>::iterator ite = all.find(puzzle);
    res[k++] = ite->t;
  }
  for (int i = 0; i < k; i++) {
    cout << res[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

struct p {
  int num[8];
  int pos; // 0?????????
  int t;
};

bool operator<(const p &a, const p &b) {
  for (int i = 0; i < 8; i++) {
    if (a.num[i] < b.num[i]) {
      return true;
    } else if (a.num[i] > b.num[i]) {
      return false;
    }
  }
  return false;
}

bool operator>(const p &a, const p &b) {
  for (int i = 0; i < 8; i++) {
    if (a.num[i] > b.num[i]) {
      return true;
    } else if (a.num[i] < b.num[i]) {
      return false;
    }
  }
  return false;
}

bool operator==(const p &a, const p &b) {
  for (int i = 0; i < 8; i++) {
    if (a.num[i] != b.num[i]) {
      return false;
    }
  }
  return true;
}

p puzzle; //?????¨??????????????????

p comp; //????????¢

int res[1000]; //??????

int solve() {
  //???????????????????§£????????????????????°?????????
  queue<p> que;
  set<p> history; //?±\\??´
  que.push(puzzle);
  history.insert(puzzle);
  while (1) {
    p puz = que.front();
    que.pop();
    //????????????????????????????????§??????
    if (puz == comp) {
      //??????
      return puz.t;
    }
    //?§????
    //???
    p temp;
    if (puz.pos >= 4) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos - 4]);
      temp.pos -= 4;
      temp.t++;
      if (history.find(temp) == history.end()) {
        //????????¨?????????????±???¢?????´???
        que.push(temp);
        history.insert(temp);
      }
    }
    //???
    if (puz.pos <= 3) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos + 4]);
      temp.pos += 4;
      temp.t++;
      if (history.find(temp) == history.end()) {
        que.push(temp);
        history.insert(temp);
      }
    }
    //???
    if (puz.pos != 3 && puz.pos != 7) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos + 1]);
      temp.pos += 1;
      temp.t++;
      if (history.find(temp) == history.end()) {
        que.push(temp);
        history.insert(temp);
      }
    }
    //???
    if (puz.pos != 0 && puz.pos != 4) {
      temp = puz;
      swap(temp.num[temp.pos], temp.num[temp.pos - 1]);
      temp.pos -= 1;
      temp.t++;
      if (history.find(temp) == history.end()) {
        que.push(temp);
        history.insert(temp);
      }
    }
  }
}

int main() {
  for (int i = 0; i < 8; i++) {
    comp.num[i] = i;
  }
  comp.pos = 0;
  comp.t = 0;
  int a;
  int k = 0;
  while (cin >> a) {
    puzzle.num[0] = a;
    for (int i = 1; i < 8; i++) {
      cin >> puzzle.num[i];
    }
    for (int i = 0; i < 8; i++) {
      if (puzzle.num[i] == 0) {
        puzzle.pos = i;
        break;
      }
    }
    puzzle.t = 0;
    res[k++] = solve();
  }
  for (int i = 0; i < k; i++) {
    cout << res[i] << endl;
  }
  return 0;
}