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

#include <algorithm>
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
constexpr int H = 19;
constexpr int W = 15;
int ans = INF;

bool out(const int &count, const pair<int, int> &next) {
  int y = next.first;
  int x = next.second;

  if (x < 0)
    return true;
  if (y < 0)
    return true;
  if (14 < x)
    return true;
  if (18 < y) {
    ans = min(ans, count);
    return true;
  }
  return false;
}

int gap(pair<int, int> l, pair<int, int> r) {
  int a = abs(l.first - r.first);
  int b = abs(l.second - r.second);
  return max(a, b);
}

bool goal(const pair<int, int> now) {
  if (now.first == H)
    return true;
  if (now.first == H - 1 && 0 <= now.second && now.second < W)
    return true;
  return false;
}

void step(const array<array<bool, W>, H> &goban, const pair<int, int> &now,
          const int &count) {
  constexpr int dirSiz = 8;
  constexpr int dh[] = {-1, 0, 1, 1, 1, 0, -1, -1};
  constexpr int dw[] = {1, 1, 1, 0, -1, -1, -1, 0};
  if (ans <= count)
    return;

  // visualize(goban, now);

  REP(i, dirSiz) {
    auto nextGoban = goban;
    auto next = now;

    while (true) {
      next.first += dh[i];
      next.second += dw[i];
      if (out(count, next))
        break;

      if (nextGoban[next.first][next.second]) {
        nextGoban[next.first][next.second] = false;
      } else {
        break;
      }
    }
    if (gap(now, next) <= 1) {
      continue;
    } else {
      if (goal(next)) {
        ans = min(ans, count);
      } else {
        if (!out(count, next)) {
          // visualize(goban, now);
          step(nextGoban, next, count + 1);
        }
      }
    }
  }
}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  pair<int, int> start;
  array<array<bool, W>, H> goban;
  REP(i, H) {
    string tmp;
    array<bool, W> row;
    cin >> tmp;
    REP(j, W) {
      char c = tmp[j];
      row[j] = (c == 'X');
      if (c == 'O')
        start = {i, j};
    }
    goban[i] = row;
  }

  step(goban, start, 1);
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  debug();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <vector>

#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define REP(i, n) FOR((i), 0, (n))
#define LL long long
#define CLR(a) memset((a), 0, sizeof(a))
#define SZ(x) (int((x).size()))
#define WAITING(str)                                                           \\
  int str;                                                                     \\
  std::cin >> str;
#define DEBUGING(str) cout << str << endl
using namespace std;

const LL MOD = 1000000007; // 10^9+7
const int INF = (1 << 30);

// 変数
constexpr int H = 19;
constexpr int W = 15;
int ans = INF;

void visualize(const vector<vector<bool>> &goban, pair<int, int> now) {
  cerr << "===================" << endl;
  cerr << now.first << ":::" << now.second << endl;
  REP(i, H) {
    REP(j, W) {
      if (now.first == i && now.second == j)
        cerr << "o";
      else
        cerr << (goban[i][j] ? "X" : ".");
    }
    cerr << endl;
  }
  cerr << "===================" << endl;
}

bool out(const int &count, const pair<int, int> &next) {
  int y = next.first;
  int x = next.second;

  if (x < 0)
    return true;
  if (y < 0)
    return true;
  if (14 < x)
    return true;
  if (18 < y) {
    ans = min(ans, count);
    return true;
  }
  return false;
}

int gap(pair<int, int> l, pair<int, int> r) {
  int a = abs(l.first - r.first);
  int b = abs(l.second - r.second);
  return max(a, b);
}

bool goal(const pair<int, int> now) {
  if (now.first == H)
    return true;
  if (now.first == H - 1 && 0 <= now.second && now.second < W)
    return true;
  return false;
}

void step(const vector<vector<bool>> &goban, const pair<int, int> &now,
          const int &count) {
  constexpr int dirSiz = 8;
  constexpr int dh[] = {-1, 0, 1, 1, 1, 0, -1, -1};
  constexpr int dw[] = {1, 1, 1, 0, -1, -1, -1, 0};
  if (ans <= count)
    return;

  // visualize(goban, now);

  REP(i, dirSiz) {
    auto nextGoban = goban;
    auto next = now;

    while (true) {
      next.first += dh[i];
      next.second += dw[i];
      if (out(count, next))
        break;

      if (nextGoban[next.first][next.second]) {
        nextGoban[next.first][next.second] = false;
      } else {
        break;
      }
    }
    if (gap(now, next) <= 1) {
      continue;
    } else {
      if (goal(next)) {
        ans = min(ans, count);
      } else {
        if (!out(count, next)) {
          // visualize(goban, now);
          step(nextGoban, next, count + 1);
        }
      }
    }
  }
}

// デバッグ
void debug() {
  int N;
  cin >> N;
}

// メイン関数
int main() {
  pair<int, int> start;
  vector<vector<bool>> goban;
  REP(i, H) {
    string tmp;
    vector<bool> row;
    cin >> tmp;
    REP(j, W) {
      char c = tmp[j];
      row.push_back(c == 'X');
      if (c == 'O')
        start = {i, j};
    }
    goban.push_back(row);
  }

  step(goban, start, 1);
  if (ans == INF)
    ans = -1;
  cout << ans << endl;

  debug();
  return 0;
}