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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

int w, h;
int sx, sy, gx, gy;
bool f[32][32];
int res;
bool out(int x, int y) { return x < 0 || x >= w || y < 0 || y >= h; }
void dfs(int x, int y, int depth) {
  if (depth >= res)
    return;

  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (out(x, y) || f[ny][nx])
      continue;

    for (; !out(nx, ny); nx += dx[i], ny += dy[i]) {
      if (nx == gx && ny == gy) {
        res = depth;
        break;
        ;
      } else if (f[ny][nx]) {
        f[ny][nx] = false;
        dfs(nx - dx[i], ny - dy[i], depth + 1);
        f[ny][nx] = true;
        break;
      }
    }
  }
}
int main() {
  while (cin >> w >> h, w) {
    clear(f, 0);
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        int t;
        cin >> t;
        if (t == 1)
          f[y][x] = true;
        else if (t == 2)
          sx = x, sy = y;
        else if (t == 3)
          gx = x, gy = y;
      }
    }

    res = 11;
    dfs(sx, sy, 1);
    if (res == 11)
      res = -1;
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define clear(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

struct P {
  int x, y, d;
  vector<vector<bool>> f;
  P(int x, int y, int d, const vector<vector<bool>> &f)
      : x(x), y(y), d(d), f(f) {}
};
int main() {
  int w, h;
  while (cin >> w >> h, w) {
    int sx, sy, gx, gy;
    vector<vector<bool>> f(h, vector<bool>(w));
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        int t;
        cin >> t;
        if (t == 1)
          f[y][x] = true;
        else if (t == 2)
          sx = x, sy = y;
        else if (t == 3)
          gx = x, gy = y;
      }
    }

    queue<P> q;
    q.push(P(sx, sy, 0, f));
    int res = -1;
    while (res == -1 && !q.empty() && q.front().d < 10) {
      P t = q.front();
      q.pop();
      for (int i = 0; i < 4; ++i) {
#define out(x, y) (x < 0 || x >= w || y < 0 || y >= h)
        int nx = t.x + dx[i], ny = t.y + dy[i];
        if (!out(nx, ny) && !t.f[ny][nx]) {
          for (; !out(nx, ny); nx += dx[i], ny += dy[i]) {
            if (nx == gx && ny == gy) {
              res = t.d + 1;
              break;
            } else if (t.f[ny][nx]) {
              vector<vector<bool>> nf(t.f);
              nf[ny][nx] = false;
              nx -= dx[i], ny -= dy[i];
              q.push(P(nx, ny, t.d + 1, nf));
              break;
            }
          }
        }
      }
    }
    cout << res << endl;
  }
}