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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define all(a) a.begin(), a.end()
#define lb(s, x) lower_bound(all(s), x) - s.begin()
#define debug(x) cerr << #x << ": " << x << endl
#define INF 1e9
typedef long long ll;

int X, Y;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using P = pair<int, int>;
vector<vector<int>> s;

int solve(int y, int x, int k, set<P> st, int depth) {
  if (depth > 10)
    return INF;
  if (k == -1) {
    int res = INF;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
        continue;
      if (s[ny][nx] == 3)
        return 1;
      if (!(s[ny][nx] == 1 && st.count({ny, nx}) == 0)) {
        res = min(solve(ny, nx, i, st, depth + 1), res);
      }
    }
    return res + 1;
  }
  while (true) {
    int ny = y + dy[k];
    int nx = x + dx[k];
    if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
      return INF; // fall
    if (s[ny][nx] == 3)
      return 0;
    if (s[ny][nx] == 1 && st.count({ny, nx}) == 0) {
      st.insert({ny, nx});
      return solve(y, x, -1, st, depth);
    }
    y = ny, x = nx;
  }
}

int main(void) {
  while (cin >> X >> Y, X | Y) {
    s.clear();
    s.resize(Y);

    int sy, sx, gy, gx;
    rep(y, Y) {
      s[y].resize(X);
      rep(x, X) {
        cin >> s[y][x];
        if (s[y][x] == 2) {
          sy = y, sx = x;
          s[y][x] = 0;
        }
        if (s[y][x] == 3) {
          gy = y, gx = x;
        }
      }
    }
    set<P> st;
    int res = solve(sy, sx, -1, st, 0);
    cout << (res > 10 ? -1 : res) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rep1(i, n) for (int i = 1; i <= int(n); ++i)
#define all(a) a.begin(), a.end()
#define lb(s, x) lower_bound(all(s), x) - s.begin()
#define debug(x) cerr << #x << ": " << x << endl
#define INF 1e9
typedef long long ll;

int X, Y;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

using P = pair<int, int>;
vector<vector<int>> s;

int solve(int y, int x, int k, set<P> st) {
  if (k == -1) {
    int res = INF;
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
        continue;
      if (!(s[ny][nx] == 1 && st.count({ny, nx}) == 0)) {
        res = min(solve(y, x, i, st), res);
      }
    }
    return res + 1;
  }
  while (true) {
    int ny = y + dy[k];
    int nx = x + dx[k];
    if (ny < 0 || nx < 0 || ny >= Y || nx >= X)
      return INF; // fall
    if (s[ny][nx] == 3)
      return 0;
    if (s[ny][nx] == 1 && st.count({ny, nx}) == 0) {
      st.insert({ny, nx});
      return solve(y, x, -1, st);
    }
    y = ny, x = nx;
  }
}

int main(void) {
  while (cin >> X >> Y, X | Y) {
    s.clear();
    s.resize(Y);

    int sy, sx, gy, gx;
    rep(y, Y) {
      s[y].resize(X);
      rep(x, X) {
        cin >> s[y][x];
        if (s[y][x] == 2) {
          sy = y, sx = x;
          s[y][x] = 0;
        }
        if (s[y][x] == 3) {
          gy = y, gx = x;
        }
      }
    }
    int res = solve(sy, sx, -1, {});
    cout << (res > 10 ? -1 : res) << endl;
  }
  return 0;
}