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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
struct dr {
  int y, x, d, t;
  dr() {}
  dr(int y, int x, int d, int t) : y(y), x(x), d(d), t(t) {}
};
int ans = 6;
int h = 4, w = 4;

void dfs(int d, mat v) {
  if (ans <= d + 1)
    return;
  if (d >= 5)
    return;
  /*
    cout<<d<<":"<<endl;
    for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
    cout<<v[i][j]<<" \\n"[j==w-1];
  */
  int ax[] = {1, 0, -1, 0};
  int ay[] = {0, 1, 0, -1};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!v[i][j])
        continue;
      mat u(v);
      u[i][j]++;
      queue<dr> qd;
      int tmp = -1;
      while (1) {
        if (!qd.empty()) {
          dr t = qd.front();
          qd.pop();
          if (tmp < t.t) {
            for (int a = 0; a < h; a++)
              for (int b = 0; b < w; b++)
                if (u[a][b] >= 5)
                  for (int k = 0; k < 4; k++) {
                    qd.push(dr(a, b, k, t.t));
                    u[a][b] = 0;
                  }
            tmp = t.t;
          }
          t.y += ay[t.d];
          t.x += ax[t.d];
          t.t++;
          if (t.y < 0 || h <= t.y || t.x < 0 || w <= t.x)
            continue;
          if (u[t.y][t.x])
            u[t.y][t.x]++;
          else
            qd.push(t);
        } else {
          tmp++;
          bool fl = 0;
          for (int a = 0; a < h; a++)
            for (int b = 0; b < w; b++)
              if (u[a][b] >= 5)
                for (int k = 0; k < 4; k++) {
                  qd.push(dr(a, b, k, tmp));
                  u[a][b] = 0;
                  fl = 1;
                }
          if (!fl)
            break;
        }
      }
      bool f = 0;
      for (int a = 0; a < h; a++)
        for (int b = 0; b < w; b++)
          f |= u[a][b];
      if (!f) {
        ans = d + 1;
        return;
      }
      dfs(d + 1, u);
    }
  }
}

signed main() {
  mat v(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> v[i][j];
  bool f = 0;
  for (int a = 0; a < h; a++)
    for (int b = 0; b < w; b++)
      f |= v[a][b];
  if (!f) {
    cout << 0 << endl;
    return 0;
  }
  dfs(0, v);
  cout << (ans > 5 ? -1 : ans) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
struct dr {
  int y, x, d, t;
  dr() {}
  dr(int y, int x, int d, int t) : y(y), x(x), d(d), t(t) {}
};
int ans = 6;
int h = 4, w = 4;

void dfs(int d, mat v) {
  if (ans <= d)
    return;
  bool f = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      f |= v[i][j];
  if (!f) {
    ans = d;
    return;
  }
  if (d > 5)
    return;
  /*
    cout<<d<<":"<<endl;
    for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
    cout<<v[i][j]<<" \\n"[j==w-1];
  */
  int ax[] = {1, 0, -1, 0};
  int ay[] = {0, 1, 0, -1};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!v[i][j])
        continue;
      mat u(v);
      u[i][j]++;
      queue<dr> qd;
      int tmp = -1;
      while (1) {
        if (!qd.empty()) {
          dr t = qd.front();
          qd.pop();
          if (tmp < t.t) {
            for (int a = 0; a < h; a++)
              for (int b = 0; b < w; b++)
                if (u[a][b] >= 5)
                  for (int k = 0; k < 4; k++) {
                    qd.push(dr(a, b, k, t.t));
                    u[a][b] = 0;
                  }
            tmp = t.t;
          }
          t.y += ay[t.d];
          t.x += ax[t.d];
          t.t++;
          if (t.y < 0 || h <= t.y || t.x < 0 || w <= t.x)
            continue;
          if (u[t.y][t.x])
            u[t.y][t.x]++;
          else
            qd.push(t);
        } else {
          tmp++;
          bool fl = 0;
          for (int a = 0; a < h; a++)
            for (int b = 0; b < w; b++)
              if (u[a][b] >= 5)
                for (int k = 0; k < 4; k++) {
                  qd.push(dr(a, b, k, tmp));
                  u[a][b] = 0;
                  fl = 1;
                }
          if (!fl)
            break;
        }
      }
      dfs(d + 1, u);
    }
  }
}

signed main() {
  mat v(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      cin >> v[i][j];
  dfs(0, v);
  cout << (ans > 5 ? -1 : ans) << endl;
  return 0;
}