Problem p00725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

typedef vector<char> vc;
typedef vector<vc> vvc;

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

struct state {
  vvc f;
  int x, y, d;
  state(vvc f, int x, int y, int d) : f(f), x(x), y(y), d(d){};
};

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vvc field(h, vc(w));
    int sx, sy;
    REP(i, h) {
      REP(j, w) {
        cin >> field[i][j];
        if (field[i][j] == '2') {
          sx = j, sy = i;
        }
      }
    }

    stack<state> Q;
    Q.push(state(field, sx, sy, 0));
    int ans = INF;
    while (!Q.empty()) {
      state st = Q.top();
      Q.pop();
      if (st.d == 10) {
        continue;
      }

      REP(d, 4) {
        bool ok = false;
        int nx = st.x;
        int ny = st.y;
      first:
        nx += dx[d];
        ny += dy[d];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
          continue;
        }
        switch (st.f[ny][nx]) {
        case '0':
        case '2':
          ok = true;
          goto first;
          break;
        case '1':
          if (ok) {
            st.f[ny][nx] = '0';
            Q.push(state(st.f, nx - dx[d], ny - dy[d], st.d + 1));
            st.f[ny][nx] = '1';
          }
          break;
        case '3':
          ans = min(ans, st.d + 1);
          break;
        }
      }
    }

    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

typedef vector<char> vc;
typedef vector<vc> vvc;

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

struct state {
  vvc f;
  int x, y, d;
  state(vvc f, int x, int y, int d) : f(f), x(x), y(y), d(d){};
};

int main() {
  int w, h;
  while (cin >> w >> h, w) {
    vvc field(h, vc(w));
    int sx, sy;
    REP(i, h) {
      REP(j, w) {
        cin >> field[i][j];
        if (field[i][j] == '2') {
          sx = j, sy = i;
        }
      }
    }

    stack<state> Q;
    Q.push(state(field, sx, sy, 0));
    int ans = INF;
    while (!Q.empty()) {
      state st = Q.top();
      Q.pop();
      if (st.d == 10 || ans <= st.d) {
        continue;
      }

      REP(d, 4) {
        bool ok = false;
        int nx = st.x;
        int ny = st.y;
      first:
        nx += dx[d];
        ny += dy[d];
        if (nx < 0 || w <= nx || ny < 0 || h <= ny) {
          continue;
        }
        switch (st.f[ny][nx]) {
        case '0':
        case '2':
          ok = true;
          goto first;
          break;
        case '1':
          if (ok) {
            st.f[ny][nx] = '0';
            Q.push(state(st.f, nx - dx[d], ny - dy[d], st.d + 1));
            st.f[ny][nx] = '1';
          }
          break;
        case '3':
          ans = min(ans, st.d + 1);
          break;
        }
      }
    }

    cout << (ans == INF ? -1 : ans) << endl;
  }

  return 0;
}