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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
typedef pair<P, int> PP;
const int H = 10;
const int W = 10;

int N, l[3] = {5, 9, 13};
int my[3][13] = {{-1, 0, 0, 0, 1},
                 {-1, -1, -1, 0, 0, 0, 1, 1, 1},
                 {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2}};
int mx[3][13] = {{0, -1, 0, 1, 0},
                 {-1, 0, 1, -1, 0, 1, -1, 0, 1},
                 {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0}};
int sy[3] = {1, 1, 2};
int sx[3] = {0, 1, 0};

vector<PP> ans;

bool check(vector<vector<int>> &v, int y, int x, int t) {
  REP(i, l[t]) {
    int ny = y + my[t][i], nx = x + mx[t][i];
    if (ny < 0 || nx < 0 || ny >= H || nx >= W || v[ny][nx] - 1 < 0)
      return false;
  }
  return true;
}

void add(vector<vector<int>> &v, int y, int x, int t, int pls) {
  REP(i, l[t]) {
    int ny = y + my[t][i], nx = x + mx[t][i];
    v[ny][nx] += pls;
  }
}

P findP(vector<vector<int>> &v) {
  REP(i, H) REP(j, W) if (v[i][j] > 0) return P(i, j);
  return P(-1, -1);
}

void dfs(int now, vector<vector<int>> &v, vector<PP> &ps) {
  P tar = findP(v);
  if (now > N || ans.size() > 0)
    return;
  if (tar.first == -1) {
    if (now == N)
      ans = ps;
    return;
  }
  int y = tar.first, x = tar.second;
  REP(t, 3) {
    if (check(v, y + sy[t], x + sx[t], t)) {
      add(v, y + sy[t], x + sx[t], t, -1);
      ps.push_back(PP(P(y + sy[t], x + sx[t]), t));
      dfs(now + 1, v, ps);
      add(v, y + sy[t], x + sx[t], t, 1);
      ps.pop_back();
    }
  }
}

int main() {
  cin >> N;
  vector<vector<int>> v(H, vector<int>(W));
  REP(i, H) REP(j, W) cin >> v[i][j];
  vector<PP> ps;
  dfs(0, v, ps);
  for (PP p : ans)
    cout << p.first.second << " " << p.first.first << " " << p.second + 1
         << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
typedef pair<P, int> PP;
const int H = 10;
const int W = 10;

int N, l[3] = {5, 9, 13};
int my[3][13] = {{-1, 0, 0, 0, 1},
                 {-1, -1, -1, 0, 0, 0, 1, 1, 1},
                 {-2, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 2}};
int mx[3][13] = {{0, -1, 0, 1, 0},
                 {-1, 0, 1, -1, 0, 1, -1, 0, 1},
                 {0, -1, 0, 1, -2, -1, 0, 1, 2, -1, 0, 1, 0}};

vector<PP> ans;

bool check(vector<vector<int>> &v, int y, int x, int t) {
  REP(i, l[t]) {
    int ny = y + my[t][i], nx = x + mx[t][i];
    if (ny < 0 || nx < 0 || ny >= H || nx >= W || v[ny][nx] - 1 < 0)
      return false;
  }
  return true;
}

void add(vector<vector<int>> &v, int y, int x, int t, int pls) {
  REP(i, l[t]) {
    int ny = y + my[t][i], nx = x + mx[t][i];
    v[ny][nx] += pls;
  }
}

bool isValid(vector<vector<int>> &v) {
  REP(i, H) REP(j, W) if (v[i][j] > 0) return false;
  return true;
}

void dfs(int now, vector<vector<int>> &v, vector<PP> &ps, int sy, int sx) {
  if (ans.size() > 0)
    return;
  if (now >= N) {
    if (isValid(v))
      ans = ps;
    return;
  }
  FOR(y, sy, H) {
    FOR(x, (y == sy ? sx : 0), W) {
      if (v[y][x] <= 0)
        continue;
      if (check(v, y - 1, x, 0)) {
        add(v, y - 1, x, 0, -1);
        ps.push_back(PP(P(y - 1, x), 0));
        dfs(now + 1, v, ps, y, x);
        ps.pop_back();
        add(v, y - 1, x, 0, 1);
      }
      if (check(v, y - 1, x - 1, 1)) {
        add(v, y - 1, x - 1, 1, -1);
        ps.push_back(PP(P(y - 1, x - 1), 1));
        dfs(now + 1, v, ps, y, x);
        ps.pop_back();
        add(v, y - 1, x - 1, 1, 1);
      }
      if (check(v, y - 2, x, 2)) {
        add(v, y - 2, x, 2, -1);
        ps.push_back(PP(P(y - 2, x), 2));
        dfs(now + 1, v, ps, y, x);
        ps.pop_back();
        add(v, y - 2, x, 2, 1);
      }
    }
  }
}

int main() {
  cin >> N;
  vector<vector<int>> v(H, vector<int>(W));
  REP(i, H) REP(j, W) cin >> v[i][j];
  vector<PP> ps;
  dfs(0, v, ps, 0, 0);
  for (PP p : ans)
    cout << p.first.second << " " << p.first.first << " " << p.second + 1
         << endl;
  return 0;
}