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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
//  using i32=int;using i64=long long;using ll =i64;
//  using uint=unsigned int;using ull=unsigned long long;
//  template<typename T> using matrix=vector<vector<T> >;

#define BR "\\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

// config
// #undef NDEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp, act)
#define STOP(e)
#else
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) << endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x) << endl
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMPLN(exp);                                                               \\
    DEBUGLN(act);                                                              \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}

template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

string BITtoString(int bit) {
  stringstream ss;
  while (bit != 0) {
    ss << (bit % 2);
    bit /= 2;
  }
  string res = ss.str();
  reverse(ALL(res));
  return res;
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}

int H = 10, W = 10;
class Main {
public:
  Main(){};

  bool large(int x, int y) {
    for (int dx = -2; dx <= 2; dx++)
      for (int dy = -2; dy <= 2; dy++) {
        if (abs(dx) + abs(dy) > 2)
          continue;
        int px = x + dx, py = y + dy;
        if (!IN(0, px, W) || !IN(0, py, H))
          return false;
        if (board[py][px] == 0)
          return false;
      }
    return true;
  }
  bool mid(int x, int y) {
    for (int dx = -1; dx <= 1; dx++)
      for (int dy = -1; dy <= 1; dy++) {
        int px = x + dx, py = y + dy;
        if (!IN(0, px, W) || !IN(0, py, H))
          return false;
        if (board[py][px] == 0)
          return false;
      }
    return true;
  }
  bool small(int x, int y) {
    for (int dx = -1; dx <= 1; dx++)
      for (int dy = -1; dy <= 1; dy++) {
        if (abs(dx) + abs(dy) > 1)
          continue;
        int px = x + dx, py = y + dy;
        if (!IN(0, px, W) || !IN(0, py, H))
          return false;
        if (board[py][px] == 0)
          return false;
      }
    return true;
  }

  bool checkexprev(int poscode) {
    int x = poscode % W, y = poscode / W;
    REP(py, y - 2) {
      if (board[py][x] != 0)
        return true;
    }
    return false;
  }

  bool dfs(int poscode, int q) {
    if (q == 0) {
      REP(y, H) REP(x, W) if (board[y][x] != 0) return false;
      return true;
    }
    if (poscode == 100)
      return false; // over board
    if (checkexprev(poscode))
      return false;
    int next = poscode + 1;
    while (next < 100 && board[next / W][next % W] == 0)
      next++;
    if (dfs(next, q))
      return true; // move next

    int x = poscode % W, y = poscode / W;

    if (small(x, y)) {
      for (int dx = -1; dx <= 1; dx++)
        for (int dy = -1; dy <= 1; dy++) {
          if (abs(dx) + abs(dy) > 1)
            continue;
          int px = x + dx, py = y + dy;
          board[py][px]--;
        }
      if (dfs(poscode, q - 1)) {
        stringstream ss;
        ss << x << " " << y << " " << 1;
        res.push_back(ss.str());
        return true;
      }
      for (int dx = -1; dx <= 1; dx++)
        for (int dy = -1; dy <= 1; dy++) {
          if (abs(dx) + abs(dy) > 1)
            continue;
          int px = x + dx, py = y + dy;
          board[py][px]++;
        }
    } else
      return false;
    if (mid(x, y)) {
      for (int dx = -1; dx <= 1; dx++)
        for (int dy = -1; dy <= 1; dy++) {
          int px = x + dx, py = y + dy;
          board[py][px]--;
        }
      if (dfs(poscode, q - 1)) {
        stringstream ss;
        ss << x << " " << y << " " << 2;
        res.push_back(ss.str());
        return true;
      }
      for (int dx = -1; dx <= 1; dx++)
        for (int dy = -1; dy <= 1; dy++) {
          int px = x + dx, py = y + dy;
          board[py][px]++;
        }
    } else
      return false;

    if (large(x, y)) {
      for (int dx = -2; dx <= 2; dx++)
        for (int dy = -2; dy <= 2; dy++) {
          if (abs(dx) + abs(dy) > 2)
            continue;
          int px = x + dx, py = y + dy;
          board[py][px]--;
        }
      if (dfs(poscode, q - 1)) {
        stringstream ss;
        ss << x << " " << y << " " << 3;
        res.push_back(ss.str());
        return true;
      }
      for (int dx = -2; dx <= 2; dx++)
        for (int dy = -2; dy <= 2; dy++) {
          if (abs(dx) + abs(dy) > 2)
            continue;
          int px = x + dx, py = y + dy;
          board[py][px]++;
        }
    }
    return false;
  }

  vector<string> res;

