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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> pint;
const int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
pint Start, Goal;
int W, H;
bool range_out(int x, int y) { return x < 0 || y < 0 || W <= x || H <= y; }
int solve(mat fld, pint now, int turn) {
  if (turn >= 10)
    return INF;
  int res = INF;
  // cout << "now" << now.first << ' ' << now.second << endl;
  rep(i, 4) {
    int nx = now.first + dx[i], ny = now.second + dy[i];
    if (range_out(nx, ny) || fld[ny][nx] == 1)
      continue;
    mat temp = fld;
    bool out = false;
    while (true) {
      if (range_out(nx, ny)) {
        out = true;
        break;
      }
      if (fld[ny][nx] == 1) {
        temp[ny][nx] = 0;
        nx -= dx[i];
        ny -= dy[i];
        break;
      }
      if (fld[ny][nx] == 3)
        return turn + 1;
      nx += dx[i];
      ny += dy[i];
    }
    // cout << nx << ' ' << ny << endl;
    if (!out)
      res = min(res, solve(temp, pint(nx, ny), turn + 1));
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> W >> H && W) {
    mat fld(H, vec(W));
    rep(i, H) rep(j, W) {
      cin >> fld[i][j];
      if (fld[i][j] == 2)
        Start = pint(j, i);
      if (fld[i][j] == 3)
        Goal = pint(j, i);
    }
    int ans = solve(fld, Start, 0);
    if (ans == INF || ans > 10)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> pint;
const int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
pint Start, Goal;
int W, H;
bool range_out(int x, int y) { return x < 0 || y < 0 || W <= x || H <= y; }
int solve(mat fld, pint now, int turn) {
  if (turn > 10)
    return INF;
  int res = INF;
  // cout << "now" << now.first << ' ' << now.second << endl;
  rep(i, 4) {
    int nx = now.first + dx[i], ny = now.second + dy[i];
    if (range_out(nx, ny) || fld[ny][nx] == 1)
      continue;
    mat temp = fld;
    bool out = false;
    while (true) {
      if (range_out(nx, ny)) {
        out = true;
        break;
      }
      if (fld[ny][nx] == 1) {
        temp[ny][nx] = 0;
        nx -= dx[i];
        ny -= dy[i];
        break;
      }
      if (fld[ny][nx] == 3)
        return turn + 1;
      nx += dx[i];
      ny += dy[i];
    }
    // cout << nx << ' ' << ny << endl;
    if (!out)
      res = min(res, solve(temp, pint(nx, ny), turn + 1));
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> W >> H && W) {
    mat fld(H, vec(W));
    rep(i, H) rep(j, W) {
      cin >> fld[i][j];
      if (fld[i][j] == 2)
        Start = pint(j, i);
      if (fld[i][j] == 3)
        Goal = pint(j, i);
    }
    int ans = solve(fld, Start, 0);
    if (ans == INF || ans > 10)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}