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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int x[14];
bool r[14][100];
bool tempai(int num, int jan, int chou, bool make) {
  for (int i = 0; i < 14; i++) {
    if (!r[i][num]) {
      if (jan == 1 && i < 13) {
        if (x[i] == x[i + 1] && !(r[i + 1][num])) {
          for (int j = 0; j < 14; j++) {
            r[j][num + 1] = r[j][num];
          }
          r[i][num + 1] = 1;
          r[i + 1][num + 1] = 1;
          make = tempai(num + 1, 0, chou, make);
        }
      }
      if (chou > 0 && i < 12) {
        if (x[i] == x[i + 1] && x[i + 1] == x[i + 2] && !(r[i + 1][num]) &&
            !(r[i + 2][num])) {
          for (int j = 0; j < 14; j++) {
            r[j][num + 1] = r[j][num];
          }
          r[i][num + 1] = 1;
          r[i + 1][num + 1] = 1;
          r[i + 2][num + 1] = 1;
          make = tempai(num + 1, jan, chou - 1, make);
        }
      }
      if (chou > 0 && x[i] < 8) {
        int a = -1, b = -1;
        for (int j = i + 1; j < 14; j++) {
          if (x[j] == x[i] + 1 && !(r[j][num])) {
            a = j;
          }
          if (x[j] == x[i] + 2 && !(r[j][num])) {
            b = j;
          }
        }
        if (a != -1 && b != -1) {
          for (int j = 0; j < 14; j++) {
            r[j][num + 1] = r[j][num];
          }
          r[i][num + 1] = 1;
          r[a][num + 1] = 1;
          r[b][num + 1] = 1;
          make = tempai(num + 1, jan, chou - 1, make);
        }
      }
    }
  }
  if (!(jan) && !(chou)) {
    make = true;
  }
  return make;
}
int main() {
  bool ant = false;
  for (;;) {
    int hai[14] = {0};
    vector<int> ans;
    for (int i = 0; i < 13; i++) {
      if (scanf("%1d", &hai[i]) == EOF) {
        ant = true;
        break;
      }
    }
    if (ant == true) {
      break;
    }
    for (int i = 1; i < 10; i++) {
      hai[13] = i;
      for (int j = 0; j < 14; j++) {
        x[j] = hai[j];
      }
      sort(x, x + 14);
      bool ax = false;
      for (int j = 0, r = -1, cou = 0; j < 14; j++) {
        if (r != x[j]) {
          r = x[j];
          cou = 1;
        } else {
          cou++;
        }
        if (cou > 4) {
          ax = true;
          break;
        }
      }
      if (!ax) {
        bool f = tempai(0, 1, 4, false);
        if (f) {
          ans.push_back(i);
        }
      }
    }
    if (ans.empty()) {
      cout << "0" << endl;
    } else {
      for (int i = 0; i < ans.size(); i++) {
        if (!i) {
          cout << ans[i];
        } else {
          cout << " " << ans[i];
        }
      }
      cout << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int x[14];
bool r[14][100];
bool tempai(int num, int jan, int chou, bool make) {
  for (int i = 0; i < 14; i++) {
    if (!r[i][num]) {
      if (jan == 1 && i < 13) {
        if (x[i] == x[i + 1] && !(r[i + 1][num])) {
          for (int j = 0; j < 14; j++) {
            r[j][num + 1] = r[j][num];
          }
          r[i][num + 1] = 1;
          r[i + 1][num + 1] = 1;
          make = tempai(num + 1, 0, chou, make);
        }
      }
      if (chou > 0 && i < 12) {
        if (x[i] == x[i + 1] && x[i + 1] == x[i + 2] && !(r[i + 1][num]) &&
            !(r[i + 2][num])) {
          for (int j = 0; j < 14; j++) {
            r[j][num + 1] = r[j][num];
          }
          r[i][num + 1] = 1;
          r[i + 1][num + 1] = 1;
          r[i + 2][num + 1] = 1;
          make = tempai(num + 1, jan, chou - 1, make);
        }
      }
      if (chou > 0 && x[i] < 8) {
        int a = -1, b = -1;
        for (int j = i + 1; j < 14; j++) {
          if (x[j] == x[i] + 1 && !(r[j][num])) {
            a = j;
          }
          if (x[j] == x[i] + 2 && !(r[j][num])) {
            b = j;
          }
        }
        if (a != -1 && b != -1) {
          for (int j = 0; j < 14; j++) {
            r[j][num + 1] = r[j][num];
          }
          r[i][num + 1] = 1;
          r[a][num + 1] = 1;
          r[b][num + 1] = 1;
          make = tempai(num + 1, jan, chou - 1, make);
        }
      }
    }
  }
  if (!(jan) && !(chou)) {
    make = true;
  }
  return make;
}
int main() {
  for (;;) {
    int hai[14] = {0};
    vector<int> ans;
    for (int i = 0; i < 13; i++) {
      if (scanf("%1d", &hai[i]) == EOF) {
        break;
      }
    }
    for (int i = 1; i < 10; i++) {
      hai[13] = i;
      for (int j = 0; j < 14; j++) {
        x[j] = hai[j];
      }
      sort(x, x + 14);
      bool ax = false;
      for (int j = 0, r = -1, cou = 0; j < 14; j++) {
        if (r != x[j]) {
          r = x[j];
          cou = 1;
        } else {
          cou++;
        }
        if (cou > 4) {
          ax = true;
          break;
        }
      }
      if (!ax) {
        bool f = tempai(0, 1, 4, false);
        if (f) {
          ans.push_back(i);
        }
      }
    }
    if (ans.empty()) {
      cout << "0" << endl;
    } else {
      for (int i = 0; i < ans.size(); i++) {
        if (!i) {
          cout << ans[i];
        } else {
          cout << " " << ans[i];
        }
      }
      cout << endl;
    }
  }
  return 0;
}