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

#include <iostream>
#include <stdlib.h>
using namespace std;

#define r(a) for (int a = 0; a < 10; a++)

int N, x[15], y[15], s[15], c[10][10];

bool paint(int i, int j, int s, int x, bool v) {
  if (s == 1)
    for (int p = i - 1; p <= i + 1; p++)
      for (int q = j - 1; q <= j + 1; q++) {
        if (abs(p - i) + abs(q - j) == 2)
          continue;
        if (!v) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  if (s == 2)
    for (int p = i; p <= i + 2; p++)
      for (int q = j; q <= j + 2; q++) {
        if (!v) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  if (s == 3)
    for (int p = i - 2; p <= i + 2; p++)
      for (int q = j - 2; q <= j + 2; q++) {
        if (abs(p - i) + abs(q - j) > 2)
          continue;
        if (!v) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  return 1;
}
bool dfs(int n) {
  if (n == N) {
    r(i) r(j) if (c[i][j]) return 0;
    for (int i = 0; i < n; i++)
      cout << x[i] << " " << y[i] << " " << s[i] << endl;
    return 1;
  }
  r(i) r(j) {
    if (!c[i][j])
      continue;
    if (j > 0 && j < 9 && i < 8)
      if (paint(i + 1, j, 1, 0, 0)) {
        paint(i + 1, j, 1, -1, 1);
        x[n] = j, y[n] = i + 1, s[n] = 1;
        if (dfs(n + 1))
          return 1;
        paint(i + 1, j, 1, 1, 1);
      }
    if (i < 8 && j < 8)
      if (paint(i, j, 2, 0, 0)) {
        paint(i, j, 2, -1, 1);
        x[n] = j + 1, y[n] = i + 1, s[n] = 2;
        if (dfs(n + 1))
          return 1;
        paint(i, j, 2, 1, 1);
      }
    if (j > 1 && j < 8 && i < 6)
      if (paint(i + 2, j, 3, 0, 0)) {
        paint(i + 2, j, 3, -1, 1);
        x[n] = j, y[n] = i + 2, s[n] = 3;
        if (dfs(n + 1))
          return 1;
        paint(i + 2, j, 3, 1, 1);
      }
    return 0;
  }
}
int main() {
  cin >> N;
  r(a) r(b) cin >> c[a][b];
  dfs(0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
using namespace std;

#define r(a) for (int a = 0; a < 10; a++)

int N, x[15], y[15], s[15], c[10][10];

bool paint(int i, int j, int s, int x, int v) {
  if (s == 1)
    for (int p = i - 1; p <= i + 1; p++)
      for (int q = j - 1; q <= j + 1; q++) {
        if (abs(p - i) + abs(q - j) == 2)
          continue;
        if (v == 0) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  if (s == 2)
    for (int p = i; p <= i + 2; p++)
      for (int q = j; q <= j + 2; q++) {
        if (v == 0) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  if (s == 3)
    for (int p = i - 2; p <= i + 2; p++)
      for (int q = j - 2; q <= j + 2; q++) {
        if (abs(p - i) + abs(q - j) > 2)
          continue;
        if (v == 0) {
          if (!c[p][q])
            return 0;
        } else
          c[p][q] += x;
      }
  return 1;
}
bool dfs(int n) {
  if (n == N) {
    r(i) r(j) if (c[i][j]) return 0;
    for (int i = 0; i < n; i++)
      cout << x[i] << " " << y[i] << " " << s[i] << endl;
    return 1;
  }
  r(i) r(j) {
    if (!c[i][j])
      continue;
    if (j > 0 && j < 9 && i < 8)
      if (paint(i + 1, j, 1, 0, 0)) {
        paint(i + 1, j, 1, -1, 1);
        x[n] = j, y[n] = i + 1, s[n] = 1;
        if (dfs(n + 1))
          return 1;
        paint(i + 1, j, 1, 1, 1);
      }
    if (i < 8 && j < 8)
      if (paint(i, j, 2, 0, 0)) {
        paint(i, j, 2, -1, 1);
        x[n] = j + 1, y[n] = i + 1, s[n] = 2;
        if (dfs(n + 1))
          return 1;
        paint(i, j, 2, 1, 1);
      }
    if (j > 1 && j < 8 && i < 6)
      if (paint(i + 2, j, 3, 0, 0)) {
        paint(i + 2, j, 3, -1, 1);
        x[n] = j, y[n] = i + 2, s[n] = 3;
        if (dfs(n + 1))
          return 1;
        paint(i + 2, j, 3, 1, 1);
      }
  }
  return 0;
}
int main() {
  cin >> N;
  r(a) r(b) cin >> c[a][b];
  dfs(0);
}