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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
using namespace std;

struct data {
  unsigned long value;
  int num;

  data(unsigned long v, int n) {
    value = v;
    num = n;
  }
  data() {}

  bool operator==(const data &a) const { return a.value == value; }
  bool operator<(const data &a) const { return value < a.value; }
};

int main() {
  string in;
  unsigned long l = 001234567;
  queue<data> que;
  que.push(data(l, 0));
  set<data> used;
  used.insert(data(l, 0));

  while (!que.empty()) {
    data d;
    d = que.front();
    que.pop();

    /* decode */
    int board[2][4];
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 4; j++) {
        board[i][j] = ((070000000 >> ((i * 4 + j) * 3)) & d.value) >>
                      ((7 - (i * 4 + j)) * 3);
      }
    }
    /* search */
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 4; j++) {
        if (!board[i][j]) {
          if (i != 0) {
            swap(board[i - 1][j], board[i][j]);
            unsigned long enc = 0;
            for (int k = 0; k < 8; k++) {
              enc |= board[0][k] << (21 - k * 3);
            }
            if (used.find(data(enc, 0)) == used.end()) {
              used.insert(data(enc, d.num + 1));
              que.push(data(enc, d.num + 1));
            }
            swap(board[i - 1][j], board[i][j]);
          }
          if (i != 1) {
            swap(board[i][j], board[i + 1][j]);
            unsigned long enc = 0;
            for (int k = 0; k < 8; k++) {
              enc |= board[0][k] << (21 - k * 3);
            }
            if (used.find(data(enc, 0)) == used.end()) {
              used.insert(data(enc, d.num + 1));
              que.push(data(enc, d.num + 1));
            }
            swap(board[i][j], board[i + 1][j]);
          }
          if (j != 0) {
            swap(board[i][j - 1], board[i][j]);
            unsigned long enc = 0;
            for (int k = 0; k < 8; k++) {
              enc |= board[0][k] << (21 - k * 3);
            }
            if (used.find(data(enc, 0)) == used.end()) {
              used.insert(data(enc, d.num + 1));
              que.push(data(enc, d.num + 1));
            }
            swap(board[i][j - 1], board[i][j]);
          }
          if (j != 3) {
            swap(board[i][j], board[i][j + 1]);
            unsigned long enc = 0;
            for (int k = 0; k < 8; k++) {
              enc |= board[0][k] << (21 - k * 3);
            }
            if (used.find(data(enc, 0)) == used.end()) {
              used.insert(data(enc, d.num + 1));
              que.push(data(enc, d.num + 1));
            }
            swap(board[i][j], board[i][j + 1]);
          }
        }
      }
    }
  }

  while (getline(cin, in)) {
    l = 0;
    stringstream sstr(in);
    int tmp;
    while (sstr >> tmp) {
      l <<= 3;
      l |= tmp;
    }
    cout << used.find(data(l, 0))->num << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
using namespace std;

struct data {
  unsigned long value;
  int num;

  data(long v, int n) {
    value = v;
    num = n;
  }
  data() {}
};

int main() {
  string in;

  while (getline(cin, in)) {
    stringstream sstr(in);
    unsigned long l = 0;
    int tmp;
    while (sstr >> tmp) {
      l <<= 3;
      l |= tmp;
    }

    queue<data> que;
    que.push(data(l, 0));
    set<unsigned long> used;
    used.insert(l);

    while (!que.empty()) {
      data d;
      d = que.front();
      que.pop();
      if (d.value == 001234567) {
        cout << d.num << endl;
        break;
      }

      /* decode */
      int board[2][4];
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
          board[i][j] = ((070000000 >> ((i * 4 + j) * 3)) & d.value) >>
                        ((7 - (i * 4 + j)) * 3);
        }
      }
      /* search */
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
          if (!board[i][j]) {
            if (i != 0) {
              swap(board[i - 1][j], board[i][j]);
              unsigned long enc = 0;
              for (int k = 0; k < 8; k++) {
                enc |= board[0][k] << (21 - k * 3);
              }
              if (used.find(enc) == used.end()) {
                used.insert(enc);
                que.push(data(enc, d.num + 1));
              }
              swap(board[i - 1][j], board[i][j]);
            }
            if (i != 1) {
              swap(board[i][j], board[i + 1][j]);
              unsigned long enc = 0;
              for (int k = 0; k < 8; k++) {
                enc |= board[0][k] << (21 - k * 3);
              }
              if (used.find(enc) == used.end()) {
                used.insert(enc);
                que.push(data(enc, d.num + 1));
              }
              swap(board[i][j], board[i + 1][j]);
            }
            if (j != 0) {
              swap(board[i][j - 1], board[i][j]);
              unsigned long enc = 0;
              for (int k = 0; k < 8; k++) {
                enc |= board[0][k] << (21 - k * 3);
              }
              if (used.find(enc) == used.end()) {
                used.insert(enc);
                que.push(data(enc, d.num + 1));
              }
              swap(board[i][j - 1], board[i][j]);
            }
            if (j != 3) {
              swap(board[i][j], board[i][j + 1]);
              unsigned long enc = 0;
              for (int k = 0; k < 8; k++) {
                enc |= board[0][k] << (21 - k * 3);
              }
              if (used.find(enc) == used.end()) {
                used.insert(enc);
                que.push(data(enc, d.num + 1));
              }
              swap(board[i][j], board[i][j + 1]);
            }
          }
        }
      }
    }
  }
}