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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;
int amida[35][12];

typedef long long ll;
typedef pair<int, int> P;

void disp_amida(int row, int col) {
  rep(i, row) {
    rep(j, col) { cerr << amida[i][j]; } // end rep
    cerr << endl;
  } // end rep
}

int do_amida(int row, int curr) {
  rep(i, row) {
    //		cerr << "curr: " << curr << endl;
    if (amida[i][curr] == 1) {
      curr++;
    } else if (amida[i][curr - 1] == 1) {
      curr--;
    } // end if
  }   // end rep

  //	cerr << "curr: " << curr << endl;

  return curr;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.DL2", "r", stdin );
  int n, m, b, d;
  while (scanf("%d", &n), n) {
    memset(amida, 0, sizeof(amida));
    scanf("%d %d %d", &m, &b, &d);
    int res = 1;
    P ans;
    rep(i, d) {
      for (int j = 1; j <= n - 1; j++) {
        scanf("%1d", &amida[i][j]);
      } // end rep
    }   // end rep

    vector<P> cand;
    cand.clear();
    rep(i, d) {
      for (int j = 1; j <= n - 1; j++) {
        if (amida[i][j] == 0 && amida[i][j + 1] == 0 && amida[i][j - 1] == 0) {
          cand.push_back(P(i, j));
        } // end if
      }   // end for
    }     // end rep
          /*
                          rep (i, cand.size() ){
                                  cerr << cand[i].first << ':' << cand[i].second <<
             endl;       } // end rep
          */
    int start = m;
    int end = do_amida(d, start);
    if (end == b)
      res = 0;
    if (res != 0) {
      rep(i, cand.size()) {
        int r = cand[i].first;
        int c = cand[i].second;
        amida[r][c] = 1;
        end = do_amida(d, start);
        if (end == b) {
          res = -1;
          ans = P(r, c);
          break;
        } // end if
        amida[r][c] = 0;
      } // end rep
    }   // end if
    if (res != -1) {
      cout << res << endl;
    } else {
      cout << ans.first + 1 << ' ' << ans.second << endl;
    } // end if
  }   // end loop

  //	printf ("%d\\n", res );

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;
int amida[12][35];

typedef long long ll;
typedef pair<int, int> P;

void disp_amida(int row, int col) {
  rep(i, row) {
    rep(j, col) { cerr << amida[i][j]; } // end rep
    cerr << endl;
  } // end rep
}

int do_amida(int row, int curr) {
  rep(i, row) {
    //		cerr << "curr: " << curr << endl;
    if (amida[i][curr] == 1) {
      curr++;
    } else if (amida[i][curr - 1] == 1) {
      curr--;
    } // end if
  }   // end rep

  //	cerr << "curr: " << curr << endl;

  return curr;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.DL2", "r", stdin );
  int n, m, b, d;
  while (scanf("%d", &n), n) {
    memset(amida, 0, sizeof(amida));
    scanf("%d %d %d", &m, &b, &d);
    int res = -1;
    P ans;
    rep(i, d) {
      for (int j = 1; j <= n - 1; j++) {
        scanf("%1d", &amida[i][j]);
      } // end rep
    }   // end rep

    vector<P> cand;
    rep(i, d) {
      for (int j = 1; j <= n - 1; j++) {
        if (amida[i][j] == 0 && amida[i][j + 1] == 0 && amida[i][j - 1] == 0) {
          cand.push_back(P(i, j));
        } // end if
      }   // end for
    }     // end rep
          /*
                          rep (i, cand.size() ){
                                  cerr << cand[i].first << ':' << cand[i].second <<
             endl;       } // end rep
          */
    int start = m;
    int end = do_amida(d, start);
    if (end == b)
      res = 0;
    if (res != 0) {
      rep(i, cand.size()) {
        int r = cand[i].first;
        int c = cand[i].second;
        amida[r][c] = 1;
        int s = m;
        int e = do_amida(d, s);
        if (e == b) {
          res = 1;
          ans = P(r, c);
          break;
        } // end if
        amida[r][c] = 0;
      } // end rep
    }   // end if
    if (res != 1) {
      cout << res << endl;
    } else {
      cout << ans.first + 1 << ' ' << ans.second << endl;
    } // end if
  }   // end loop

  //	printf ("%d\\n", res );

  return 0;
}