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

#include <cstdio>
#include <vector>

using namespace std;

int H, W;

inline bool hantei(vector<vector<int>> tatami) {
  bool ok = true;

  for (int k = 0; k < H - 1; k++) {
    for (int l = 0; l < W - 1; l++) {
      int c1 = tatami[k][l];
      int c2 = tatami[k][l + 1];
      int c3 = tatami[k + 1][l];
      int c4 = tatami[k + 1][l + 1];

      bool ok2 = !(c1 > 0 && c2 > 0 && c3 > 0 && c4 > 0 && c1 != c2 &&
                   c1 != c3 && c1 != c4 && c2 != c3 && c2 != c4 && c3 != c4);

      ok = ok && ok2;
    }
  }

  return ok;
}

inline int dfs(int count_, vector<vector<int>> tatami) {
  if ((count_ - 1) * 2 == H * W) {
    return 1;
  }

  int sum = 0;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (!tatami[i][j]) {
        if (H - i - 1) {
          if (!tatami[i + 1][j]) {
            vector<vector<int>> tatami2(tatami.begin(), tatami.end());

            tatami2[i][j] = count_;
            tatami2[i + 1][j] = count_;

            if (!hantei(tatami2)) {
              return 0;
            }

            sum += dfs(count_ + 1, tatami2);
          }
        }

        if (W - j - 1) {
          if (!tatami[i][j + 1]) {
            vector<vector<int>> tatami2(tatami.begin(), tatami.end());

            tatami2[i][j] = count_;
            tatami2[i][j + 1] = count_;

            if (!hantei(tatami2)) {
              return 0;
            }

            sum += dfs(count_ + 1, tatami2);
          }
        }

        return sum;
      }
    }
  }

  return 0;
}

int main() {
  while (true) {
    scanf("%d", &H);
    scanf("%d", &W);

    if (H == 0 && W == 0) {
      break;
    }

    if (H % 2 == 1 && W % 2 == 1) {
      printf("0\\n");
    } else if (H == 1 || W == 1) {
      printf("1\\n");
    } else if (H == W) {
      printf("2\\n");
    } else {
      vector<vector<int>> start(H, vector<int>(W, 0));

      printf("%d\\n", dfs(1, start));
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>

using namespace std;

int H, W;

inline bool hantei(vector<vector<int>> tatami) {
  bool ok = true;

  for (int k = 0; k < H - 1; k++) {
    for (int l = 0; l < W - 1; l++) {
      int c1 = tatami[k][l];
      int c2 = tatami[k][l + 1];
      int c3 = tatami[k + 1][l];
      int c4 = tatami[k + 1][l + 1];

      bool ok2 = !((c1 * c2 * c3 * c4) && c1 != c2 && c1 != c3 && c1 != c4 &&
                   c2 != c3 && c2 != c4 && c3 != c4);

      ok = ok & ok2;
    }
  }

  return ok;
}

inline int dfs(int count_, vector<vector<int>> tatami) {
  if ((count_ - 1) * 2 == H * W) {
    return 1;
  }

  int sum = 0, starty = count_ / W;

  for (int i = starty; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (!tatami[i][j]) {
        if (H - i - 1) {
          if (!tatami[i + 1][j]) {
            vector<vector<int>> tatami2(tatami.begin(), tatami.end());

            tatami2[i][j] = count_;
            tatami2[i + 1][j] = count_;

            if (!hantei(tatami2)) {
              return 0;
            }

            sum += dfs(count_ + 1, tatami2);
          }
        }

        if (W - j - 1) {
          if (!tatami[i][j + 1]) {
            vector<vector<int>> tatami2(tatami.begin(), tatami.end());

            tatami2[i][j] = count_;
            tatami2[i][j + 1] = count_;

            if (!hantei(tatami2)) {
              return 0;
            }

            sum += dfs(count_ + 1, tatami2);
          }
        }

        return sum;
      }
    }
  }

  return 0;
}

int main() {
  while (true) {
    scanf("%d", &H);
    scanf("%d", &W);

    if (H == 0 && W == 0) {
      break;
    }

    if (H % 2 == 1 && W % 2 == 1) {
      printf("0");
    } else if (H == 1 || W == 1) {
      printf("1");
    } else {
      vector<vector<int>> start(H, vector<int>(W, 0));

      if (H == W) {
        start[0][0] = 1;
        start[0][1] = 1;

        printf("%d\\n", dfs(2, start) * 2);
      } else {
        printf("%d\\n", dfs(1, start));
      }
    }
  }

  return 0;
}