Problem p00725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
int solve(vector<vector<int>> field, int h, int w, int y, int x, int steps) {
  if (steps > 10)
    return 100;
  int answer = 100;
  REP(i, 4) {
    if (y + dy[i] < 0 || h <= y + dy[i] || x + dx[i] < 0 || w <= x + dx[i] ||
        field[y + dy[i]][x + dx[i]] == 1)
      continue;
    else {
      int tmpx = x, tmpy = y;
      while (0 <= tmpy && tmpy < h && 0 <= tmpx && tmpx < w &&
             field[tmpy][tmpx] == 0) {
        tmpy += dy[i];
        tmpx += dx[i];
      }
      if (tmpy < 0 || h <= tmpy || tmpx < 0 || w <= tmpx)
        continue;
      if (field[tmpy][tmpx] == 3)
        return 1;
      if (field[tmpy][tmpx] == 1) {
        vector<vector<int>> newfield;
        newfield.resize(h);
        REP(j, h) {
          newfield[j].resize(w);
          REP(k, w)
          newfield[j][k] = field[j][k];
        }
        newfield[tmpy][tmpx] = 0;
        answer = min(
            answer,
            solve(newfield, h, w, tmpy - dy[i], tmpx - dx[i], steps + 1) + 1);
      }
    }
  }
  return answer;
}
int main(void) {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (!w && !h)
      return 0;
    vector<vector<int>> field;
    field.resize(h);
    REP(i, h) {
      field[i].resize(w);
      REP(j, w) { cin >> field[i][j]; }
    }
    int x, y;
    REP(i, h) {
      REP(j, w) {
        if (field[i][j] == 2) {
          y = i;
          x = j;
          field[i][j] = 0;
        }
      }
    }
    int answer = solve(field, h, w, y, x, 0);
    if (answer > 10)
      answer = -1;
    cout << answer << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

using namespace std;
int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0};
int solve(vector<vector<int>> field, int h, int w, int y, int x, int steps) {
  if (steps >= 10)
    return 100;
  int answer = 100;
  REP(i, 4) {
    if (y + dy[i] < 0 || h <= y + dy[i] || x + dx[i] < 0 || w <= x + dx[i] ||
        field[y + dy[i]][x + dx[i]] == 1)
      continue;
    else {
      int tmpx = x, tmpy = y;
      while (0 <= tmpy && tmpy < h && 0 <= tmpx && tmpx < w &&
             field[tmpy][tmpx] == 0) {
        tmpy += dy[i];
        tmpx += dx[i];
      }
      if (tmpy < 0 || h <= tmpy || tmpx < 0 || w <= tmpx)
        continue;
      if (field[tmpy][tmpx] == 3)
        return 1;
      if (field[tmpy][tmpx] == 1) {
        vector<vector<int>> newfield;
        newfield.resize(h);
        REP(j, h) {
          newfield[j].resize(w);
          REP(k, w)
          newfield[j][k] = field[j][k];
        }
        newfield[tmpy][tmpx] = 0;
        answer = min(
            answer,
            solve(newfield, h, w, tmpy - dy[i], tmpx - dx[i], steps + 1) + 1);
      }
    }
  }
  return answer;
}
int main(void) {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (!w && !h)
      return 0;
    vector<vector<int>> field;
    field.resize(h);
    REP(i, h) {
      field[i].resize(w);
      REP(j, w) { cin >> field[i][j]; }
    }
    int x, y;
    REP(i, h) {
      REP(j, w) {
        if (field[i][j] == 2) {
          y = i;
          x = j;
          field[i][j] = 0;
        }
      }
    }
    int answer = solve(field, h, w, y, x, 0);
    if (answer > 10)
      answer = -1;
    cout << answer << endl;
  }
}