Problem p00725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pi;
#define MK make_pair
#define F first
#define S second

int a[50][50];
const int MX = 1e6;

int search(int x, int y, int w) {
  int k = MX;
  if (w == 0) {
    return k;
  }
  if (a[x][y + 1] != 1) {
    for (int i = 1;; i++) {
      if (a[x][y + i] == -1) {
        break;
      }
      if (a[x][y + i] == 1) {
        a[x][y + i] = 0;
        k = min(k, search(x, y + i - 1, w - 1) + 1);
        a[x][y + i] = 1;
        break;
      }
      if (a[x][y + i] == 3) {
        return 0;
      }
    }
  }
  if (a[x + 1][y] != 1) {
    for (int i = 1;; i++) {
      if (a[x + i][y] == -1) {
        break;
      }
      if (a[x + i][y] == 1) {
        a[x + i][y] = 0;
        k = min(k, search(x + i - 1, y, w - 1) + 1);
        a[x + i][y] = 1;
        break;
      }
      if (a[x + i][y] == 3) {
        return 0;
      }
    }
  }
  if (a[x][y - 1] != 1) {
    for (int i = -1;; i--) {
      if (a[x][y + i] == -1) {
        break;
      }
      if (a[x][y + i] == 1) {
        a[x][y + i] = 0;
        k = min(k, search(x, y + i + 1, w - 1) + 1);
        a[x][y + i] = 1;
        break;
      }
      if (a[x][y + i] == 3) {
        return 0;
      }
    }
  }
  if (a[x - 1][y] != 1) {
    for (int i = -1;; i--) {
      if (a[x + i][y] == -1) {
        break;
      }
      if (a[x + i][y] == 1) {
        a[x + i][y] = 0;
        k = min(k, search(x + i + 1, y, w - 1) + 1);
        a[x + i][y] = 1;
        break;
      }
      if (a[x + i][y] == 3) {
        return 0;
      }
    }
  }

  return k;
}

int main() {
  while (1) {

    int w, h;
    cin >> w >> h;
    fill(&a[0][0], &a[49][50], -1);
    int x, y;
    for (int i = 1; i <= h; i++) {
      for (int t = 1; t <= w; t++) {
        cin >> a[i][t];
        if (a[i][t] == 2) {
          x = i;
          y = t;
        }
      }
    }

    int ans = search(x, y, 10);
    cout << (ans == MX ? -1 : ans + 1) << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> pi;
#define MK make_pair
#define F first
#define S second

int a[50][50];
const int MX = 1e6;

int search(int x, int y, int w) {
  int k = MX;
  if (w == 0) {
    return k;
  }
  if (a[x][y + 1] != 1) {
    for (int i = 1;; i++) {
      if (a[x][y + i] == -1) {
        break;
      }
      if (a[x][y + i] == 1) {
        a[x][y + i] = 0;
        k = min(k, search(x, y + i - 1, w - 1) + 1);
        a[x][y + i] = 1;
        break;
      }
      if (a[x][y + i] == 3) {
        return 0;
      }
    }
  }
  if (a[x + 1][y] != 1) {
    for (int i = 1;; i++) {
      if (a[x + i][y] == -1) {
        break;
      }
      if (a[x + i][y] == 1) {
        a[x + i][y] = 0;
        k = min(k, search(x + i - 1, y, w - 1) + 1);
        a[x + i][y] = 1;
        break;
      }
      if (a[x + i][y] == 3) {
        return 0;
      }
    }
  }
  if (a[x][y - 1] != 1) {
    for (int i = -1;; i--) {
      if (a[x][y + i] == -1) {
        break;
      }
      if (a[x][y + i] == 1) {
        a[x][y + i] = 0;
        k = min(k, search(x, y + i + 1, w - 1) + 1);
        a[x][y + i] = 1;
        break;
      }
      if (a[x][y + i] == 3) {
        return 0;
      }
    }
  }
  if (a[x - 1][y] != 1) {
    for (int i = -1;; i--) {
      if (a[x + i][y] == -1) {
        break;
      }
      if (a[x + i][y] == 1) {
        a[x + i][y] = 0;
        k = min(k, search(x + i + 1, y, w - 1) + 1);
        a[x + i][y] = 1;
        break;
      }
      if (a[x + i][y] == 3) {
        return 0;
      }
    }
  }

  return k;
}

int main() {
  while (1) {

    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    fill(&a[0][0], &a[49][50], -1);
    int x, y;
    for (int i = 1; i <= h; i++) {
      for (int t = 1; t <= w; t++) {
        cin >> a[i][t];
        if (a[i][t] == 2) {
          x = i;
          y = t;
        }
      }
    }

    int ans = search(x, y, 10);
    cout << (ans == MX ? -1 : ans + 1) << endl;
  }

  return 0;
}