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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define SREP(i, a, n) for (int i = a; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define all(a) (a).begin(), (a).end()
#define mp(a, b) make_pair(a, b)
#define mt make_tuple
#define fst first
#define scn second

const ll inf = (ll)1e18;
const ll mod = (ll)1e9 + 7;
const ld eps = 1e-9;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

vector<vint> a;
int w = 0, h = 0;
int ret;
int dfs(int i, int j, int t) {
  if (ret <= t)
    return ret;
  int res = ret;
  rep(k, 4) {
    int cy = i, cx = j;
    int ny = cy + dy[k], nx = cx + dx[k];
    while (0 <= ny && ny < h && 0 <= nx && nx < w && a[ny][nx] == 0) {
      cy = ny;
      cx = nx;
      ny += dy[k];
      nx += dx[k];
    }
    if (ny < 0 || h <= ny || nx < 0 || w <= nx)
      continue;
    else if (a[ny][nx] == 3) {
      res = min(ret, t + 1);
    } else if (cy == i && cx == j)
      continue;
    else if (a[ny][nx] == 1) {
      a[ny][nx] = 0;
      res = min(res, dfs(cy, cx, t + 1));
      a[ny][nx] = 1;
    }
  }
  return res;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    a = vector<vint>(h, vint(w, 0));
    rep(i, h) rep(j, w) cin >> a[i][j];
    rep(i, h) rep(j, w) {
      if (a[i][j] == 2) {
        a[i][j] = 0;
        ret = 11;
        ret = dfs(i, j, 0);
        ret = (ret == 11 ? -1 : ret);
        cout << ret << endl;
        goto next;
      }
    }
  next:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vint;
typedef vector<vint> iarray;

const int inf = 1 << 25;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int w, h;

bool isInside(int x, int y) {
  if (x < 0 || w <= x)
    return false;
  if (y < 0 || h <= y)
    return false;
  return true;
}

iarray f;

int dfs(int depth, int x, int y) {
  if (depth == 10)
    return inf;
  int ret = inf;
  depth++;
  for (int i = 0; i < 4; i++) {
    if (!isInside(x + dx[i], y + dy[i]) || f[y + dy[i]][x + dx[i]] == 1)
      continue;
    int nx = x + dx[i], ny = y + dy[i];
    while (isInside(nx + dx[i], ny + dy[i]) && f[ny + dy[i]][nx + dx[i]] == 0) {
      nx += dx[i];
      ny += dy[i];
    }
    if (!isInside(nx + dx[i], ny + dy[i]))
      continue;
    if (f[ny + dy[i]][nx + dx[i]] == 3) {
      return depth;
    } else {
      f[ny + dy[i]][nx + dx[i]] = 0;
      ret = min(ret, dfs(depth, nx, ny));
      f[ny + dy[i]][nx + dx[i]] = 1;
    }
  }
  return ret;
}

int main() {
  while (true) {
    cin >> w >> h;
    f.clear();
    f = iarray(h, vint(w));
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> f[i][j];
    int sx, sy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (f[i][j] == 2) {
          sy = i;
          sx = j;
          f[i][j] = 0;
          break;
        }
      }
    }
    int ret = dfs(0, sx, sy);
  }
}