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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

string L_cheak(string cloth, int now, bool *ret) {
  if (cloth[now + (14 * 0) + 0] != '0' && cloth[now + (14 * 1) - 1] != '0' &&
      cloth[now + (14 * 1) + 0] != '0' && cloth[now + (14 * 1) + 1] != '0' &&
      cloth[now + (14 * 2) - 2] != '0' && cloth[now + (14 * 2) - 1] != '0' &&
      cloth[now + (14 * 2) + 0] != '0' && cloth[now + (14 * 2) + 1] != '0' &&
      cloth[now + (14 * 2) + 2] != '0' && cloth[now + (14 * 3) - 1] != '0' &&
      cloth[now + (14 * 3) + 0] != '0' && cloth[now + (14 * 3) + 1] != '0' &&
      cloth[now + (14 * 4) + 0] != '0') {
    cloth[now + (14 * 0) + 0]--;
    cloth[now + (14 * 1) - 1]--;
    cloth[now + (14 * 1) + 0]--;
    cloth[now + (14 * 1) + 1]--;
    cloth[now + (14 * 2) - 2]--;
    cloth[now + (14 * 2) - 1]--;
    cloth[now + (14 * 2) + 0]--;
    cloth[now + (14 * 2) + 1]--;
    cloth[now + (14 * 2) + 2]--;
    cloth[now + (14 * 3) - 1]--;
    cloth[now + (14 * 3) + 0]--;
    cloth[now + (14 * 3) + 1]--;
    cloth[now + (14 * 4) + 0]--;
    *ret = true;
    return cloth;
  } else {
    return cloth;
  }
}

string M_cheak(string cloth, int now, bool *ret) {
  if (cloth[now + (14 * 0) - 0] != '0' && cloth[now + (14 * 0) + 1] != '0' &&
      cloth[now + (14 * 0) + 2] != '0' && cloth[now + (14 * 1) - 0] != '0' &&
      cloth[now + (14 * 1) + 1] != '0' && cloth[now + (14 * 1) + 2] != '0' &&
      cloth[now + (14 * 2) - 0] != '0' && cloth[now + (14 * 2) + 1] != '0' &&
      cloth[now + (14 * 2) + 2] != '0') {
    cloth[now + (14 * 0) - 0]--;
    cloth[now + (14 * 0) + 1]--;
    cloth[now + (14 * 0) + 2]--;
    cloth[now + (14 * 1) - 0]--;
    cloth[now + (14 * 1) + 1]--;
    cloth[now + (14 * 1) + 2]--;
    cloth[now + (14 * 2) - 0]--;
    cloth[now + (14 * 2) + 1]--;
    cloth[now + (14 * 2) + 2]--;
    *ret = true;
    return cloth;
  } else {
    return cloth;
  }
}
string S_cheak(string cloth, int now, bool *ret) {
  if (cloth[now + (14 * 0) - 0] != '0' && cloth[now + (14 * 1) - 1] != '0' &&
      cloth[now + (14 * 1) + 0] != '0' && cloth[now + (14 * 1) + 1] != '0' &&
      cloth[now + (14 * 2) - 0] != '0') {
    cloth[now + (14 * 0) - 0]--;
    cloth[now + (14 * 1) - 1]--;
    cloth[now + (14 * 1) + 0]--;
    cloth[now + (14 * 1) + 1]--;
    cloth[now + (14 * 2) - 0]--;
    *ret = true;
    return cloth;
  } else {
    return cloth;
  }
}

