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

#include <algorithm>
#include <iostream>
#include <string>

#define NO 100000
#define NO2 1000000000
using namespace std;

int puz[14];

bool solve(int Spuz[]) {
  int Bpuz[14];
  int tmp[3];
  bool f = true;
  for (int i = 0; i < 14; i++) {
    Bpuz[i] = Spuz[i];
    if (Bpuz[i] != NO && Bpuz[i] != NO2)
      f = false;
  }
  if (f) {
    return true;
  }
  sort(Bpuz, Bpuz + 14);
  if (f == false) {
    if (Bpuz[0] == Bpuz[1] && Bpuz[1] == Bpuz[2]) {
      for (int j = 0; j < 3; j++) {
        tmp[j] = Bpuz[j];
      }
      Bpuz[0] = NO, Bpuz[1] = NO, Bpuz[2] = NO;
      f = solve(Bpuz);
      for (int j = 0; j < 3; j++) {
        Bpuz[j] = tmp[j];
      }
    }
  }
  if (f == false) {
    if (Bpuz[13] != NO2) {
      if (Bpuz[0] == Bpuz[1]) {
        for (int j = 0; j < 2; j++) {
          tmp[j] = Bpuz[j];
        }
        Bpuz[0] = NO2, Bpuz[1] = NO2;
        f = solve(Bpuz);
        for (int j = 0; j < 2; j++) {
          Bpuz[j] = tmp[j];
        }
      }
    }
  }
  if (f == false) {
    int ii[3] = {0}, cnt = 1;
    ii[0] = 0;
    for (int i = 1; i < 14; i++) {
      if (Bpuz[ii[cnt - 1]] != Bpuz[i]) {
        if (Bpuz[ii[cnt - 1]] + 1 == Bpuz[i]) {
          ii[cnt] = i;
          cnt++;
          if (cnt == 3) {
            Bpuz[ii[0]] = NO, Bpuz[ii[1]] = NO, Bpuz[ii[2]] = NO;
            f = solve(Bpuz);
            break;
          }
        } else
          break;
      }
    }
  }
  return f;
}

int main(void) {

  string inp;
  int cnt, ans[10];
  int cou;
  bool fl;

  while (cin >> inp) {
    for (int i = 0; i < 13; i++) {
      puz[i] = inp[i] - '0';
    }
    cnt = 0;
    for (int i = 1; i < 10; i++) {
      puz[13] = i;
      fl = true;
      for (int j = 1; j < 10; j++) {
        cou = 0;
        for (int k = 0; k < 14; k++) {
          if (puz[k] == j) {
            cou++;
          }
        }
        if (cou >= 5) {
          fl = false;
          break;
        }
      }
      if (fl) {
        if (solve(puz)) {
          ans[cnt] = i;
          cnt++;
        }
      }
    }
    if (cnt != 0) {
      for (int i = 0; i < cnt - 1; i++) {
        cout << ans[i] << " ";
      }
      cout << ans[cnt - 1] << endl;
    } else
      cout << "0" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>

#define NO 100000
#define NO2 1000000000
using namespace std;

int puz[14];

bool solve(int Spuz[]) {
  int Bpuz[14];
  int tmp[3];
  bool f = true;
  for (int i = 0; i < 14; i++) {
    Bpuz[i] = Spuz[i];
    if (Bpuz[i] != NO && Bpuz[i] != NO2)
      f = false;
  }
  if (f) {
    return true;
  }
  sort(Bpuz, Bpuz + 14);
  if (f == false) {
    if (Bpuz[0] == Bpuz[1] && Bpuz[1] == Bpuz[2]) {
      for (int j = 0; j < 3; j++) {
        tmp[j] = Bpuz[j];
      }
      Bpuz[0] = NO, Bpuz[1] = NO, Bpuz[2] = NO;
      f = solve(Bpuz);
      for (int j = 0; j < 3; j++) {
        Bpuz[j] = tmp[j];
      }
    }
  }
  if (f == false) {
    if (Bpuz[13] != NO2) {
      if (Bpuz[0] == Bpuz[1]) {
        for (int j = 0; j < 2; j++) {
          tmp[j] = Bpuz[j];
        }
        Bpuz[0] = NO2, Bpuz[1] = NO2;
        f = solve(Bpuz);
        for (int j = 0; j < 2; j++) {
          Bpuz[j] = tmp[j];
        }
      }
    }
  }
  if (f == false) {
    int ii[3] = {0}, cnt = 1;
    ii[0] = 0;
    for (int i = 1; i < 14; i++) {
      if (Bpuz[ii[cnt - 1]] != Bpuz[i]) {
        if (Bpuz[ii[cnt - 1]] + 1 == Bpuz[i]) {
          ii[cnt] = i;
          cnt++;
          if (cnt == 3) {
            Bpuz[ii[0]] = NO, Bpuz[ii[1]] = NO, Bpuz[ii[2]] = NO;
            f = solve(Bpuz);
            break;
          }
        } else
          break;
      }
    }
  }
  return f;
}

int main(void) {

  string inp;
  int cnt, ans[10];
  int cou;
  bool fl;

  while (1) {
    cin >> inp;
    for (int i = 0; i < 13; i++) {
      puz[i] = inp[i] - '0';
    }
    cnt = 0;
    for (int i = 1; i < 10; i++) {
      puz[13] = i;
      fl = true;
      for (int j = 1; j < 10; j++) {
        cou = 0;
        for (int k = 0; k < 14; k++) {
          if (puz[k] == j) {
            cou++;
          }
        }
        if (cou >= 5) {
          fl = false;
          break;
        }
      }
      if (fl) {
        if (solve(puz)) {
          ans[cnt] = i;
          cnt++;
        }
      }
    }
    if (cnt != 0) {
      for (int i = 0; i < cnt - 1; i++) {
        cout << ans[i] << " ";
      }
      cout << ans[cnt - 1] << endl;
    } else
      cout << "0" << endl;
  }

  return 0;
}