Problem p00725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
int ans = 100000;
int h, w;
int dx[] = {0, -1, 0, 1};
int sy, sx, gy, gx;
int dy[] = {-1, 0, 1, 0};
int moji[35][35];
void dfs(int y, int x, int cnt) // 1,,,top 2...left 3...down 4...right
{
  if (cnt + 1 >= ans)
    return;
  // cout<<"nct :"<<cnt<<endl;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (ny >= 0 && nx >= 0 && h > ny && w > nx && (moji[ny][nx] != 1)) {
      while (ny >= 0 && nx >= 0 && h > ny && w > nx) {
        if (moji[ny][nx] == 3) {
          ans = min(ans, cnt + 1);
          return;
        }
        // if(ny==gy&&nx==gx)return;
        // if(nx<0||ny<0||nx>=w||ny>=h)return -1;
        if (moji[ny][nx] == 1) {
          moji[ny][nx] = 0;
          // if(i==0)
          { dfs(ny - dy[i], nx - dx[i], cnt + 1); }
          moji[ny][nx] = 1;
          break;
        }
        nx += dx[i];
        ny += dy[i];
      }
    }
    //	A:;
  }
  return;
}
int main() {
  while (cin >> w >> h, w) {
    ans = 100000;
    rep(i, 35) rep(j, 35) moji[i][j] = 0;
    rep(i, h) rep(j, w) {
      cin >> moji[i][j];
      if (moji[i][j] == 2) {
        sx = j;
        sy = i;
        moji[i][j] = 0;
      }
      /*else if(moji[i][j]==3)
      {
              gx=j;
              gy=i;
              moji[i][j]=0;
      }
      */
    }
    dfs(sy, sx, 0);
    cout << (ans <= 10 ? ans : -1) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
const ll MOD = 1000000007;
int ans = 100000;
int h, w;
int dx[] = {0, -1, 0, 1};
int sy, sx, gy, gx;
int dy[] = {-1, 0, 1, 0};
int moji[35][35];
void dfs(int y, int x, int cnt) // 1,,,top 2...left 3...down 4...right
{
  if (cnt + 1 >= ans)
    return;
  // cout<<"nct :"<<cnt<<endl;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (ny >= 0 && nx >= 0 && h > ny && w > nx && (moji[ny][nx] != 1)) {
      while (ny >= 0 && nx >= 0 && h > ny && w > nx) {
        if (moji[ny][nx] == 3) {
          ans = min(ans, cnt + 1);
          return;
        }
        // if(ny==gy&&nx==gx)return;
        // if(nx<0||ny<0||nx>=w||ny>=h)return -1;
        if (moji[ny][nx] == 1) {
          moji[ny][nx] = 0;
          // if(i==0)
          { dfs(ny - dy[i], nx - dx[i], cnt + 1); }
          moji[ny][nx] = 1;
          break;
        }
        nx += dx[i];
        ny += dy[i];
      }
    }
    //	A:;
  }
  return;
}
int main() {
  while (cin >> w >> h, w) {
    ans = 11;
    rep(i, 35) rep(j, 35) moji[i][j] = 0;
    rep(i, h) rep(j, w) {
      cin >> moji[i][j];
      if (moji[i][j] == 2) {
        sx = j;
        sy = i;
        moji[i][j] = 0;
      }
      /*else if(moji[i][j]==3)
      {
              gx=j;
              gy=i;
              moji[i][j]=0;
      }
      */
    }
    dfs(sy, sx, 0);
    cout << (ans <= 10 ? ans : -1) << endl;
  }
  return 0;
}