Problem p00069 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }
bool reachble(vector<vint> str, int s, int e);
bool plusReachble(vector<vint> stair, int d, int m, int g);

int n;

bool reachble(vector<vint> str, int s, int e) {
  rep(i, str.size()) {
    if (s < n - 1 && str[i][s])
      s++;
    else if (0 < s && str[i][s - 1])
      s--;
  }

  return s == e;
}

bool plusReachble(vector<vint> stair, int d, int m, int g) {
  rep(i, d) rep(j, n - 1) if (!stair[i][j] && (j == 0 || !stair[i][j - 1]) &&
                              (j == n - 2 || !stair[i][j + 1])) {
    stair[i][j] = 1;
    if (reachble(stair, m, g)) {
      cout << i + 1 << " " << j + 1 << endl;
      return true;
    }
    stair[i][j] = 0;
  }
  return false;
}

int main() {
  while (cin >> n && n) {
    int m, g, d;
    cin >> m >> g >> d;
    m--;
    g--;

    vector<vint> stair(d, vint(n - 1));

    rep(i, d) {
      string bln;
      cin >> bln;

      rep(j, n - 1) stair[i][j] = bln[j] - '0';
    }

    if (reachble(stair, m, g))
      cout << 0 << endl;
    else if (plusReachble(stair, d, m, g))
      ;
    else
      cout << 1 << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int n;

bool reachble(vector<vint> str, int s, int e) {
  rep(i, str.size()) {
    if (s < n - 1 && str[i][s])
      s++;
    else if (0 < s && str[i][s - 1])
      s--;
  }

  return s == e;
}

int main() {
  while (cin >> n && n) {
    int m, g, d;
    cin >> m >> g >> d;
    m--;
    g--;

    vector<vint> stair(d, vint(n - 1));

    rep(i, d) {
      string bln;
      cin >> bln;

      rep(j, n) stair[i][j] = bln[j] - '0';
    }

    if (reachble(stair, m, g))
      cout << 0 << endl;
    else {
      bool flag = false;
      rep(i, d) {
        rep(j, n - 1) {
          if (!stair[i][j]) {
            stair[i][j] = 1;
            if (reachble(stair, m, g)) {
              cout << i + 1 << " " << j + 1 << endl;
              flag = true;
              break;
            }
            stair[i][j] = 0;
          }
        }
        if (flag)
          break;
      }
      if (!flag)
        cout << 1 << endl;
    }
  }
}