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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define endl "\\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\\n", x)

string sw[30];

signed main(void) {
  int i, j, k, l;
  for (int testcase = 0;; testcase++) {
    int(n);
    if (!n)
      break;
    int3(m, s, d);
    m--;
    s--;

    rep(i, d) sw[i].clear();
    rep(i, d) {
      sc(sw[i]);
      sw[i] = "0" + sw[i] + "0";
    }

    int lis[d + 1][n][2];

    rep(j, n) lis[0][j][0] = j;
    rep(i, d) {
      rep(j, n) lis[i + 1][j][0] = lis[i][j][0];
      rep1(j, sw[i].size() - 1) if (sw[i][j] == '1')
          swap(lis[i + 1][j][0], lis[i + 1][j - 1][0]);
    }

    rep(j, n) lis[d][j][1] = j;
    per(i, d) {
      rep(j, n) lis[i][j][1] = lis[i + 1][j][1];
      rep1(j, sw[i].size() - 1) if (sw[i][j] == '1')
          swap(lis[i][j][1], lis[i][j - 1][1]);
    }

    rep(k, n) if (m == lis[d][k][0]) break;
    if (k == s)
      pri(0);
    else {
      bool fl = false;
      rep(i, d) {
        fl = false;
        rep1(j, n) if (sw[i][j] + sw[i][j - 1] + sw[i][j + 1] == '0' * 3) {
          rep(k, n) if (m == lis[i][k][0]) break;
          if (k + 1 == j)
            k++;
          else if (k == j)
            k--;
          else
            continue;
          if (lis[i + 1][k][1] == s) {
            fl = true;
            break;
          }
        }
        if (fl)
          break;
      }
      if (fl)
        pri2(i + 1, j);
      else
        pri(1);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for (i = 0; i < n; ++i)
#define rep1(i, n) for (i = 1; i < n; ++i)
#define rep2d(i, j, n)                                                         \\
  for (i = 0; i < n; ++i)                                                      \\
    for (j = i + 1; j < n; ++j)
#define per(i, n) for (i = n - 1; i > -1; --i)
#define int(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define int2(x, y)                                                             \\
  int x, y;                                                                    \\
  scanf("%d%d", &x, &y)
#define int3(x, y, z)                                                          \\
  int x, y, z;                                                                 \\
  scanf("%d%d%d", &x, &y, &z)
#define sc(x) cin >> x
#define sc2(x, y) cin >> x >> y
#define sc3(x, y, z) cin >> x >> y >> z
#define scn(n, a) rep(i, n) cin >> a[i]
#define sc2n(n, a, b) rep(i, n) cin >> a[i] >> b[i]
#define pri(x) cout << x << "\\n"
#define pri2(x, y) cout << x << " " << y << "\\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\\n", x)

string sw[10];

signed main(void) {
  int i, j, k, l;
  for (int testcase = 0;; testcase++) {
    int(n);
    if (!n)
      break;
    int3(m, s, d);
    m--;
    s--;

    rep(i, d) sw[i].clear();
    rep(i, d) {
      sc(sw[i]);
      sw[i] = "0" + sw[i] + "0";
    }

    int lis[d + 1][n][2];

    rep(j, n) lis[0][j][0] = j;
    rep(i, d) {
      rep(j, n) lis[i + 1][j][0] = lis[i][j][0];
      rep1(j, sw[i].size() - 1) if (sw[i][j] == '1')
          swap(lis[i + 1][j][0], lis[i + 1][j - 1][0]);
    }

    rep(j, n) lis[d][j][1] = j;
    per(i, d) {
      rep(j, n) lis[i][j][1] = lis[i + 1][j][1];
      rep1(j, sw[i].size() - 1) if (sw[i][j] == '1')
          swap(lis[i][j][1], lis[i][j - 1][1]);
    }

    if (lis[0][m][0] == s)
      pri(0);
    else {
      bool fl = false;
      rep(i, d) {
        fl = false;
        rep1(j, n) if (sw[i][j] + sw[i][j - 1] + sw[i][j + 1] == '0' * 3) {
          rep(k, n) if (m == lis[i][k][0]) break;
          if (k + 1 == j)
            k++;
          else if (k + 2 == j)
            k--;
          else
            continue;
          if (lis[i + 1][k][1] == s) {
            fl = true;
            break;
          }
        }
        if (fl)
          break;
      }
      if (fl)
        pri2(i + 1, j);
      else
        pri(1);
    }
  }
  return 0;
}