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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
using namespace std;

int d[4] = {1, -1, 4, -4};
map<string, int> memo;

void swap(string &s, int a, int b) {
  char temp = s[a];
  s[a] = s[b];
  s[b] = temp;
}

void bfs() {
  queue<pair<string, int>> q;
  q.push(make_pair("01234567", 0));

  pair<string, int> n;
  while (not q.empty()) {
    n = q.front();
    q.pop();
    rep(i, 4) {
      int np = n.second + d[i];
      if (n.second == 3 && np == 4)
        continue;
      if (n.second == 4 && np == 3)
        continue;
      if (np < 0 || np >= 8)
        continue;

      string cps = n.first;
      swap(cps, n.second, np);

      if (memo.count(cps) == 0) {
        q.push(make_pair(cps, np));
        memo[cps] = memo[n.first] + 1;
      }
    }
  }
}

int main() {
  memo["01234567"] = 0;
  bfs();
  // show(memo.size())
  string input;
  while (cin >> input) {
    range(i, 1, 8) {
      string temp;
      cin >> temp;
      input += temp;
    }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
const int INF = 100000000;
using namespace std;

struct point {
  int x, y;
};
int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
int h = 2, w = 4;
int ans = INF;
map<string, int> memo;

bool check(int m[2][4]) {
  int k = 0;
  rep(i, h) {
    rep(j, w) {
      if (m[i][j] != k)
        return 0;
      k++;
    }
  }
  return 1;
}

void fix(int m[2][4], bool v[2][4]) {}

void dfs(int m[2][4], point before, point p, int cnt) {
  string s;
  rep(i, 2) rep(j, 4) s += '0' + m[i][j];

  if (s == "01234567") {
    ans = min(ans, cnt);
    return;
  } else if (cnt == 28)
    return;

  if (memo[s] == 0) {
    memo[s] = cnt;
  } else {
    return;
  }

  rep(i, 4) {
    point next;
    next.x = p.x + dx[i];
    next.y = p.y + dy[i];
    if (next.x < 0 || next.x >= w || next.y < 0 || next.y >= h)
      continue;
    if (next.x == before.x && next.y == before.y)
      continue;

    int cpm[2][4];
    bool cpv[2][4];
    rep(i, 2) memcpy(cpm[i], m[i], sizeof(m[i]));
    swap(cpm[p.y][p.x], cpm[next.y][next.x]);
    dfs(cpm, p, next, cnt + 1);
  }
}

int main() {
  int inp;
  int m[2][4];
  while (cin >> m[0][0]) {
    memo.clear();
    ans = INF;
    point zero = point{0, 0};

    rep(i, 2) {
      rep(j, 4) {
        if (i + j == 0)
          continue;
        cin >> m[i][j];
        if (m[i][j] == 0)
          zero = point{j, i};
      }
    }

    dfs(m, zero, zero, 0);

    cout << ans << endl;
  }
}