bool Blur(int l, int m, int s, string cloth, int now) {
  if (now >= cloth.length()) {
    for (int i = 0; i < cloth.length(); i++) {
      if (cloth[i] != '0')
        return false;
    }
    return true;
  }

  if (cloth[now] != '0') {
    string temp = cloth;
    bool ret1 = false, ret2 = false, ret3 = false;
    temp = L_cheak(temp, now, &ret1);
    if (l > 0 && ret1) {
      ret1 = Blur(l - 1, m, s, temp, now);
      if (ret1) {

        cout << now % 14 - 2 << " " << now / 14 + 2 << " " << 3 << endl;
        return true;
      }
    }
    temp = cloth;
    temp = M_cheak(temp, now, &ret2);
    if (m > 0 && ret2) {
      ret2 = Blur(l, m - 1, s, temp, now);
      if (ret2) {
        cout << now % 14 - 1 << " " << now / 14 + 1 << " " << 2 << endl;
        return true;
      }
    }
    temp = cloth;
    temp = S_cheak(temp, now, &ret3);
    if (s > 0 && ret3) {
      ret3 = Blur(l, m, s - 1, temp, now);
      if (ret3) {
        cout << now % 14 - 2 << " " << now / 14 + 1 << " " << 1 << endl;
        return true;
      }
    }
    return false;

  } else {
    return Blur(l, m, s, cloth, now + 1);
  }
}

