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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct Ink {
  int y, x, size;
  Ink() {}
  Ink(int _y, int _x, int _size) {
    y = _y;
    x = _x;
    size = _size;
  }
};

const int dy[3][4] = {-1, 0, 1, 0, -1, -1, 1, 1, -2, 0, 2, 0};
const int dx[3][4] = {0, 1, 0, -1, -1, 1, 1, -1, 0, 2, 0, -2};

int N;
bool found;

bool can(int fld[10][10], int y, int x, int size) {
  for (int k = 0; k < 4; k++) {
    int ny = y + dy[size][k], nx = x + dx[size][k];
    if (ny < 0 || 10 <= ny || nx < 0 || 10 <= nx || fld[ny][nx] == 0)
      return false;
  }
  return true;
}

void dye(int fld[10][10], int y, int x, int size, int v) {
  for (int k = 0; k < 4; k++) {
    int ny = y + dy[size][k], nx = x + dx[size][k];
    fld[ny][nx] += v;
  }
}

void dfs(int fld[10][10], vector<Ink> &ink, int y, int x) {
  if (found)
    return;
  bool white = true;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (fld[i][j] > 0 && i * 10 + j < (y * 10 + x) - 20)
        return;
      if (fld[i][j] > N - ink.size())
        return;
      if (fld[i][j] != 0) {
        white = false;
      }
    }
  }
  if (white && ink.size() == N) {
    for (int i = 0; i < ink.size(); i++) {
      printf("%d %d %d\\n", ink[i].x, ink[i].y, ink[i].size + 1);
      found = true;
    }
    return;
  }

  for (int i = y; i < 10; i++) {
    for (int j = (i == y) ? x : 0; j < 10; j++) {
      if (fld[i][j] == 0)
        continue;
      fld[i][j]--;
      int size;
      for (size = 0; size < 3; size++) {
        if (can(fld, i, j, size) == false)
          break;
        dye(fld, i, j, size, -1);
        ink.push_back(Ink(i, j, size));
        dfs(fld, ink, i, j);
        ink.pop_back();
      }
      for (int size_ = size; size--;) {
        dye(fld, i, j, size, 1);
      }
      fld[i][j]++;
    }
  }
}

int main() {
  int fld[10][10];
  cin >> N;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      cin >> fld[i][j];
  vector<Ink> ink;
  dfs(fld, ink, 0, 0);
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct Ink {
  int y, x, size;
  Ink() {}
  Ink(int _y, int _x, int _size) {
    y = _y;
    x = _x;
    size = _size;
  }
};

const int dy[3][4] = {-1, 0, 1, 0, -1, -1, 1, 1, -2, 0, 2, 0};
const int dx[3][4] = {0, 1, 0, -1, -1, 1, 1, -1, 0, 2, 0, -2};

int N;
bool found;

bool can(int fld[10][10], int y, int x, int size) {
  for (int k = 0; k < 4; k++) {
    int ny = y + dy[size][k], nx = x + dx[size][k];
    if (ny < 0 || 10 <= ny || nx < 0 || 10 <= nx || fld[ny][nx] == 0)
      return false;
  }
  return true;
}

void dye(int fld[10][10], int y, int x, int size, int v) {
  for (int k = 0; k < 4; k++) {
    int ny = y + dy[size][k], nx = x + dx[size][k];
    fld[ny][nx] += v;
  }
}

void dfs(int fld[10][10], vector<Ink> &ink, int y, int x) {
  if (found)
    return;
  bool white = true;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (fld[i][j] > N - ink.size())
        return;
      if (fld[i][j] != 0) {
        white = false;
      }
    }
  }
  if (white && ink.size() == N) {
    for (int i = 0; i < ink.size(); i++) {
      printf("%d %d %d\\n", ink[i].x, ink[i].y, ink[i].size + 1);
      found = true;
    }
    return;
  }

  for (int i = y; i < 10; i++) {
    for (int j = (i == y) ? x : 0; j < 10; j++) {
      if (fld[i][j] == 0)
        continue;
      fld[i][j]--;
      int size;
      for (size = 0; size < 3; size++) {
        if (can(fld, i, j, size) == false)
          break;
        dye(fld, i, j, size, -1);
        ink.push_back(Ink(i, j, size));
        dfs(fld, ink, i, j);
        ink.pop_back();
      }
      while (size--) {
        dye(fld, i, j, size, 1);
      }
      fld[i][j]++;
    }
  }
}

int main() {
  int fld[10][10];
  cin >> N;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      cin >> fld[i][j];
  vector<Ink> ink;
  dfs(fld, ink, 0, 0);
  return 0;
}