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

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

typedef pair<int, int> pii;
typedef pair<int, pii> pip;

int n;
int maps[10][10];
int cnt; // ???????????????????¨?
int m[4] = {0, 5, 9, 13};
int dx[13] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 2, 0, -2, 0};
int dy[13] = {0, 0, -1, 0, 1, -1, 1, 1, -1, 0, -2, 0, 2};
stack<pip> ans; // x, y, size

bool ismap(int x, int y) { return x >= 0 && x < 10 && y >= 0 && y < 10; }

// (x, y) ??? ????????? ?????¨???????????§?????§??????
int sim(int x, int y) {
  int res = 0;
  REP(i, 3) {
    FOR(j, m[i], m[i + 1]) {
      int nx = x + dx[j], ny = y + dy[j];
      if (!ismap(nx, ny) || !maps[ny][nx])
        return res;
    }
    res++;
  }
  return res;
}

// (x, y)?????????????????????s????????????k?¶????
void sum(int x, int y, int s, int k) {
  REP(i, m[s]) {
    int nx = x + dx[i], ny = y + dy[i];
    maps[ny][nx] += k;
  }
}

// (x, y) ????????¢?´¢ ?????? k ???
bool dfs(int x, int y, int k) {
  if (y == 10 && cnt == 0)
    return true;
  if (k < 0)
    return false;
  if (!ismap(x, y))
    return false;

  if (ismap(x - 2, y - 2) && maps[y - 2][x - 2])
    return false;

  int ms = sim(x, y);
  REP(i, ms) {
    sum(x, y, i + 1, -1);
    cnt -= m[i + 1];

    if (dfs(x, y, k - 1)) {
      ans.push(pip(x, pii(y, i + 1)));
      return true;
    }

    cnt += m[i + 1];
    sum(x, y, i + 1, 1);
  }

  int tmp = y * 10 + x + 1;
  if (dfs(tmp % 10, tmp / 10, k))
    return true;

  return false;
}

int main() {
  cin >> n;
  REP(i, 10) REP(j, 10) {
    scanf("%d", &maps[i][j]);
    cnt += maps[i][j];
  }

  dfs(0, 0, n);

  while (!ans.empty()) {
    pip p = ans.top();
    ans.pop();
    printf("%d %d %d\\n", p.first, p.second.first, p.second.second);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

typedef pair<int, int> pii;
typedef pair<int, pii> pip;

int n;
int maps[10][10];
int cnt; // ???????????????????¨?
int m[3] = {5, 9, 13};
stack<pip> ans; // x, y, size

bool ismap(int x, int y) { return x >= 0 && x < 10 && y >= 0 && y < 10; }

// (x, y) ??? ????????? ?????¨???????????§?????§??????
int sim(int x, int y) {
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  int nx, ny;
  int res = 0;

  if (!maps[y][x])
    return false;

  REP(i, 4) {
    nx = x + dx[i];
    ny = y + dy[i];
    if (!ismap(nx, ny) || !maps[ny][nx])
      return res;
  }
  res++;

  REP(i, 2) REP(j, 2) {
    nx = x + (j == 0 ? -1 : 1);
    ny = y + (i == 0 ? -1 : 1);
    if (!ismap(nx, ny) || !maps[ny][nx])
      return res;
  }
  res++;

  REP(i, 4) {
    nx = x + dx[i] * 2;
    ny = y + dy[i] * 2;
    if (!ismap(nx, ny) || !maps[ny][nx])
      return res;
  }
  res++;

  return res;
}

// (x, y)?????????????????????s????????????k?¶????
void sum(int x, int y, int s, int k) {
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  int nx, ny;

  switch (s) {
  case 3:
    REP(i, 4) {
      nx = x + dx[i] * 2;
      ny = y + dy[i] * 2;
      maps[ny][nx] += k;
    }
  case 2:
    REP(i, 2) REP(j, 2) {
      nx = x + (j == 0 ? -1 : 1);
      ny = y + (i == 0 ? -1 : 1);
      maps[ny][nx] += k;
    }
  case 1:
    REP(i, 4) {
      nx = x + dx[i];
      ny = y + dy[i];
      maps[ny][nx] += k;
    }
  }
}

// (x, y) ????????¢?´¢ ?????? k ???
bool dfs(int x, int y, int k) {
  if (k == 0)
    return cnt == 0;
  if (k * m[2] < cnt)
    return false;

  int ms = sim(x, y);
  REP(i, ms) {
    sum(x, y, i + 1, -1);
    cnt -= m[i];

    if (dfs(x, y, k - 1)) {
      ans.push(pip(x, pii(y, i + 1)));
      return true;
    }

    cnt += m[i];
    sum(x, y, i + 1, 1);
  }

  int tmp = y * 10 + x + 1;
  if (tmp == 100)
    return false;
  if (dfs(tmp % 10, tmp / 10, k))
    return true;

  return false;
}

int main() {
  cin >> n;
  REP(i, 10) REP(j, 10) {
    scanf("%d", &maps[i][j]);
    cnt += maps[i][j];
  }

  dfs(0, 0, n);

  while (!ans.empty()) {
    pip p = ans.top();
    ans.pop();
    printf("%d %d %d\\n", p.first, p.second.first, p.second.second);
  }

  return 0;
}