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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
  map<int, int> memo;
  int p = 1234567;
  memo[p] = 0;
  queue<int> q;
  q.push(p);
  while (!q.empty()) {
    int prev = q.front();
    q.pop();

    int puzzle[8];
    int t = prev;
    for (int i = 7; i >= 0; --i, t /= 10)
      puzzle[i] = t % 10;

    int zeroPos;
    for (zeroPos = 0; puzzle[zeroPos]; ++zeroPos)
      ;

    const int d[] = {-1, 1, 4, -4};
    for (int i = 0; i < 4; ++i) {
      int swapPos = zeroPos + d[i];
      if (swapPos < 0 || swapPos >= 8 || zeroPos == 3 && d[i] == 1 ||
          zeroPos == 4 && d[i] == -1)
        continue;

      int copy[8];
      memcpy(copy, puzzle, sizeof(copy));
      swap(copy[zeroPos], copy[swapPos]);
      int next = 0;
      for (int j = 0; j < 8; ++j)
        next = next * 10 + copy[j];

      if (!memo.count(next)) {
        memo[next] = memo[prev] + 1;
        q.push(next);
      }
    }
  }
  while (true) {
    char buf[128];
    cin.getline(buf, sizeof(buf));
    if (strlen(buf) == 0)
      break;
    istringstream is(buf);
    int p = 0;
    for (int i = 0; i < 8; ++i) {
      int t;
      is >> t;
      p = p * 10 + t;
    }
    printf("%d\\n", memo[p]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

int main() {
  while (true) {
    char buf[128];
    cin.getline(buf, sizeof(buf));
    if (strlen(buf) == 0)
      break;
    istringstream is(buf);
    int p = 0;
    for (int i = 0; i < 8; ++i) {
      int t;
      is >> t;
      p = p * 10 + t;
    }

    int ans = -1;
    if (p == 1234567)
      ans = 0;

    map<int, bool> visited;
    vector<int> q;
    q.push_back(p);
    for (int i = 0; ans == -1; ++i) {
      vector<int> v;
      for (int j = 0; j < q.size(); ++j)
        v.push_back(q[j]);
      q.clear();

      for (int j = 0; j < v.size(); ++j) {
        int t = v[j];
        // printf("%d\\n", t);

        int puzzle[8];
        for (int k = 7; k >= 0; --k, t /= 10)
          puzzle[k] = t % 10;

        int zeroPos;
        for (zeroPos = 0; puzzle[zeroPos]; ++zeroPos)
          ;
        const int d[] = {-4, 4, -1, 1};
        for (int k = 0; k < 4; ++k) {
          int swapPos = zeroPos + d[k];
          if (swapPos < 0 || swapPos >= 8 || zeroPos == 3 && d[k] == 1 ||
              zeroPos == 4 && d[k] == -1)
            continue;

          int copy[8];
          memcpy(copy, puzzle, sizeof(copy));
          swap(copy[zeroPos], copy[swapPos]);
          int nextPuzzle = 0;
          for (int l = 0; l < 8; ++l)
            nextPuzzle = nextPuzzle * 10 + copy[l];

          if (nextPuzzle == 1234567) {
            ans = i + 1;
            break;
          } else if (!visited[nextPuzzle]) {
            q.push_back(nextPuzzle);
            visited[nextPuzzle] = true;
          }
        }
      }
    }
    printf("%d\\n", ans);
  }

  return 0;
}