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

#include <stdio.h>
int t[10][10];
int v;
int p[12];
int q[12];
int r[12];
int dx[] = {1, 0, 0, -1, 1, 1, -1, -1, 2, -2, 0, 0};
int dy[] = {0, 1, -1, 0, 1, -1, 1, -1, 0, 0, 2, -2};
int solve(int a, int b, int c) {
  if (v < a * 5 || v > a * 13)
    return 0;
  if (a == 0) {
    if (v == 0)
      return 1;
    else
      return 0;
  }
  for (int i = b; i < 9; i++) {
    int j = 1;
    if (i == b)
      j = c;
    for (; j < 9; j++) {
      if (t[i][j]) {
        int V = 0;
        for (int k = 0; k < 12; k++) {
          if (i + dx[k] < 0 || i + dx[k] > 9)
            break;
          if (j + dy[k] < 0 || j + dy[k] > 9)
            break;
          if (t[i + dx[k]][j + dy[k]])
            V++;
          if (k == 3 && V == 0)
            return 0;
          if (k == 7 && V < 3)
            return 0;
          if (k == 11 && V == 12) {
            t[i][j]--;
            for (int l = 0; l < 12; l++)
              t[i + dx[l]][j + dy[l]]--;
            p[a - 1] = i;
            q[a - 1] = j;
            r[a - 1] = 3;
            v -= 13;
            int N = solve(a - 1, i, j);
            if (N)
              return 1;
            t[i][j]++;
            v += 13;
            for (int l = 0; l < 12; l++)
              t[i + dx[l]][j + dy[l]]++;
          }
          if (k == 11 && V < 4)
            return 0;
        }
        for (int k = 0; k < 8; k++) {
          if (!t[i + dx[k]][j + dy[k]])
            break;
          if (k == 7) {
            t[i][j]--;
            for (int l = 0; l < 8; l++)
              t[i + dx[l]][j + dy[l]]--;
            p[a - 1] = i;
            q[a - 1] = j;
            r[a - 1] = 2;
            v -= 9;
            int N = solve(a - 1, i, j);
            if (N)
              return 1;
            t[i][j]++;
            v += 9;
            for (int l = 0; l < 8; l++)
              t[i + dx[l]][j + dy[l]]++;
          }
        }
        for (int k = 0; k < 4; k++) {
          if (!t[i + dx[k]][j + dy[k]])
            break;
          if (k == 3) {
            t[i][j]--;
            for (int l = 0; l < 4; l++)
              t[i + dx[l]][j + dy[l]]--;
            p[a - 1] = i;
            q[a - 1] = j;
            r[a - 1] = 1;
            v -= 5;
            int N = solve(a - 1, i, j);
            if (N)
              return 1;
            t[i][j]++;
            v += 5;
            for (int l = 0; l < 4; l++)
              t[i + dx[l]][j + dy[l]]++;
          }
        }
      }
    }
  }
  return 0;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++) {
      scanf("%d", &t[i][j]);
      v += t[i][j];
    }
  solve(a, 1, 1);
  for (int i = 0; i < a; i++)
    printf("%d %d %d\\n", q[i], p[i], r[i]);
}

Error: TLE

Fixed submission:

#include <stdio.h>
int t[10][10];
int v;
int p[12];
int q[12];
int r[12];
int dx[] = {1, 0, 0, -1, 1, 1, -1, -1, 2, -2, 0, 0};
int dy[] = {0, 1, -1, 0, 1, -1, 1, -1, 0, 0, 2, -2};
int solve(int a, int b, int c) {
  if (v < a * 5 || v > a * 13)
    return 0;
  if (a == 0) {
    if (v == 0)
      return 1;
    else
      return 0;
  }
  for (int i = b; i < 9; i++) {
    int j = 1;
    if (i == b)
      j = c;
    for (; j < 9; j++) {
      if (t[i][j]) {
        int V = 0;
        for (int k = 0; k < 12; k++) {
          if (i + dx[k] < 0 || i + dx[k] > 9)
            break;
          if (j + dy[k] < 0 || j + dy[k] > 9)
            break;
          if (t[i + dx[k]][j + dy[k]])
            V++;
          if (k == 3 && V == 4) {
            t[i][j]--;
            for (int l = 0; l < 4; l++)
              t[i + dx[l]][j + dy[l]]--;
            p[a - 1] = i;
            q[a - 1] = j;
            r[a - 1] = 1;
            v -= 5;
            int N = solve(a - 1, i, j);
            if (N)
              return 1;
            t[i][j]++;
            v += 5;
            for (int l = 0; l < 4; l++)
              t[i + dx[l]][j + dy[l]]++;
          }
          if (k == 3 && V == 0)
            return 0;
          if (k == 7 && V == 8) {
            t[i][j]--;
            for (int l = 0; l < 8; l++)
              t[i + dx[l]][j + dy[l]]--;
            p[a - 1] = i;
            q[a - 1] = j;
            r[a - 1] = 2;
            v -= 9;
            int N = solve(a - 1, i, j);
            if (N)
              return 1;
            t[i][j]++;
            v += 9;
            for (int l = 0; l < 8; l++)
              t[i + dx[l]][j + dy[l]]++;
          }
          if (k == 7 && V < 3)
            return 0;
          if (k == 11 && V == 12) {
            t[i][j]--;
            for (int l = 0; l < 12; l++)
              t[i + dx[l]][j + dy[l]]--;
            p[a - 1] = i;
            q[a - 1] = j;
            r[a - 1] = 3;
            v -= 13;
            int N = solve(a - 1, i, j);
            if (N)
              return 1;
            t[i][j]++;
            v += 13;
            for (int l = 0; l < 12; l++)
              t[i + dx[l]][j + dy[l]]++;
          }
          if (k == 11 && V < 4)
            return 0;
        }
      }
    }
  }
  return 0;
}
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++) {
      scanf("%d", &t[i][j]);
      v += t[i][j];
    }
  solve(a, 1, 1);
  for (int i = 0; i < a; i++)
    printf("%d %d %d\\n", q[i], p[i], r[i]);
}