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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T> constexpr bool in_range(T y, T x, T H, T W) {
  return 0 <= y && y < H && 0 <= x && x < W;
}

typedef long long ll;
int const inf = 1 << 29;

const bool M[3][5][5] = {
    {
        {0, 0, 0, 0, 0},
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 0, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {1, 1, 1, 1, 1},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
    },
};

int G[10][10];

bool check(int k, int y, int x) {
  REP(i, -2, 3) REP(j, -2, 3) {
    if (M[k][2 + i][2 + j]) {
      if (!in_range(y + i, x + j, 10, 10))
        return false;
      if (!G[y + i][x + j])
        return false;
    }
  }
  return true;
}

void use(int k, int y, int x) {
  REP(i, -2, 3) REP(j, -2, 3) if (M[k][2 + i][2 + j]) G[y + i][x + j]--;
}

void revive(int k, int y, int x) {
  REP(i, -2, 3) REP(j, -2, 3) if (M[k][2 + i][2 + j]) G[y + i][x + j]++;
}

bool cleared() {
  bool ok = 1;
  rep(i, 10) rep(j, 10) { ok &= G[i][j] == 0; }
  return ok;
}

bool invalid(int y, int x) {
  if (y - 2 >= 0 && x - 2 >= 0 && G[y - 2][x - 2])
    return 1;
  return 0;
}

bool solve(int r, int y, int x) {
  if (y == 10)
    return r == 0 && cleared();
  if (invalid(y, x))
    return 0;
  if (r && G[y][x]) {
    rep(k, 3) {
      if (check(k, y, x)) {
        use(k, y, x);
        if (solve(r - 1, y, x)) {
          cout << x << ' ' << y << ' ' << k + 1 << endl;
          return 1;
        }
        revive(k, y, x);
      }
    }
  }
  return solve(r, y + (x == 9), (x + 1) % 10);
}

int main() {

  int N;
  cin >> N;
  rep(i, 10) rep(j, 10) { cin >> G[i][j]; }

  solve(N, 0, 0);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}
template <class T> constexpr bool in_range(T y, T x, T H, T W) {
  return 0 <= y && y < H && 0 <= x && x < W;
}

typedef long long ll;
int const inf = 1 << 29;

const bool M[3][5][5] = {
    {
        {0, 0, 0, 0, 0},
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 0, 0, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 1, 1, 1, 0},
        {0, 0, 0, 0, 0},
    },
    {
        {0, 0, 1, 0, 0},
        {0, 1, 1, 1, 0},
        {1, 1, 1, 1, 1},
        {0, 1, 1, 1, 0},
        {0, 0, 1, 0, 0},
    },
};

int G[10][10];

bool check(int k, int y, int x) {
  REP(i, -2, 3) REP(j, -2, 3) {
    if (M[k][2 + i][2 + j]) {
      if (!in_range(y + i, x + j, 10, 10))
        return false;
      if (!G[y + i][x + j])
        return false;
    }
  }
  return true;
}

void use(int k, int y, int x) {
  REP(i, -2, 3) REP(j, -2, 3) if (M[k][2 + i][2 + j]) G[y + i][x + j]--;
}

void revive(int k, int y, int x) {
  REP(i, -2, 3) REP(j, -2, 3) if (M[k][2 + i][2 + j]) G[y + i][x + j]++;
}

bool cleared() {
  bool ok = 1;
  rep(i, 10) rep(j, 10) { ok &= G[i][j] == 0; }
  return ok;
}

bool solve(int r, int y, int x) {
  if (y == 10)
    return r == 0 && cleared();
  if (r && G[y][x]) {
    rep(k, 3) {
      if (check(k, y, x)) {
        use(k, y, x);
        if (r && solve(r - 1, y, x)) {
          cout << x << ' ' << y << ' ' << k + 1 << endl;
          return 1;
        }
        revive(k, y, x);
      }
    }
  }
  return solve(r, y + (x == 9), (x + 1) % 10);
}

int main() {

  int N;
  cin >> N;
  rep(i, 10) rep(j, 10) { cin >> G[i][j]; }

  solve(N, 0, 0);

  return 0;
}