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

#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
using namespace std;
map<int, int> bfs;
int a[8];
int main() {
  bfs[342391] = 0;
  queue<int> Q;
  Q.push(342391);
  while (Q.size()) {
    int at = Q.front();
    Q.pop();
    int zero;
    for (int i = 0; i < 8; i++)
      if ((at % (8 << (3 * i)) / (1 << (3 * i))) == 0)
        zero = i;
    if (zero % 4) {
      if (!bfs.count(at -
                     (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                         (1 << (3 * (zero - 1))) +
                     (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                         (1 << (3 * zero)))) {
        bfs[at -
            (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                (1 << (3 * (zero - 1))) +
            (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                (1 << (3 * zero))] = bfs[at] + 1;
        Q.push(at -
               (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                   (1 << (3 * (zero - 1))) +
               (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                   (1 << (3 * zero)));
      }
    }
    if (zero % 4 != 3) {
      if (!bfs.count(at -
                     (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                         (1 << (3 * (zero + 1))) +
                     (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                         (1 << (3 * zero)))) {
        bfs[at -
            (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                (1 << (3 * (zero + 1))) +
            (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                (1 << (3 * zero))] = bfs[at] + 1;
        Q.push(at -
               (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                   (1 << (3 * (zero + 1))) +
               (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                   (1 << (3 * zero)));
      }
    }
    if (!bfs.count(at -
                   (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                       (1 << (3 * (zero ^ 4))) +
                   (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                       (1 << (3 * zero)))) {
      bfs[at -
          (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
              (1 << (3 * (zero ^ 4))) +
          (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
              (1 << (3 * zero))] = bfs[at] + 1;
      Q.push(at -
             (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                 (1 << (3 * (zero ^ 4))) +
             (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                 (1 << (3 * zero)));
    }
  }
  while (~scanf("%d", a)) {
    for (int i = 1; i < 8; i++)
      scanf("%d", a + i);
    // bfs.clear();
    int t = 0;
    for (int i = 0; i < 8; i++) {
      t *= 8;
      t += a[i];
    }
    printf("%d\\n", bfs[t]);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <map>
#include <queue>
#include <stdio.h>
using namespace std;
map<int, int> bfs;
int a[8];
int main() {
  while (~scanf("%d", a)) {
    for (int i = 1; i < 8; i++)
      scanf("%d", a + i);
    bfs.clear();
    int t = 0;
    for (int i = 0; i < 8; i++) {
      t *= 8;
      t += a[i];
    }
    bfs[t] = 0;
    queue<int> Q;
    Q.push(t);
    while (Q.size()) {
      int at = Q.front();
      if (at == 342391)
        break;
      //	printf("%d\\n",at);
      Q.pop();
      int zero;
      for (int i = 0; i < 8; i++)
        if ((at % (8 << (3 * i)) / (1 << (3 * i))) == 0)
          zero = i;
      if (zero % 4) {
        if (!bfs.count(
                at -
                (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                    (1 << (3 * (zero - 1))) +
                (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                    (1 << (3 * zero)))) {
          bfs[at -
              (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                  (1 << (3 * (zero - 1))) +
              (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                  (1 << (3 * zero))] = bfs[at] + 1;
          Q.push(at -
                 (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                     (1 << (3 * (zero - 1))) +
                 (at % (8 << (3 * (zero - 1))) / (1 << (3 * (zero - 1)))) *
                     (1 << (3 * zero)));
        }
      }
      if (zero % 4 != 3) {
        if (!bfs.count(
                at -
                (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                    (1 << (3 * (zero + 1))) +
                (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                    (1 << (3 * zero)))) {
          bfs[at -
              (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                  (1 << (3 * (zero + 1))) +
              (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                  (1 << (3 * zero))] = bfs[at] + 1;
          Q.push(at -
                 (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                     (1 << (3 * (zero + 1))) +
                 (at % (8 << (3 * (zero + 1))) / (1 << (3 * (zero + 1)))) *
                     (1 << (3 * zero)));
        }
      }
      if (!bfs.count(at -
                     (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                         (1 << (3 * (zero ^ 4))) +
                     (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                         (1 << (3 * zero)))) {
        bfs[at -
            (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                (1 << (3 * (zero ^ 4))) +
            (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                (1 << (3 * zero))] = bfs[at] + 1;
        Q.push(at -
               (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                   (1 << (3 * (zero ^ 4))) +
               (at % (8 << (3 * (zero ^ 4))) / (1 << (3 * (zero ^ 4)))) *
                   (1 << (3 * zero)));
      }
    }
    //	printf("%d ",bfs.count(342391));
    printf("%d\\n", bfs[342391]);
  }
}