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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<P, int> T;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
int N, res = 0;
vector<T> ans;
int ink[10][10];
int sx[5] = {-1, 0, 0, 0, 1};
int mx[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int lx[13] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2};
int sy[5] = {0, 1, -1, 0, 0};
int my[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int ly[13] = {0, 1, 0, -1, 2, 1, 0, -1, -2, 1, 0, -1, 0};
void ps(int x, int y) {
  for (int i = 0; i < 5; i++) {
    int nx = x + sx[i], ny = y + sy[i];
    ink[nx][ny]--;
  }
  res -= 5;
}
void psr(int x, int y) {
  for (int i = 0; i < 5; i++) {
    int nx = x + sx[i], ny = y + sy[i];
    ink[nx][ny]++;
  }
  res += 5;
}
bool psc(int x, int y) {
  for (int i = 0; i < 5; i++) {
    int nx = x + sx[i], ny = y + sy[i];
    if (ink[nx][ny] == 0)
      return false;
  }
  return true;
}
void pm(int x, int y) {
  for (int i = 0; i < 9; i++) {
    int nx = x + mx[i], ny = y + my[i];
    ink[nx][ny]--;
  }
  res -= 9;
}
void pmr(int x, int y) {
  for (int i = 0; i < 9; i++) {
    int nx = x + mx[i], ny = y + my[i];
    ink[nx][ny]++;
  }
  res += 9;
}
bool pmc(int x, int y) {
  for (int i = 0; i < 9; i++) {
    int nx = x + mx[i], ny = y + my[i];
    if (ink[nx][ny] == 0)
      return false;
  }
  return true;
}
void pl(int x, int y) {
  for (int i = 0; i < 13; i++) {
    int nx = x + lx[i], ny = y + ly[i];
    ink[nx][ny]--;
  }
  res -= 13;
}
void plr(int x, int y) {
  for (int i = 0; i < 13; i++) {
    int nx = x + lx[i], ny = y + ly[i];
    ink[nx][ny]++;
  }
  res += 13;
}
bool plc(int x, int y) {
  for (int i = 0; i < 13; i++) {
    int nx = x + lx[i], ny = y + ly[i];
    if (ink[nx][ny] == 0)
      return false;
  }
  return true;
}
bool dfs(int d) {
  if (res > 13 * (N - d))
    return false;
  if (d == N) {
    if (res == 0)
      return true;
    else
      return false;
  }
  int x, y;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (ink[i][j] > 0) {
        x = i, y = j;
        goto next;
      }
    }
  }
next:;
  if (psc(x + 1, y)) {
    ps(x + 1, y);
    bool f = dfs(d + 1);
    if (f)
      ans.pb(T(P(x + 1, y), 1));
    psr(x + 1, y);
    if (f)
      return true;
  }
  if (pmc(x + 1, y + 1)) {
    pm(x + 1, y + 1);
    bool f = dfs(d + 1);
    if (f)
      ans.pb(T(P(x + 1, y + 1), 2));
    pmr(x + 1, y + 1);
    if (f)
      return true;
  }
  if (plc(x + 2, y)) {
    pl(x + 2, y);
    bool f = dfs(d + 1);
    if (f)
      ans.pb(T(P(x + 2, y), 3));
    plr(x + 2, y);
    if (f)
      return true;
  }
  return false;
}
int main() {
  cin >> N;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++) {
      cin >> ink[i][j];
      res += ink[i][j];
    }
  dfs(0);
  for (int i = 0; i < N; i++)
    printf("%d %d %d\\n", ans[i].fi.sec, ans[i].fi.fi, ans[i].sec);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
int N;
bool found = false;
vector<int> ans;
int ink[10][10];
int sx[5] = {-1, 0, 0, 0, 1};
int mx[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int lx[13] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2};
int sy[5] = {0, 1, -1, 0, 0};
int my[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int ly[13] = {0, 1, 0, -1, 2, 1, 0, -1, -2, 1, 0, -1, 0};
void ps(int x, int y) {
  for (int i = 0; i < 5; i++) {
    int nx = x + sx[i], ny = y + sy[i];
    ink[nx][ny]--;
  }
}
void psr(int x, int y) {
  for (int i = 0; i < 5; i++) {
    int nx = x + sx[i], ny = y + sy[i];
    ink[nx][ny]++;
  }
}
bool psc(int x, int y) {
  for (int i = 0; i < 5; i++) {
    int nx = x + sx[i], ny = y + sy[i];
    if (ink[nx][ny] == 0)
      return false;
  }
  return true;
}
void pm(int x, int y) {
  for (int i = 0; i < 9; i++) {
    int nx = x + mx[i], ny = y + my[i];
    ink[nx][ny]--;
  }
}
void pmr(int x, int y) {
  for (int i = 0; i < 9; i++) {
    int nx = x + mx[i], ny = y + my[i];
    ink[nx][ny]++;
  }
}
bool pmc(int x, int y) {
  for (int i = 0; i < 9; i++) {
    int nx = x + mx[i], ny = y + my[i];
    if (ink[nx][ny] == 0)
      return false;
  }
  return true;
}
void pl(int x, int y) {
  for (int i = 0; i < 13; i++) {
    int nx = x + lx[i], ny = y + ly[i];
    ink[nx][ny]--;
  }
}
void plr(int x, int y) {
  for (int i = 0; i < 13; i++) {
    int nx = x + lx[i], ny = y + ly[i];
    ink[nx][ny]++;
  }
}
bool plc(int x, int y) {
  for (int i = 0; i < 13; i++) {
    int nx = x + lx[i], ny = y + ly[i];
    if (ink[nx][ny] == 0)
      return false;
  }
  return true;
}
bool check() {
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      if (ink[i][j] > 0)
        return false;
  return true;
}
void dfs(int d, vector<int> s) {
  if (found)
    return;
  if (d == N) {
    if (check()) {
      found = true;
      ans = s;
    }
    return;
  }
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      if (psc(i, j)) {
        s.pb(i);
        s.pb(j);
        s.pb(1);
        ps(i, j);
        dfs(d + 1, s);
        psr(i, j);
        s.ppb();
        s.ppb();
        s.ppb();
      }
    }
  }
  for (int i = 1; i <= 8; i++) {
    for (int j = 1; j <= 8; j++) {
      if (pmc(i, j)) {
        s.pb(i);
        s.pb(j);
        s.pb(2);
        pm(i, j);
        dfs(d + 1, s);
        pmr(i, j);
        s.ppb();
        s.ppb();
        s.ppb();
      }
    }
  }
  for (int i = 2; i <= 7; i++) {
    for (int j = 2; j <= 7; j++) {
      if (plc(i, j)) {
        s.pb(i);
        s.pb(j);
        s.pb(3);
        pl(i, j);
        dfs(d + 1, s);
        plr(i, j);
        s.ppb();
        s.ppb();
        s.ppb();
      }
    }
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      cin >> ink[i][j];
  vector<int> a;
  dfs(0, a);
  for (int i = 0; i < N; i++)
    printf("%d %d %d\\n", ans[3 * i + 1], ans[3 * i], ans[3 * i + 2]);
  return 0;
}