Problem p00784 (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 complex<double> P;
const int M = 5;
const int INF = 1e9;

int N, ans = INF;

int count(vector<vector<int>> &v) {
  int ret = 0, j;
  REP(i, M) {
    for (j = (int)v[i].size() - 1; j >= 0 && v[i][j] == -1; --j)
      ;
    ret = max(ret, j);
  }
  return ret + 1;
}

bool check(int x, int s, vector<vector<int>> &v, vector<vector<int>> &t) {
  REP(i, M) REP(j, N) if (t[i][j] && v[i][j + x + s] != -1) return false;
  return true;
}

void dfs(int n, int x, vector<vector<int>> &v, vector<vector<int>> &t) {
  if (n >= 10)
    if (x + N < ans)
      ans = x + N;
  if (x + N >= ans)
    return;
  FOR(_, 1, N + 1) {
    if (!check(x, _, v, t))
      continue;
    REP(i, M) REP(j, N) if (t[i][j]) v[i][j + x + _] = n;
    dfs(n + 1, x + _, v, t);
    REP(i, M) REP(j, N) if (t[i][j]) v[i][j + x + _] = -1;
  }
}

int main() {
  while (cin >> N && N) {
    ans = INF;
    vector<vector<int>> t(M, vector<int>(N)), v(M, vector<int>(N * 12, -1));
    REP(i, M) {
      REP(j, N) {
        char c;
        cin >> c;
        t[i][j] = (c == 'X' ? 1 : 0);
        if (t[i][j])
          v[i][j] = 0;
      }
    }
    dfs(1, 0, v, t);
    cout << ans << 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 complex<double> P;
const int M = 5;
const int INF = 1e9;

int N;

int count(vector<vector<int>> &v) {
  int ret = 0, j;
  REP(i, M) {
    for (j = (int)v[i].size() - 1; j >= 0 && v[i][j] == -1; --j)
      ;
    ret = max(ret, j);
  }
  return ret + 1;
}

bool check(int x, int s, vector<vector<int>> &v, vector<vector<int>> &t) {
  REP(i, M) REP(j, N) if (t[i][j] && v[i][j + x + s] != -1) return false;
  return true;
}

int dfs(int n, int x, vector<vector<int>> &v, vector<vector<int>> &t) {
  // cout <<"------------" <<endl;
  // cout <<n <<", " <<x <<", " <<endl;
  // REP(i, v.size()){
  //   REP(j, v[i].size()){
  //     if(v[i][j] != -1) cout <<v[i][j];
  //     else cout <<".";
  //   }
  //   cout <<endl;
  // }
  if (n >= 10)
    return count(v);
  int res = INF;
  FOR(_, 1, N + 1) {
    if (!check(x, _, v, t))
      continue;
    REP(i, M) REP(j, N) if (t[i][j]) v[i][j + x + _] = n;
    res = min(res, dfs(n + 1, x + _, v, t));
    REP(i, M) REP(j, N) if (t[i][j]) v[i][j + x + _] = -1;
  }
  return res;
}

int main() {
  while (cin >> N && N) {
    vector<vector<int>> t(M, vector<int>(N)), v(M, vector<int>(N * 12, -1));
    REP(i, M) {
      REP(j, N) {
        char c;
        cin >> c;
        t[i][j] = (c == 'X' ? 1 : 0);
        if (t[i][j])
          v[i][j] = 0;
      }
    }
    cout << dfs(1, 0, v, t) << endl;
  }
  return 0;
}