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

#include <algorithm>
#include <cstdio>

int N, map[10][10], dx[3] = {0, 1, 0}, dy[3] = {1, 1, 2};
int x[12], y[12], bl[12];

bool check(int x, int y, int t) {
  int u = 1;
  if (t > 0)
    u = 2;
  for (int i = -u; i <= u; i++) {
    for (int j = -u; j <= u; j++) {
      if (std::abs(i) + std::abs(j) > u)
        continue;
      if (t == 1 && (i == 0 && std::abs(j) == 2 || j == 0 && std::abs(i) == 2))
        continue;
      if (x + j >= 10 || y + i >= 10 || x + j < 0 || y + i < 0)
        return false;
      if (!map[y + i][x + j])
        return false;
    }
  }
  return true;
}

void ss(int x, int y, int t, int v) {
  // v???-????????????????????????+?????????????????????
  int u = 1;
  if (t > 0)
    u = 2;
  for (int i = -u; i <= u; i++) {
    for (int j = -u; j <= u; j++) {
      if (std::abs(i) + std::abs(j) > u)
        continue;
      if (t == 1 && (i == 0 && std::abs(j) == 2 || j == 0 && std::abs(i) == 2))
        continue;
      if (x + j >= 10 || y + i >= 10 || x + j < 0 || y + i < 0)
        continue;
      map[y + i][x + j] += v;
    }
  }
}

bool solve(int n) {
  if (n == N) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (map[i][j])
          return false;
      }
    }

    for (int i = 0; i < N; i++) {
      printf("%d %d %d\\n", x[i], y[i], bl[i]);
    }
    return true;
  }

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (!map[i][j])
        continue;

      for (int k = 0; k < 3; k++) {
        if (!check(j + dx[k], i + dy[k], k))
          continue;
        ss(j + dx[k], i + dy[k], k, -1); //???????????????

        x[n] = j + dx[k];
        y[n] = i + dy[k];
        bl[n] = k + 1;
        if (solve(n + 1))
          return true;
        ss(j + dx[k], i + dy[k], k, 1); //???????????????
      }
      return false;
    }
  }

  return false;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      scanf("%d", &map[i][j]);
    }
  }

  solve(0);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

int N, map[10][10];
int x[12], y[12], bl[12];
int bls[3][5][5] = {{{0, 0, 0, 0, 0},
                     {0, 0, 1, 0, 0},
                     {0, 1, 1, 1, 0},
                     {0, 0, 1, 0, 0},
                     {0, 0, 0, 0, 0}},
                    {{0, 0, 0, 0, 0},
                     {0, 1, 1, 1, 0},
                     {0, 1, 1, 1, 0},
                     {0, 1, 1, 1, 0},
                     {0, 0, 0, 0, 0}},
                    {{0, 0, 1, 0, 0},
                     {0, 1, 1, 1, 0},
                     {1, 1, 1, 1, 1},
                     {0, 1, 1, 1, 0},
                     {0, 0, 1, 0, 0}}};

bool check(int x, int y, int t) {
  int tmp, u = 0;
  if (t <= 1)
    u = 1;
  for (int i = -2 + u; i <= 2 - u; i++) {
    for (int j = -2 + u; j <= 2 - u; j++) {
      if (x + i >= 10 || y + j >= 10 || x + i < 0 || y + j < 0)
        continue;
      tmp = map[x + i][y + j] + bls[t][i + 2][j + 2] * (-1);

      if (tmp < 0)
        return false;
    }
  }
  return true;
}

void ss(int x, int y, int t, int v) {
  // v???-????????????????????????+?????????????????????
  int u = 0;
  if (t <= 1)
    u = 1;
  for (int i = -2 + u; i <= 2 - u; i++) {
    for (int j = -2 + u; j <= 2 - u; j++) {
      if (x + i >= 10 || y + j >= 10 || x + i < 0 || y + j < 0)
        continue;
      map[x + i][y + j] += bls[t][i + 2][j + 2] * v;
    }
  }
}

bool solve(int n) {
  if (n == N) {
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        if (map[i][j])
          return false;
      }
    }

    for (int i = 0; i < N; i++) {
      printf("%d %d %d\\n", x[i], y[i], bl[i]);
    }
    return true;
  }

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      if (!map[i][j])
        continue;

      for (int k = 0; k < 3; k++) {
        if (!check(i, j, k))
          continue;
        ss(i, j, k, -1); //???????????????

        x[n] = j;
        y[n] = i;
        bl[n] = k + 1;
        if (solve(n + 1))
          return true;
        ss(i, j, k, 1); //???????????????
      }
    }
  }

  return false;
}

int main() {
  scanf("%d", &N);
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      scanf("%d", &map[i][j]);
    }
  }

  solve(0);

  return 0;
}