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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int w, h;
int sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int result;

bool isIn(int x, int y) { return 0 <= x && x < h && 0 <= y && y < w; }

int dfs(int x, int y, int direction, int count, vector<vector<int>> field) {
  if (count >= result || 10 < count || !isIn(x, y))
    return 100;
  else if (field[x][y] == 1)
    return 100;
  else if (field[x][y] == 3)
    return count;
  else {

    while (isIn(x, y) && field[x][y] == 0) {
      x += dx[direction];
      y += dy[direction];
    }
    if (!isIn(x, y))
      return 100;
    else if (field[x][y] == 3)
      return count;
    else {
      x -= dx[direction];
      y -= dy[direction];
      int result = 100;
      field[x + dx[direction]][y + dy[direction]] = 0;
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        result = min(result, dfs(nx, ny, i, count + 1, field));
      }
      return result;
    }
  }
}

int main() {
  while (cin >> w >> h, w + h) {
    vector<vector<int>> field(h);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int t;
        scanf("%d", &t);
        field[i].push_back(t);
        if (field[i][j] == 2) {
          sx = i;
          sy = j;
          field[i][j] = 0;
        }
      }
    }
    result = 20;
    for (int i = 0; i < 4; i++) {
      result = min(result, dfs(sx + dx[i], sy + dy[i], i, 1, field));
    }
    cout << (result == 20 ? -1 : result) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int w, h;
int sx, sy;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int result;

bool isIn(int x, int y) { return 0 <= x && x < h && 0 <= y && y < w; }

int dfs(int x, int y, int direction, int count, vector<vector<int>> field) {
  if (count >= result || 10 < count || !isIn(x, y))
    return 100;
  else if (field[x][y] == 1)
    return 100;
  else if (field[x][y] == 3)
    return count;
  else {
    if (!isIn(x + dx[direction], y + dy[direction]))
      return 100;
    else if (field[x + dx[direction]][y + dy[direction]] != 1)
      return dfs(x + dx[direction], y + dy[direction], direction, count, field);
    else if (field[x + dx[direction]][y + dy[direction]] == 1) {
      int result = 100;
      field[x + dx[direction]][y + dy[direction]] = 0;
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        result = min(result, dfs(nx, ny, i, count + 1, field));
      }
      return result;
    }
  }
}

int main() {
  while (cin >> w >> h, w + h) {
    vector<vector<int>> field(h);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int t;
        scanf("%d", &t);
        field[i].push_back(t);
        if (field[i][j] == 2) {
          sx = i;
          sy = j;
          field[i][j] = 0;
        }
      }
    }
    result = 20;
    for (int i = 0; i < 4; i++) {
      result = min(result, dfs(sx + dx[i], sy + dy[i], i, 1, field));
    }
    cout << (result == 20 ? -1 : result) << endl;
  }
}