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

#include <cmath>
#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int n;
int a[10][10];
int res[3][12][2];
int ii, jj, kk;
int dy1[] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2};
int dx1[] = {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0};
int dy2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dx2[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dy3[] = {-1, 0, 0, 0, 1};
int dx3[] = {0, -1, 0, 1, 0};

bool solve(int i, int j, int k, int y, int x) {
  if (y == 10) {
    if (i == 0 && j == 0 && k == 0) {
      rep(t, ii) cout << res[0][t][1] << " " << res[0][t][0] << " 3" << endl;
      rep(t, jj) cout << res[1][t][1] << " " << res[1][t][0] << " 2" << endl;
      rep(t, kk) cout << res[2][t][1] << " " << res[2][t][0] << " 1" << endl;
      return true;
    } else {
      return false;
    }
  }
  if (x == 10)
    return solve(i, j, k, y + 1, 0);
  if (3 <= y && a[y - 3][x])
    return false;
  if (a[y][x] == 0)
    return solve(i, j, k, y, x + 1);
  if (solve(i, j, k, y, x + 1))
    return true;
  if (i > 0 && 2 <= y && y < 8 && 2 <= x && x < 8) {
    rep(t, 13) if (a[y + dy1[t]][x + dx1[t]] == 0) goto fail1;
    res[0][i - 1][0] = y;
    res[0][i - 1][1] = x;
    rep(t, 13)-- a[y + dy1[t]][x + dx1[t]];
    if (solve(i - 1, j, k, y, x))
      return true;
    rep(t, 13)++ a[y + dy1[t]][x + dx1[t]];
  }
fail1:;
  if (j > 0 && 1 <= y && y < 9 && 1 <= x && x < 9) {
    rep(t, 9) if (a[y + dy2[t]][x + dx2[t]] == 0) goto fail2;
    res[1][j - 1][0] = y;
    res[1][j - 1][1] = x;
    rep(t, 9)-- a[y + dy2[t]][x + dx2[t]];
    if (solve(i, j - 1, k, y, x))
      return true;
    rep(t, 9)++ a[y + dy2[t]][x + dx2[t]];
  }
fail2:;
  if (k > 0 && 1 <= y && y < 9 && 1 <= x && x < 9) {
    rep(t, 5) if (a[y + dy3[t]][x + dx3[t]] == 0) goto fail3;
    res[2][k - 1][0] = y;
    res[2][k - 1][1] = x;
    rep(t, 5)-- a[y + dy3[t]][x + dx3[t]];
    if (solve(i, j, k - 1, y, x))
      return true;
    rep(t, 5)++ a[y + dy3[t]][x + dx3[t]];
  }
fail3:;
  return false;
}

int main() {
  cin >> n;
  rep(i, 10) rep(j, 10) cin >> a[i][j];
  int cnt = 0;
  rep(i, 10) rep(j, 10) cnt += a[i][j];
  rep(i, n + 1) rep(j, n + 1) rep(k, n + 1) {
    if (i + j + k != n)
      continue;
    if (i * 13 + j * 9 + k * 5 != cnt)
      continue;
    ii = i;
    jj = j;
    kk = k;
    if (solve(i, j, k, 0, 0))
      return 0;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int n;
int a[10][10];
int res[3][12][2];
int ii, jj, kk;
int dy1[] = {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2};
int dx1[] = {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0};
int dy2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dx2[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
int dy3[] = {-1, 0, 0, 0, 1};
int dx3[] = {0, -1, 0, 1, 0};

bool solve(int i, int j, int k, int y, int x) {
  if (y == 10) {
    if (i == 0 && j == 0 && k == 0) {
      rep(t, ii) cout << res[0][t][1] << " " << res[0][t][0] << " 3" << endl;
      rep(t, jj) cout << res[1][t][1] << " " << res[1][t][0] << " 2" << endl;
      rep(t, kk) cout << res[2][t][1] << " " << res[2][t][0] << " 1" << endl;
      return true;
    } else {
      return false;
    }
  }
  if (x == 10)
    return solve(i, j, k, y + 1, 0);
  if (a[y][x] == 0)
    return solve(i, j, k, y, x + 1);
  if (solve(i, j, k, y, x + 1))
    return true;
  if (i > 0 && 2 <= y && y < 8 && 2 <= x && x < 8) {
    rep(t, 13) if (a[y + dy1[t]][x + dx1[t]] == 0) goto fail1;
    res[0][i - 1][0] = y;
    res[0][i - 1][1] = x;
    rep(t, 13)-- a[y + dy1[t]][x + dx1[t]];
    if (solve(i - 1, j, k, y, x))
      return true;
    rep(t, 13)++ a[y + dy1[t]][x + dx1[t]];
  }
fail1:;
  if (j > 0 && 1 <= y && y < 9 && 1 <= x && x < 9) {
    rep(t, 9) if (a[y + dy2[t]][x + dx2[t]] == 0) goto fail2;
    res[1][j - 1][0] = y;
    res[1][j - 1][1] = x;
    rep(t, 9)-- a[y + dy2[t]][x + dx2[t]];
    if (solve(i, j - 1, k, y, x))
      return true;
    rep(t, 9)++ a[y + dy2[t]][x + dx2[t]];
  }
fail2:;
  if (k > 0 && 1 <= y && y < 9 && 1 <= x && x < 9) {
    rep(t, 5) if (a[y + dy3[t]][x + dx3[t]] == 0) goto fail3;
    res[2][k - 1][0] = y;
    res[2][k - 1][1] = x;
    rep(t, 5)-- a[y + dy3[t]][x + dx3[t]];
    if (solve(i, j, k - 1, y, x))
      return true;
    rep(t, 5)++ a[y + dy3[t]][x + dx3[t]];
  }
fail3:;
  return false;
}

int main() {
  cin >> n;
  rep(i, 10) rep(j, 10) cin >> a[i][j];
  int cnt = 0;
  rep(i, 10) rep(j, 10) cnt += a[i][j];
  rep(i, n + 1) rep(j, n + 1) rep(k, n + 1) {
    if (i + j + k != n)
      continue;
    if (i * 13 + j * 9 + k * 5 != cnt)
      continue;
    ii = i;
    jj = j;
    kk = k;
    if (solve(i, j, k, 0, 0))
      return 0;
  }
}