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

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;

ll n;
int w, h;
void depth(int x, int y, string s[], ll num) {
  num++;
  // cout << x << "    " << y <<"     " <<num << endl;
  // cout << endl;
  /*REP(i,h){
    REP(j,s[0].size()){
      cout << s[i][j];
    }
    cout << endl;
    }
    cout << endl;*/
  if (num <= 10) {
    if (x + 1 < w && s[y][x + 1] != '1') {
      int ww = 25;
      for (int i = x + 1; i < w; i++) {
        if (s[y][i] == '3') {
          n = min(n, num);
          return;
        }
        if (s[y][i] == '1') {
          ww = i;
          break;
        }
      }
      if (ww != 25) {
        s[y][ww] = '0';

        depth(ww - 1, y, s, num);
        s[y][ww] = '1';
      }
    }
    if (x - 1 >= 0 && s[y][x - 1] != '1') {
      int ww = 25;
      for (int i = x - 1; i >= 0; i--) {
        if (s[y][i] == '3') {
          n = min(n, num);
          return;
        }
        if (s[y][i] == '1') {
          ww = i;
          break;
        }
      }
      if (ww != 25) {
        s[y][ww] = '0';

        depth(ww + 1, y, s, num);
        s[y][ww] = '1';
      }
    }
    if (y - 1 >= 0 && s[y - 1][x] != '1') {
      int ww = 25;
      for (int i = y - 1; i >= 0; i--) {
        if (s[i][x] == '3') {
          n = min(n, num);
          return;
        }
        if (s[i][x] == '1') {
          ww = i;
          break;
        }
      }
      if (ww != 25) {
        s[ww][x] = '0';

        depth(x, ww + 1, s, num);
        s[ww][x] = '1';
      }
    }
    if (y + 1 < h && s[y + 1][x] != '1') {

      // cout << y+1 <<"         " <<x <<  endl;
      int ww = 25;
      for (int i = y + 1; i < h; i++) {
        if (s[i][x] == '3') {
          n = min(n, num);
          return;
        }
        if (s[i][x] == '1') {
          ww = i;
          break;
        }
      }
      if (ww != 25) {
        s[ww][x] = '0';
        // cout << ww << endl;
        depth(x, ww - 1, s, num);
        s[ww][x] = '1';
      }
    }
  }
  return;
}

int main() {

  while (cin >> w >> h, w + h) {
    string s[h];
    REP(i, h) {
      REP(j, w) {
        string r;
        cin >> r;
        s[i] += r;
      }
    }
    n = INF;
    int x, y;
    REP(i, h) {
      REP(j, w) {
        if (s[i][j] == '2') {
          x = j;
          y = i;
        }
      }
    }

    depth(x, y, s, 0);
    if (n == INF || n > 10)
      cout << -1 << endl;
    else
      cout << n << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
const int INF = 1e9;
typedef long long ll;

ll n;
int w, h;
void depth(int x, int y, string s[], ll num) {
  num++;
  // cout << x << "    " << y <<"     " <<num << endl;
  // cout << endl;
  /*REP(i,h){
    REP(j,s[0].size()){
      cout << s[i][j];
    }
    cout << endl;
    }
    cout << endl;*/
  if (x + 1 < w && s[y][x + 1] != '1') {
    int ww = 25;
    for (int i = x + 1; i < w; i++) {
      if (s[y][i] == '3') {
        n = min(n, num);
        return;
      }
      if (s[y][i] == '1') {
        ww = i;
        break;
      }
    }
    if (ww != 25) {
      s[y][ww] = '0';

      depth(ww - 1, y, s, num);
      s[y][ww] = '1';
    }
  }
  if (x - 1 >= 0 && s[y][x - 1] != '1') {
    int ww = 25;
    for (int i = x - 1; i >= 0; i--) {
      if (s[y][i] == '3') {
        n = min(n, num);
        return;
      }
      if (s[y][i] == '1') {
        ww = i;
        break;
      }
    }
    if (ww != 25) {
      s[y][ww] = '0';

      depth(ww + 1, y, s, num);
      s[y][ww] = '1';
    }
  }
  if (y - 1 >= 0 && s[y - 1][x] != '1') {
    int ww = 25;
    for (int i = y - 1; i >= 0; i--) {
      if (s[i][x] == '3') {
        n = min(n, num);
        return;
      }
      if (s[i][x] == '1') {
        ww = i;
        break;
      }
    }
    if (ww != 25) {
      s[ww][x] = '0';

      depth(x, ww + 1, s, num);
      s[ww][x] = '1';
    }
  }
  if (y + 1 < h && s[y + 1][x] != '1') {

    // cout << y+1 <<"         " <<x <<  endl;
    int ww = 25;
    for (int i = y + 1; i < h; i++) {
      if (s[i][x] == '3') {
        n = min(n, num);
        return;
      }
      if (s[i][x] == '1') {
        ww = i;
        break;
      }
    }
    if (ww != 25) {
      s[ww][x] = '0';
      // cout << ww << endl;
      depth(x, ww - 1, s, num);
      s[ww][x] = '1';
    }
  }
  return;
}

int main() {

  while (cin >> w >> h, w + h) {
    string s[h];
    REP(i, h) {
      REP(j, w) {
        string r;
        cin >> r;
        s[i] += r;
      }
    }
    n = INF;
    int x, y;
    REP(i, h) {
      REP(j, w) {
        if (s[i][j] == '2') {
          x = j;
          y = i;
        }
      }
    }

    depth(x, y, s, 0);
    if (n == INF || n > 10)
      cout << -1 << endl;
    else
      cout << n << endl;
  }
  return 0;
}