  vector<vector<int>> board;
  void run() {
    int q;
    cin >> q;
    board = vector<vector<int>>(H, vector<int>(W));
    REP(y, H) REP(x, W) cin >> board[y][x];
    dfs(0, q);
    REP(i, q) cout << res[i] << endl;
  }
};
int main() {
  Main().run();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
//  using i32=int;using i64=long long;using ll =i64;
//  using uint=unsigned int;using ull=unsigned long long;
//  template<typename T> using matrix=vector<vector<T> >;

#define BR "\\n"
#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

// config
// #undef NDEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp, act)
#define STOP(e)
#else
#define DUMP(x) cerr << #x << " = " << (x)
#define DUMPLN(x) DUMP(x) << endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x) << endl
#define LINE() cerr << " (L" << __LINE__ << ")"
#define LINELN() LINE() << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMPLN(exp);                                                               \\
    DEBUGLN(act);                                                              \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}

template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

string BITtoString(int bit) {
  stringstream ss;
  while (bit != 0) {
    ss << (bit % 2);
    bit /= 2;
  }
  string res = ss.str();
  reverse(ALL(res));
  return res;
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}

int H = 10, W = 10;
class Main {
public:
  Main(){};

  bool large(int x, int y) {
    for (int dx = -2; dx <= 2; dx++)
      for (int dy = -2; dy <= 2; dy++) {
        if (abs(dx) + abs(dy) > 2)
          continue;
        int px = x + dx, py = y + dy;
        if (!IN(0, px, W) || !IN(0, py, H))
          return false;
        if (board[py][px] == 0)
          return false;
      }
    return true;
  }
  bool mid(int x, int y) {
    for (int dx = -1; dx <= 1; dx++)
      for (int dy = -1; dy <= 1; dy++) {
        int px = x + dx, py = y + dy;
        if (!IN(0, px, W) || !IN(0, py, H))
          return false;
        if (board[py][px] == 0)
          return false;
      }
    return true;
  }
  bool small(int x, int y) {
    for (int dx = -1; dx <= 1; dx++)
      for (int dy = -1; dy <= 1; dy++) {
        if (abs(dx) + abs(dy) > 1)
          continue;
        int px = x + dx, py = y + dy;
        if (!IN(0, px, W) || !IN(0, py, H))
          return false;
        if (board[py][px] == 0)
          return false;
      }
    return true;
  }

  bool dfs(int poscode, int q) {
    if (q == 0) {
      REP(y, H) REP(x, W) if (board[y][x] != 0) return false;
      return true;
    }
    if (poscode == 100)
      return false; // over board

    int next = poscode + 1;
    while (next < 100 && board[next / W][next % W] == 0)
      next++;
    if (dfs(next, q))
      return true; // move next

    int x = poscode % W, y = poscode / W;

    if (small(x, y)) {
      for (int dx = -1; dx <= 1; dx++)
        for (int dy = -1; dy <= 1; dy++) {
          if (abs(dx) + abs(dy) > 1)
            continue;
          int px = x + dx, py = y + dy;
          board[py][px]--;
        }
      if (dfs(poscode, q - 1)) {
        stringstream ss;
        ss << x << " " << y << " " << 1;
        res.push_back(ss.str());
        return true;
      }
      for (int dx = -1; dx <= 1; dx++)
        for (int dy = -1; dy <= 1; dy++) {
          if (abs(dx) + abs(dy) > 1)
            continue;
          int px = x + dx, py = y + dy;
          board[py][px]++;
        }

      if (mid(x, y)) {
        for (int dx = -1; dx <= 1; dx++)
          for (int dy = -1; dy <= 1; dy++) {
            int px = x + dx, py = y + dy;
            board[py][px]--;
          }
        if (dfs(poscode, q - 1)) {
          stringstream ss;
          ss << x << " " << y << " " << 2;
          res.push_back(ss.str());
          return true;
        }
        for (int dx = -1; dx <= 1; dx++)
          for (int dy = -1; dy <= 1; dy++) {
            int px = x + dx, py = y + dy;
            board[py][px]++;
          }
        if (large(x, y)) {
          for (int dx = -2; dx <= 2; dx++)
            for (int dy = -2; dy <= 2; dy++) {
              if (abs(dx) + abs(dy) > 2)
                continue;
              int px = x + dx, py = y + dy;
              board[py][px]--;
            }
          if (dfs(poscode, q - 1)) {
            stringstream ss;
            ss << x << " " << y << " " << 3;
            res.push_back(ss.str());
            return true;
          }
          for (int dx = -2; dx <= 2; dx++)
            for (int dy = -2; dy <= 2; dy++) {
              if (abs(dx) + abs(dy) > 2)
                continue;
              int px = x + dx, py = y + dy;
              board[py][px]++;
            }
        }
      }
    }
    return false;
  }

  vector<string> res;

  vector<vector<int>> board;
  void run() {
    int q;
    cin >> q;
    board = vector<vector<int>>(H, vector<int>(W));
    REP(y, H) REP(x, W) cin >> board[y][x];
    dfs(0, q);
    REP(i, q) cout << res[i] << endl;
  }
};
int main() {
  Main().run();
  return 0;
}