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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vvi ta;
int n, m, out;
bool F() {
  bool h = true;
  rep(i, n - 1) rep(j, m - 1) {
    int a = ta[i][j], b = ta[i + 1][j], c = ta[i][j + 1], d = ta[i + 1][j + 1];
    if (a * b * c * d == 0)
      break;
    if (a == b || a == c || a == d || b == c || b == d || c == d)
      ;
    else
      h = false;
  }
  return h;
}
void f(int d, int a, int b) {
  if (a == n) {
    out += F();
    return;
  }
  if (b == m) {
    f(d, a + 1, 0);
    return;
  }

  if (ta[a][b]) {
    f(d, a, b + 1);
    return;
  }
  // tate
  if (a != n - 1 && ta[a + 1][b] == 0) {
    ta[a][b] = ta[a + 1][b] = d;
    if (F())
      f(d + 1, a, b + 1);
    ta[a][b] = ta[a + 1][b] = 0;
  }
  // yoko
  if (b != m - 1 && ta[a][b + 1] == 0) {
    ta[a][b] = ta[a][b + 1] = d;
    if (F())
      f(d + 1, a, b + 2);
    ta[a][b] = ta[a][b + 1] = 0;
  }
}
int main() {
  while (cin >> n >> m, n + m) {
    ta = vvi(n, vi(m));
    out = 0;
    f(1, 0, 0);
    cout << out << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
vvi ta;
int n, m, out;
bool F() {
  bool h = true;
  rep(i, n - 1) rep(j, m - 1) {
    int a = ta[i][j], b = ta[i + 1][j], c = ta[i][j + 1], d = ta[i + 1][j + 1];
    if (a == b || a == c || a == d || b == c || b == d || c == d)
      ;
    else
      h = false;
  }
  return h;
}
void f(int d, int a, int b) {
  if (a == n) {
    out += F();
    return;
  }
  if (b == m) {
    f(d, a + 1, 0);
    return;
  }
  if (ta[a][b]) {
    f(d, a, b + 1);
    return;
  }
  // tate
  if (a != n - 1 && ta[a + 1][b] == 0) {
    ta[a][b] = ta[a + 1][b] = d;
    f(d + 1, a, b + 1);
    ta[a][b] = ta[a + 1][b] = 0;
  }
  // yoko
  if (b != m - 1 && ta[a][b + 1] == 0) {
    ta[a][b] = ta[a][b + 1] = d;
    f(d + 1, a, b + 2);
    ta[a][b] = ta[a][b + 1] = 0;
  }
}
int main() {
  while (cin >> n >> m, n + m) {
    ta = vvi(n, vi(m));
    out = 0;
    f(1, 0, 0);
    cout << out << endl;
  }
}