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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

struct P {
  int x, y;
  P() {}
  P(int x_, int y_) {}
};

// memo[a] := テ」ツδ妥」ツつコテ」ツδォテ」ツ?ョテァツ環カテヲツ??a
// テ」ツ?セテ」ツ?ァテ」ツ?ョテヲツ可凝ヲツ閉ー
map<vector<vector<int>>, int> memo;

// テ・ツョツ古ヲツ按静」ツ?療」ツ?ヲテ」ツ??」ツつ凝」ツ?凝」ツ?ゥテ」ツ??」ツ??
bool is_goal(const vector<vector<int>> &a) {
  for (int i = 0; i < 8; i++) {
    int x = i % 4, y = i / 4;
    if (a[y][x] != i)
      return false;
  }
  return true;
}

// テァツゥツコテァツ卍ステ」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ィツソツ氾」ツ??
int search(const vector<vector<int>> &a) {
  for (int i = 0; i < 8; i++) {
    int x = i % 4, y = i / 4;
    if (a[y][x] == 0)
      return i;
  }
  return -1;
}

void bfs(vector<vector<int>> a) {
  memo[a] = 0;
  queue<vector<vector<int>>> q;
  q.push(a);

  while (!q.empty()) {
    vector<vector<int>> v = q.front();
    q.pop();
    int cost = memo[v];

    // テァツゥツコテァツ卍ステ」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ィツソツ氾」ツ??
    int p = search(v);
    int x = p % 4, y = p / 4;
    // テァツゥツコテァツ卍ステ」ツ?ョテ、ツスツ催ァツスツョテ」ツ?ョテッツシツ氾ヲツ鳴ケテ・ツ青妥」ツ?ョテ、ツスツ催ァツスツョテ」ツ?ィテ、ツコツ、テヲツ渉崚」ツ?療」ツ?ヲテ」ツ?ソテ」ツつ?
    for (int i = 0; i < 4; i++) {
      int mx = x + dx[i], my = y + dy[i];
      if (mx < 0 || my < 0 || 4 <= mx || 2 <= my)
        continue;

      swap(v[y][x], v[my][mx]);
      if (!memo.count(
              v)) { // テ」ツ?セテ」ツ??ヲツ篠「テァツエツ「テ」ツ?療」ツ?ヲテ」ツ?ェテ」ツ?凝」ツ?」テ」ツ?淌」ツつ嘉」ツつュテ」ツδ・テ」ツδシテ」ツ?ォテ・ツ?・テ」ツつ古」ツつ?
        memo[v] = cost + 1;
        q.push(v);
      }
      swap(v[y][x], v[my][mx]);
    }
  }
}

int main() {
  {
    vector<vector<int>> a(2, vector<int>(4));
    for (int i = 0; i < 8; i++) {
      int x = i % 4, y = i / 4;
      a[y][x] = i;
    }
    // テ・ツケツ?・ツ?ェテ・ツ?暗ヲツ篠「テァツエツ「
    // (テヲツ慊?・ツ按敕」ツ?ォテ」ツ?凖」ツ?ケテ」ツ?ヲテ」ツ?ョティツァツ」テ」ツつ津ヲツアツづ」ツつ?」ツ?ヲテ」ツ?甘」ツ??
    bfs(a);
  }
  vector<vector<int>> a(2, vector<int>(4));

  while (cin >> a[0][0]) {
    for (int i = 1; i < 8; i++) {
      int x = i % 4, y = i / 4;
      cin >> a[y][x];
    }

    cout << memo[a] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

struct P {
  int x, y;
  P() {}
  P(int x_, int y_) {}
};

// テ・ツョツ古ヲツ按静」ツ?療」ツ?ヲテ」ツ??」ツつ凝」ツ?凝」ツ?ゥテ」ツ??」ツ??
bool is_goal(const vector<vector<int>> &a) {
  for (int i = 0; i < 8; i++) {
    int x = i % 4, y = i / 4;
    if (a[y][x] != i)
      return false;
  }
  return true;
}

// テァツゥツコテァツ卍ステ」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ィツソツ氾」ツ??
int search(const vector<vector<int>> &a) {
  for (int i = 0; i < 8; i++) {
    int x = i % 4, y = i / 4;
    if (a[y][x] == 0)
      return i;
  }
  return -1;
}

int bfs(vector<vector<int>> a) {
  // memo[a] := テ」ツδ妥」ツつコテ」ツδォテ」ツ?ョテァツ環カテヲツ??a
  // テ」ツ?セテ」ツ?ァテ」ツ?ョテヲツ可凝ヲツ閉ー
  map<vector<vector<int>>, int> memo;
  memo[a] = 0;
  queue<vector<vector<int>>> q;
  q.push(a);

  while (!q.empty()) {
    vector<vector<int>> v = q.front();
    q.pop();
    int cost = memo[v];

    if (is_goal(v))
      return cost;

    // テァツゥツコテァツ卍ステ」ツ?ョテ、ツスツ催ァツスツョテ」ツつ津ィツソツ氾」ツ??
    int p = search(v);
    int x = p % 4, y = p / 4;
    // テァツゥツコテァツ卍ステ」ツ?ョテ、ツスツ催ァツスツョテ」ツ?ョテッツシツ氾ヲツ鳴ケテ・ツ青妥」ツ?ョテ、ツスツ催ァツスツョテ」ツ?ィテ、ツコツ、テヲツ渉崚」ツ?療」ツ?ヲテ」ツ?ソテ」ツつ?
    for (int i = 0; i < 4; i++) {
      int mx = x + dx[i], my = y + dy[i];
      if (mx < 0 || my < 0 || 4 <= mx || 2 <= my)
        continue;

      swap(v[y][x], v[my][mx]);
      if (!memo.count(
              v)) { // テ」ツ?セテ」ツ??ヲツ篠「テァツエツ「テ」ツ?療」ツ?ヲテ」ツ?ェテ」ツ?凝」ツ?」テ」ツ?淌」ツつ嘉」ツつュテ」ツδ・テ」ツδシテ」ツ?ォテ・ツ?・テ」ツつ古」ツつ?
        memo[v] = cost + 1;
        q.push(v);
      }
      swap(v[y][x], v[my][mx]);
    }
  }
  return -1;
}

int main() {
  vector<vector<int>> a(2, vector<int>(4));

  while (cin >> a[0][0]) {
    for (int i = 1; i < 8; i++) {
      int x = i % 4, y = i / 4;
      cin >> a[y][x];
    }

    // テ・ツケツ?・ツ?ェテ・ツ?暗ヲツ篠「テァツエツ「
    cout << bfs(a) << endl;
  }
}