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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

int LC[10][10];
int L[10][10];
int F[10][10];

void init() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      L[i][j] = LC[i][j];
      F[i][j] = 0;
    }
  }
}

void input() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cin >> LC[i][j];
    }
  }
}

void output() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cout << F[i][j];
      if (j + 1 < 10)
        cout << " ";
    }
    cout << endl;
  }
}

void flip(int r, int c) {
  F[r][c] = 1;
  L[r][c] = !L[r][c];
  if (r - 1 >= 0)
    L[r - 1][c] = !L[r - 1][c];
  if (r + 1 < 10)
    L[r + 1][c] = !L[r + 1][c];
  if (c - 1 >= 0)
    L[r][c - 1] = !L[r][c - 1];
  if (c + 1 < 10)
    L[r][c + 1] = !L[r][c + 1];
}

bool solve(int r) {
  if (r >= 10) {
    for (int i = 0; i < 10; ++i) {
      if (L[r - 1][i] == 1)
        return false;
    }
    return true;
  }
  if (r == 0) {
    for (int i = 0; i < (1 << 10); ++i) {
      init();
      for (int j = 0; j < 10; ++j) {
        if (i & (1 << j))
          flip(r, j);
      }
      if (solve(r + 1))
        return true;
    }
    return 0;
  } else if (r < 10) {
    for (int i = 0; i < 10; ++i) {
      if (L[r - 1][i]) {
        F[r][i] = 1;
        flip(r, i);
      }
    }
    if (solve(r + 1))
      return true;
  }

  return false;
}

int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; ++i) {
      input();
      solve(0);
      output();
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

int L[10][10];
int F[10][10];
int R[10][10];

void init() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      F[i][j] = 0;
    }
  }
}

void input() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cin >> L[i][j];
    }
  }
}

bool check(int r, int c) {
  int b = L[r][c];
  b ^= F[r][c];
  if (r - 1 >= 0)
    b ^= F[r - 1][c];
  if (r + 1 < 10)
    b ^= F[r + 1][c];
  if (c - 1 >= 0)
    b ^= F[r][c - 1];
  if (c + 1 < 10)
    b ^= F[r][c + 1];
  return b == 1;
}

void output() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      cout << F[i][j];
      if (j + 1 < 10)
        cout << " ";
    }
    cout << endl;
  }
}

void flip(int r, int c) {
  int b = L[r][c];
  b ^= F[r][c];
  if (r - 1 >= 0)
    b ^= F[r - 1][c];
  if (r + 1 < 10)
    b ^= F[r + 1][c];
  if (c - 1 >= 0)
    b ^= F[r][c - 1];
  if (c + 1 < 10)
    b ^= F[r][c + 1];
  R[r][c] = b;
}

bool check() {
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      if (R[i][j])
        return false;
    }
  }
  return true;
}

bool solve(int r) {
  if (r >= 10) {
    for (int i = 0; i < 10; ++i) {
      for (int j = 0; j < 10; ++j) {
        flip(i, j);
      }
    }
    return check();
  }
  if (r == 0) {
    for (int i = 0; i < (1 << 10); ++i) {
      for (int j = 0; j < 10; ++j) {
        F[r][j] = i & (1 << j) ? 1 : 0;
      }
      if (solve(r + 1))
        return true;
    }
  } else if (r < 10) {
    for (int i = 0; i < (1 << 10); ++i) {
      for (int j = 0; j < 10; ++j) {
        F[r][j] = i & (1 << j) ? 1 : 0;
      }
      bool flag = true;
      for (int j = 0; j < 10; ++j) {
        if (check(r - 1, j))
          flag = false;
      }
      if (flag && solve(r + 1))
        return true;
    }
  }

  return false;
}

int main() {
  int n;
  while (cin >> n) {
    for (int i = 0; i < n; ++i) {
      init();
      input();
      solve(0);
      output();
    }
  }
  return 0;
}