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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

int dir[] = {1, 0, -1, 0, 1};

void Search(vector<vector<int>> &field, int y, int x, int gy, int gx, int step,
            int &ans) {
  if (y == gy && x == gx) {
    ans = min(ans, step);
    return;
  }
  if (step > ans)
    return;
  if (step > 10)
    return;
  for (int i = 0; i < 4; i++) {
    int ny = y, nx = x;
    while (ny >= 0 && ny < H && nx >= 0 && nx < W) {
      //	cout << ny << " " << nx << endl;
      if (ny == gy && nx == gx) {
        ans = min(ans, step);
        return;
      }
      if (ny + dir[i] < 0 || ny + dir[i] >= H || nx + dir[i + 1] < 0 ||
          nx + dir[i + 1] >= W) {
        ny += dir[i];
        nx += dir[i + 1];
        break;
      }
      if (field[ny + dir[i]][nx + dir[i + 1]] != 1) {
        ny += dir[i];
        nx += dir[i + 1];
      } else {
        break;
      }
    }
    if (ny == y && nx == x)
      continue;
    if (ny >= 0 && ny < H && nx >= 0 && nx < W) {
      field[ny + dir[i]][nx + dir[i + 1]] = 0;
      Search(field, ny, nx, gy, gx, step + 1, ans);
      field[ny + dir[i]][nx + dir[i + 1]] = 1;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> W >> H, H) {
    vector<vector<int>> field(H, vector<int>(W));
    for (auto &i : field)
      for (auto &j : i)
        cin >> j;
    int sy, sx;
    int gy, gx;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (field[i][j] == 2)
          sy = i, sx = j;
        if (field[i][j] == 3)
          gy = i, gx = j;
      }
    }
    int ans = 100;
    Search(field, sy, sx, gy, gx, 1, ans);
    if (ans == 100)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "climits"
#include "cmath"
#include "cstdio"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

const long long int MOD = 1000000007;
const long double EPS = 1e-8;

long long int N, M, K, H, W, L, R;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int dir[] = {1, 0, -1, 0, 1};
  cin >> W >> H;
  while (W) {
    vector<vector<int>> v(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        cin >> v[i][j];
      }
    }
    int sy, sx;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (v[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }
    }
    int ans = MOD;
    for (int i = 0; i < 1 << 20; i++) {
      // cout << i << endl;
      auto field = v;
      int y = sy, x = sx;
      for (int j = 0; j < 10; j++) {
        int di = (i >> (j << 1)) & 3;
        bool end = false;
        while (1) {
          int ny = y + dir[di];
          int nx = x + dir[di + 1];
          if (ny < 0 || nx < 0 || ny >= H || nx >= W) {
            end = true;
            break;
          }
          if (field[ny][nx] == 1) {
            field[ny][nx] = 0;
            ny = y;
            nx = x;
          }
          if (field[ny][nx] == 3) {
            ans = min(ans, j);
            end = true;
            break;
          }
          y = ny;
          x = nx;
        }
        if (end)
          break;
      }
    }
    if (ans == MOD)
      cout << -1 << endl;
    else
      cout << ans + 1 << endl;
    cin >> W >> H;
  }
  return 0;
}