int main() {
  string temp;
  int in;
  string cloth;
  int n;
  while (cin >> n) {
    for (int i = 0; i < 10; i++) {
      cloth += "00";
      for (int j = 0; j < 10; j++) {
        cin >> in;
        cloth += in + '0';
      }
      cloth += "00";
    }
    cloth += "00000000000000";
    cloth += "00000000000000";
    cloth += "00000000000000";
    cloth += "00000000000000";
    cloth += "00000000000000";

    int sum = 0;
    for (int i = 0; i < cloth.length(); i++) {
      sum += cloth[i] - '0';
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        for (int k = 0; k <= n; k++) {
          if (13 * i + 9 * j + 5 * k == sum && i + j + k == n) {
            int l, m, s;
            l = i;
            m = j;
            s = k;
            temp = cloth;
            Blur(l, m, s, temp, 0);
          }
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

string cloth;

// s 100000000111000000001
// m 11100000001110000000111
// l 10000000011100000011111000000111000000001

bool search_blur(int l, int m, int s, string cloth) {

  if (l != 0) {
    for (int i = 2; i <= 7; i++) {
      for (int j = 0; j <= 5; j++) {
        string temp = cloth;
        if (temp[j * 10 + i] == '0') {

        } else if (temp[j * 10 + i] > '0' && temp[(j + 1) * 10 + i - 1] > '0' &&
                   temp[(j + 1) * 10 + i] > '0' &&
                   temp[(j + 1) * 10 + i + 1] > '0' &&
                   temp[(j + 2) * 10 + i - 2] > '0' &&
                   temp[(j + 2) * 10 + i - 1] > '0' &&
                   temp[(j + 2) * 10 + i] > '0' &&
                   temp[(j + 2) * 10 + i + 1] > '0' &&
                   temp[(j + 2) * 10 + i + 2] > '0' &&
                   temp[(j + 3) * 10 + i - 1] > '0' &&
                   temp[(j + 3) * 10 + i] > '0' &&
                   temp[(j + 3) * 10 + i + 1] > '0' &&
                   temp[(j + 4) * 10 + i] > '0') {
          temp[j * 10 + i] -= 1;
          ;
          temp[(j + 1) * 10 + i - 1] -= 1;
          temp[(j + 1) * 10 + i] -= 1;
          temp[(j + 1) * 10 + i + 1] -= 1;
          temp[(j + 2) * 10 + i - 2] -= 1;
          temp[(j + 2) * 10 + i - 1] -= 1;
          temp[(j + 2) * 10 + i] -= 1;
          temp[(j + 2) * 10 + i + 1] -= 1;
          temp[(j + 2) * 10 + i + 2] -= 1;
          temp[(j + 3) * 10 + i - 1] -= 1;
          temp[(j + 3) * 10 + i] -= 1;
          temp[(j + 3) * 10 + i + 1] -= 1;
          temp[(j + 4) * 10 + i] -= 1;

          if (search_blur(l - 1, m, s, temp)) {
            cout << i << " " << j + 2 << " " << 3 << endl;
            return true;
          }
        }
      }
    }
    return false;
  }
  if (m != 0) {
    for (int i = 1; i <= 8; i++) {
      for (int j = 0; j <= 7; j++) {
        string temp = cloth;
        if (temp[j * 10 + i] == '0') {

        } else if (temp[(j)*10 + i - 1] > '0' && temp[(j)*10 + i] > '0' &&
                   temp[(j)*10 + i + 1] > '0' &&
                   temp[(j + 1) * 10 + i - 1] > '0' &&
                   temp[(j + 1) * 10 + i] > '0' &&
                   temp[(j + 1) * 10 + i + 1] > '0' &&
                   temp[(j + 2) * 10 + i - 1] > '0' &&
                   temp[(j + 2) * 10 + i] > '0' &&
                   temp[(j + 2) * 10 + i + 1] > '0') {
          temp[(j)*10 + i - 1] -= 1;
          temp[(j)*10 + i] -= 1;
          temp[(j)*10 + i + 1] -= 1;
          temp[(j + 1) * 10 + i - 1] -= 1;
          temp[(j + 1) * 10 + i] -= 1;
          temp[(j + 1) * 10 + i + 1] -= 1;
          temp[(j + 2) * 10 + i - 1] -= 1;
          temp[(j + 2) * 10 + i] -= 1;
          temp[(j + 2) * 10 + i + 1] -= 1;

          if (search_blur(l, m - 1, s, temp)) {
            cout << i << " " << j + 1 << " " << 2 << endl;
            return true;
          }
        }
      }
    }

    return false;
  }
  if (s != 0) {
    for (int i = 1; i <= 8; i++) {
      for (int j = 0; j <= 7; j++) {
        string temp = cloth;
        if (temp[j * 10 + i] == '0') {

        } else if (temp[j * 10 + i] > '0' && temp[(j + 1) * 10 + i - 1] > '0' &&
                   temp[(j + 1) * 10 + i] > '0' &&
                   temp[(j + 1) * 10 + i + 1] > '0' &&
                   temp[(j + 2) * 10 + i] > '0') {
          temp[(j)*10 + i] -= 1;
          ;
          temp[(j + 1) * 10 + i - 1] -= 1;
          temp[(j + 1) * 10 + i] -= 1;
          temp[(j + 1) * 10 + i + 1] -= 1;
          temp[(j + 2) * 10 + i] -= 1;

          if (search_blur(l, m, s - 1, temp)) {
            cout << i << " " << j + 1 << " " << 1 << endl;
            return true;
          }
        }
      }
    }
    return false;
  }
  // cout<<"temp"<<temp<<endl;
  return true;
}

/*
bool search_blur(int l, int m, int s, string cloth, int i, int j){
    if(cloth[j*10+i]=='0'){
        int temp_i=i, temp_j=j;
        if(j==7)temp_i++;
        else temp_j++;
        return search_blur(l-1, m, s, cloth, temp_i, temp_j);
    }
    if(l!=0){
        if(i<=2&&i<=7){
            if(j<=0&&j<=5){
                string temp = cloth;
                if(temp[j*10+i]=='0'){

                }else if(temp[j*10+i]>'0'
                         &&temp[(j+1)*10+i-1]>'0'&&temp[(j+1)*10+i]>'0'&&temp[(j+1)*10+i+1]>'0'
                         &&temp[(j+2)*10+i-2]>'0'&&temp[(j+2)*10+i-1]>'0'&&temp[(j+2)*10+i]>'0'&&temp[(j+2)*10+i+1]>'0'&&temp[(j+2)*10+i+2]>'0'
                         &&temp[(j+3)*10+i-1]>'0'&&temp[(j+3)*10+i]>'0'&&temp[(j+3)*10+i+1]>'0'
                         &&temp[(j+4)*10+i]>'0')
                {
                    temp[j*10+i]-=1;
                    ;temp[(j+1)*10+i-1]-=1;temp[(j+1)*10+i]-=1;temp[(j+1)*10+i+1]-=1
                    ;temp[(j+2)*10+i-2]-=1;temp[(j+2)*10+i-1]-=1;temp[(j+2)*10+i]-=1;temp[(j+2)*10+i+1]-=1;temp[(j+2)*10+i+2]-=1
                    ;temp[(j+3)*10+i-1]-=1;temp[(j+3)*10+i]-=1;temp[(j+3)*10+i+1]-=1
                    ;temp[(j+4)*10+i]-=1;
                    int temp_i=i, temp_j=j;
                    if(j==7)temp_i++;
                    else temp_j++;
                    if(search_blur(l-1, m, s, temp, temp_i, temp_j)){
                        cout<<i<<" "<<j+2<<" "<<3<<endl;
                        return true;
                    }
                }
            }
        }
        return false;
    }
    if(m!=0){
        if(i<=1&&i<=8){
            if(j<=0&&j<=7){
                string temp = cloth;
                if(temp[j*10+i]=='0'){

                }else if(
                         temp[(j)*10+i-1]>'0'&&temp[(j)*10+i]>'0'&&temp[(j)*10+i+1]>'0'
                         &&temp[(j+1)*10+i-1]>'0'&&temp[(j+1)*10+i]>'0'&&temp[(j+1)*10+i+1]>'0'
                         &&temp[(j+2)*10+i-1]>'0'&&temp[(j+2)*10+i]>'0'&&temp[(j+2)*10+i+1]>'0')
                {
                    temp[(j)*10+i-1]-=1;temp[(j)*10+i]-=1;temp[(j)*10+i+1]-=1
                    ;temp[(j+1)*10+i-1]-=1;temp[(j+1)*10+i]-=1;temp[(j+1)*10+i+1]-=1
                    ;temp[(j+2)*10+i-1]-=1;temp[(j+2)*10+i]-=1;temp[(j+2)*10+i+1]-=1;
                    int temp_i=i, temp_j=j;
                    if(j==7)temp_i++;
                    else temp_j++;
                    if(search_blur(l, m-1, s, temp, temp_i, temp_j)){
                        cout<<i<<" "<<j+1<<" "<<2<<endl;
                        return true;
                    }
                }
            }
        }

        return false;

    }
    if(s!=0){
        if(i<=1&&i<=8){
            if(j<=0&&j<=7){
                string temp = cloth;
                if(temp[j*10+i]=='0'){

                }else if(
                         temp[j*10+i]>'0'
                         &&temp[(j+1)*10+i-1]>'0'&&temp[(j+1)*10+i]>'0'&&temp[(j+1)*10+i+1]>'0'
                         &&temp[(j+2)*10+i]>'0')
                {
                    temp[(j)*10+i]-=1;
                    ;temp[(j+1)*10+i-1]-=1;temp[(j+1)*10+i]-=1;temp[(j+1)*10+i+1]-=1
                    ;temp[(j+2)*10+i]-=1;
                    int temp_i=i, temp_j=j;
                    if(j==7)temp_i++;
                    else temp_j++;
                    if(search_blur(l, m, s-1, temp, temp_i, temp_j)){
                        cout<<i<<" "<<j+1<<" "<<1<<endl;
                        return true;
                    }
                }
            }
        }
        return false;
    }
    //cout<<"temp"<<temp<<endl;
    return true;

}
*/

int main() {
  int n;
  cin >> n;
  int sum = 0;
  for (int i = 0; i < 100; i++) {
    char temp;
    cin >> temp;
    cloth += temp;
    sum += temp - '0';
  }
  int l, m, s;
  bool flag = false;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      int temp = i * 13 + j * 9;
      if (temp > sum)
        break;
      for (int k = 0; k <= n; k++) {
        int temp2 = temp + k * 5;
        if (temp2 > sum)
          break;
        if (temp2 == sum) {

          l = i;
          m = j;
          s = k;
          if (search_blur(l, m, s, cloth /*, 2, 0*/)) {
            flag = true;
            break;
          }
        }
      }
      if (flag)
        break;
    }
    if (flag)
      break;
  }
  // cout<<cloth<<endl;
  // cout<<l<<" "<<m<<" "<<s<<endl;
}