Problem p00091 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <numeric>

using namespace std;

int Y[12], X[12], S[12];
int g[10][10];
const int W = 13 * 12 + 1;
bool dp[13][W] = {true};

bool dfs(int y, int x, int s, int n) {
  int sum = accumulate(g[0], g[10], 0);
  if (!dp[n][sum])
    return false;
  if (n == 0)
    return true;
  for (int i = s; i >= 0; i--) {
    for (int j = y; j < 10; j++) {
      y = 0;
      for (int k = x; k < 10; k++) {
        x = 0;
        static int dy[][4] = {{-1, 0, 0, 1}, {-1, -1, 1, 1}, {-2, 0, 0, 2}};
        static int dx[][4] = {{0, -1, 1, 0}, {-1, 1, -1, 1}, {0, -2, 2, 0}};
        bool f = !g[j][k];
        for (int l = 0; l <= i; l++) {
          for (int m = 0; m < 4; m++) {
            int ny = j + dy[l][m];
            int nx = k + dx[l][m];
            f |= ny < 0 || 10 <= ny || nx < 0 || 10 <= nx || g[ny][nx] == 0;
          }
        }
        if (!f) {
          g[j][k]--;
          S[n - 1] = i;
          Y[n - 1] = j;
          X[n - 1] = k;
          for (int l = 0; l <= i; l++) {
            for (int m = 0; m < 4; m++) {
              int ny = j + dy[l][m];
              int nx = k + dx[l][m];
              g[ny][nx]--;
            }
          }
          if (dfs(j, k, i, n - 1))
            return true;
          g[j][k]++;
          for (int l = 0; l <= i; l++) {
            for (int m = 0; m < 4; m++) {
              int ny = j + dy[l][m];
              int nx = k + dx[l][m];
              g[ny][nx]++;
            }
          }
        }
      }
    }
  }
  return false;
}

int main() {
  for (int i = 0; i < 12; i++) {
    for (int j = 0; j < W; j++) {
      if (dp[i][j]) {
        int d[] = {5, 9, 13};
        for (int k = 0; k < 3; k++) {
          if (j + d[k] < W) {
            dp[i + 1][j + d[k]] = true;
          }
        }
      }
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> g[i][j];
    }
  }
  dfs(0, 0, 2, n);
  while (n--) {
    cout << X[n] << ' ' << Y[n] << ' ' << S[n] + 1 << endl;
  }
}

Fixed submission:
#include <iostream>
#include <numeric>

using namespace std;

int Y[12], X[12], S[12];
int g[10][10];
const int W = 13 * 12 + 1;
bool dp[13][W] = {true};

bool dfs(int y, int x, int s, int n) {
  int sum = accumulate(g[0], g[10], 0);
  if (!dp[n][sum])
    return false;
  if (n == 0)
    return true;
  for (int i = s; i >= 0; i--) {
    for (int j = y; j < 10; j++) {
      y = 0;
      for (int k = x; k < 10; k++) {
        x = 0;
        static int dy[][4] = {{-1, 0, 0, 1}, {-1, -1, 1, 1}, {-2, 0, 0, 2}};
        static int dx[][4] = {{0, -1, 1, 0}, {-1, 1, -1, 1}, {0, -2, 2, 0}};
        bool f = !g[j][k];
        for (int l = 0; l <= i; l++) {
          for (int m = 0; m < 4; m++) {
            int ny = j + dy[l][m];
            int nx = k + dx[l][m];
            f |= ny < 0 || 10 <= ny || nx < 0 || 10 <= nx || g[ny][nx] == 0;
          }
        }
        if (!f) {
          g[j][k]--;
          S[n - 1] = i;
          Y[n - 1] = j;
          X[n - 1] = k;
          for (int l = 0; l <= i; l++) {
            for (int m = 0; m < 4; m++) {
              int ny = j + dy[l][m];
              int nx = k + dx[l][m];
              g[ny][nx]--;
            }
          }
          if (dfs(j, k, i, n - 1))
            return true;
          g[j][k]++;
          for (int l = 0; l <= i; l++) {
            for (int m = 0; m < 4; m++) {
              int ny = j + dy[l][m];
              int nx = k + dx[l][m];
              g[ny][nx]++;
            }
          }
          if (i == 0)
            return false;
        }
      }
    }
  }
  return false;
}

int main() {
  for (int i = 0; i < 12; i++) {
    for (int j = 0; j < W; j++) {
      if (dp[i][j]) {
        int d[] = {5, 9, 13};
        for (int k = 0; k < 3; k++) {
          if (j + d[k] < W) {
            dp[i + 1][j + d[k]] = true;
          }
        }
      }
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      cin >> g[i][j];
    }
  }
  dfs(0, 0, 2, n);
  while (n--) {
    cout << X[n] << ' ' << Y[n] << ' ' << S[n] + 1 << endl;
  }
}