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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
struct State {
  int x[19][15];
};
State S;
int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int solve(State p) {
  bool OK3 = false;
  for (int i = 0; i < 15; i++) {
    if (p.x[18][i] == 1 || p.x[17][i] == 1)
      OK3 = true;
  }
  if (OK3 == false)
    return 999999;
  int cx = 0, cy = 0;
  for (int i = 0; i < 285; i++) {
    if (p.x[i / 15][i % 15] == 2) {
      cx = i / 15;
      cy = i % 15;
    }
  }
  vector<int> vec;
  for (int i = 0; i < 8; i++) {
    int ex = cx + dx[i], ey = cy + dy[i];
    if (ex < 0 || ey < 0 || ex >= 19 || ey >= 15)
      continue;
    if (p.x[ex][ey] == 0)
      continue;
    vec.push_back(i);
  }
  if (vec.size() == 0)
    return 999999;
  int minx = 999999;
  for (int i = 0; i < 8; i++) {
    bool OK2 = false;
    for (int j = 0; j < vec.size(); j++) {
      if (vec[j] == i)
        OK2 = true;
    }
    if (OK2 == false)
      continue;
    State T = p;
    int ex = cx, ey = cy;
    bool flag = true;
    while (true) {
      ex += dx[i];
      ey += dy[i];
      if (ex >= 19)
        break;
      if (ex < 0 || ey < 0 || ey >= 15) {
        flag = false;
        break;
      }
      if (T.x[ex][ey] == 0)
        break;
      T.x[ex][ey] = 0;
    }
    if (flag == false)
      continue;

    if (abs(ex - cx) <= 1 && abs(ey - cy) <= 1)
      continue;
    if (ex == 18 || ex == 19)
      minx = min(minx, 1);
    else {
      T.x[cx][cy] = 0;
      T.x[ex][ey] = 2;
      minx = min(minx, solve(T) + 1);
    }
  }
  return minx;
}
int main() {
  for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 15; j++) {
      char p;
      cin >> p;
      if (p == 'O')
        S.x[i][j] = 2;
      if (p == 'X')
        S.x[i][j] = 1;
      if (p == '.')
        S.x[i][j] = 0;
    }
  }
  int R = solve(S);
  if (R >= 100000)
    R = -1;
  cout << R << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
struct State {
  int x[19][15];
};
State S;
int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int solve(State p) {
  int cx = 0, cy = 0;
  for (int i = 0; i < 285; i++) {
    if (p.x[i / 15][i % 15] == 2) {
      cx = i / 15;
      cy = i % 15;
    }
  }
  vector<int> vec;
  for (int i = 0; i < 8; i++) {
    int ex = cx + dx[i], ey = cy + dy[i];
    if (ex < 0 || ey < 0 || ex >= 19 || ey >= 15)
      continue;
    if (p.x[ex][ey] == 0)
      continue;
    vec.push_back(i);
  }
  if (vec.size() == 0)
    return 999999;
  int minx = 999999;
  for (int i = 0; i < 8; i++) {
    bool OK2 = false;
    for (int j = 0; j < vec.size(); j++) {
      if (vec[j] == i)
        OK2 = true;
    }
    if (OK2 == false)
      continue;
    State T = p;
    int ex = cx, ey = cy;
    bool flag = true;
    while (true) {
      ex += dx[i];
      ey += dy[i];
      if (ex >= 19)
        break;
      if (ex < 0 || ey < 0 || ey >= 15) {
        flag = false;
        break;
      }
      if (T.x[ex][ey] == 0)
        break;
      T.x[ex][ey] = 0;
    }
    if (flag == false)
      continue;

    if (abs(ex - cx) <= 1 && abs(ey - cy) <= 1)
      continue;
    if (ex == 18 || ex == 19)
      minx = min(minx, 1);
    else {
      T.x[cx][cy] = 0;
      T.x[ex][ey] = 2;
      minx = min(minx, solve(T) + 1);
    }
  }
  return minx;
}
int main() {
  for (int i = 0; i < 19; i++) {
    for (int j = 0; j < 15; j++) {
      char p;
      cin >> p;
      if (p == 'O')
        S.x[i][j] = 2;
      if (p == 'X')
        S.x[i][j] = 1;
      if (p == '.')
        S.x[i][j] = 0;
    }
  }
  int R = solve(S);
  if (R >= 100000)
    R = -1;
  cout << R << endl;
  return 0;
}