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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
map<mat, int> d;

int solve(mat A) {
  queue<mat> Q;
  Q.push(A);

  d[A] = 1;
  while (!Q.empty()) {
    mat a = Q.front();
    Q.pop();

    bool f = true;

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 8; j++) {
        if (a[i][j] != 0 && i * 10 + 10 + j + 1 != a[i][j]) {
          f = false;
        }
      }
    }
    if (f)
      return d[a] - 1;

    for (int i = 0; i < 4; i++) {
      for (int j = 1; j < 8; j++) {
        if (a[i][j] != 0)
          continue;

        mat b = a;
        int c = a[i][j - 1] + 1;

        int h = -1, w;
        for (int k = 0; k < 4; k++) {
          for (int l = 0; l < 8; l++) {
            if (b[k][l] == c) {
              h = k, w = l;
            }
          }
        }

        if (h == -1)
          continue;

        swap(b[h][w], b[i][j]);

        if (d[b] == 0) {
          d[b] = d[a] + 1;
          Q.push(b);
        }
      }
    }
  }
  return -1;
}

int main() {
  int Tc;
  cin >> Tc;
  while (Tc--) {
    d.clear();
    mat A(4, vec(8));
    for (int i = 0; i < 4; i++) {
      A[i][0] = i * 10 + 10 + 1;
      for (int j = 0; j < 7; j++) {
        cin >> A[i][j + 1];
        if (A[i][j + 1] % 10 == 1)
          A[i][j + 1] = 0;
      }
    }
    cout << solve(A) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

struct state {
  int t[4][8];
  int py[4], px[4];
  int qy[50], qx[50];
  int heuri;
};

int calcHeuri(state s) {
  int res = 0;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 8; j++) {
      if (s.t[i][j] == 0)
        continue;
      int y = s.t[i][j] / 10 - 1;
      int x = s.t[i][j] % 10 - 1;
      if (y != i || x != j)
        res++;
    }
  }
  return res;
}

int limit, flg;
state s, start;

bool dfs(int depth) {
  if (s.heuri == 0)
    return true;
  if (depth + s.heuri > limit) {
    flg = true;
    return false;
  }

  if (depth) {

    // cout<<depth<<" "<<s.heuri<<endl;
    /*
      for(int i=0;i<4;i++){
      for(int j=0;j<8;j++){
      printf("%3d",s.t[i][j]);
      }
      cout<<endl;
      }
      for(int i=0;i<4;i++)cout<<s.py[i]<<" "<<s.px[i]<<endl;
      cout<<endl;
    */
  }
  for (int i = 0; i < 4; i++) {
    int y = s.py[i], x = s.px[i];
    int num = s.t[y][x - 1] + 1;
    int ny = s.qy[num], nx = s.qx[num];
    if (num % 10 == 8 || num == 1)
      continue;
    swap(s.t[y][x], s.t[ny][nx]);
    s.qy[num] = y;
    s.qx[num] = x;
    s.py[i] = ny;
    s.px[i] = nx;

    if (num / 10 - 1 == y && num % 10 - 1 == x) {
      s.heuri--;
      if (dfs(depth + 1))
        return true;
      s.heuri++;
    } else if (num / 10 - 1 == ny && num % 10 - 1 == nx) {
      s.heuri++;
      if (dfs(depth + 1))
        return true;
      s.heuri--;
    } else {
      if (dfs(depth + 1))
        return true;
    }

    s.py[i] = y;
    s.px[i] = x;
    s.qy[num] = ny;
    s.qx[num] = nx;
    swap(s.t[y][x], s.t[ny][nx]);
  }
  return false;
}

void solve() {
  for (limit = start.heuri; limit <= 60; limit++) {
    s = start;
    flg = false;
    if (dfs(0)) {
      cout << limit << endl;
      return;
    }
    if (!flg)
      break;
  }
  cout << -1 << endl;
}

void init() {
  for (int i = 0; i < 4; i++) {
    start.py[i] = start.px[i] = 0;
    for (int j = 0; j < 8; j++)
      start.t[i][j] = 0;
  }
  for (int i = 0; i < 50; i++) {
    start.qy[i] = start.qx[i] = 0;
  }
}

int main() {
  int Tc;
  cin >> Tc;
  while (Tc--) {
    init();
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j < 8; j++) {
        int a;
        cin >> a;
        start.t[i][j] = a;
        if (a % 10 == 1) {
          start.py[a / 10 - 1] = i;
          start.px[a / 10 - 1] = j;
          start.t[i][j] = 0;
        }
        start.qy[a] = i;
        start.qx[a] = j;
      }
    }

    for (int i = 11; i <= 41; i += 10) {
      start.t[i / 10 - 1][0] = i;
      start.qy[i] = i / 10 - 1;
      start.qx[i] = 0;
    }
    start.heuri = calcHeuri(start);
    solve();
  }
  return 0;
}