Problem p00725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int w, h;
int board[21][21];
int tesu[21][21];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void solve(int sx, int sy) {
  // cout << sx << " " << sy << endl;
  for (int i = 0; i < 4; i++) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if (board[nx][ny] == 1)
      continue;
    while (nx >= 0 && ny >= 0 && nx < h && ny < w && board[nx][ny] != 1) {
      if (board[nx][ny] == 3) {
        tesu[nx][ny] = min(tesu[sx][sy] + 1, tesu[nx][ny]);
        break;
      }
      nx += dx[i];
      ny += dy[i];
    }
    if (board[nx][ny] == 3)
      continue;
    // cout << " " << nx << " " << ny << endl;
    if (nx < 0 || ny < 0 || nx >= h || ny >= w)
      continue;
    nx -= dx[i];
    ny -= dy[i];
    // cout << tesu[sx][sy] << endl;
    int tmp = tesu[nx][ny];
    tesu[nx][ny] = tesu[sx][sy] + 1;
    board[nx + dx[i]][ny + dy[i]] = 0;
    solve(nx, ny);
    tesu[nx][ny] = tmp;
    board[nx + dx[i]][ny + dy[i]] = 1;
  }
  return;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    int sx, sy, gx, gy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> board[i][j];
        if (board[i][j] == 2) {
          sx = i;
          sy = j;
          board[i][j] = 0;
        } else if (board[i][j] == 3) {
          gx = i;
          gy = j;
        }
        tesu[i][j] = 11;
      }
    }
    tesu[sx][sy] = 0;
    solve(sx, sy);
    if (tesu[gx][gy] != 11)
      cout << tesu[gx][gy] << endl;
    else
      cout << -1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int w, h;
int board[21][21];
int tesu[21][21];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void solve(int sx, int sy) {
  // cout << sx << " " << sy << endl;
  if (tesu[sx][sy] > 10)
    return;
  for (int i = 0; i < 4; i++) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    if (board[nx][ny] == 1)
      continue;
    while (nx >= 0 && ny >= 0 && nx < h && ny < w && board[nx][ny] != 1) {
      if (board[nx][ny] == 3) {
        tesu[nx][ny] = min(tesu[sx][sy] + 1, tesu[nx][ny]);
        break;
      }
      nx += dx[i];
      ny += dy[i];
    }
    if (board[nx][ny] == 3)
      continue;
    // cout << " " << nx << " " << ny << endl;
    if (nx < 0 || ny < 0 || nx >= h || ny >= w)
      continue;
    nx -= dx[i];
    ny -= dy[i];
    // cout << tesu[sx][sy] << endl;
    int tmp = tesu[nx][ny];
    tesu[nx][ny] = tesu[sx][sy] + 1;
    board[nx + dx[i]][ny + dy[i]] = 0;
    solve(nx, ny);
    tesu[nx][ny] = tmp;
    board[nx + dx[i]][ny + dy[i]] = 1;
  }
  return;
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    int sx, sy, gx, gy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> board[i][j];
        if (board[i][j] == 2) {
          sx = i;
          sy = j;
          board[i][j] = 0;
        } else if (board[i][j] == 3) {
          gx = i;
          gy = j;
        }
        tesu[i][j] = 11;
      }
    }
    tesu[sx][sy] = 0;
    solve(sx, sy);
    if (tesu[gx][gy] != 11)
      cout << tesu[gx][gy] << endl;
    else
      cout << -1 << endl;
  }
}