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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);

#define INF 1010000000

int masu[1001][1001];
int dp[1001][1001];

int n, m, k;
int xx, yy;

void saiki(int x, int y, int masu[1001][1001]) {
  // printf("%d %d\\n",y,x);
  if (x >= m || y >= n) {
    xx = x + 1;
    yy = y + 1;
    return;
  }

  if (masu[x][y] == 1) {
    saiki(x + 1, y, masu);
  } else {
    saiki(x, y + 1, masu);
  }
}

int main() {
  while (1) {
    sinttt(n, m, k);
    if (n == 0)
      break;
    rep(i, n) {
      rep(j, m) { sint(masu[j][i]); }
    }

    rep(i, 1001) { rep(j, 1001) dp[i][j] = 0; }

    dp[0][0] = k - 1;
    rep(j, n) {
      rep(i, m) {
        if (dp[i][j] % 2 == 0) {
          dp[i][j + 1] += dp[i][j] / 2;
          dp[i + 1][j] += dp[i][j] / 2;
        } else {
          if (masu[i][j] == 1) {
            dp[i][j + 1] += dp[i][j] / 2;
            dp[i + 1][j] += dp[i][j] / 2 + 1;
          } else {
            dp[i][j + 1] += dp[i][j] / 2 + 1;
            dp[i + 1][j] += dp[i][j] / 2;
          }
        }
      }
    }

    rep(j, n) {
      rep(i, m) {
        if (dp[i][j] % 2 == 1) {
          masu[i][j] = !masu[i][j];
        }
      }
    }

    saiki(0, 0, masu);
    printf("%d %d\\n", yy, xx);

    /*

    puts("");
    rep(i,n){
            rep(j,m){
                    printf("%d ",dp[j][i]);
            }puts("");
    }
    puts("");
    rep(i,n){
            rep(j,m){
                    printf("%d ",masu[j][i]);
            }puts("");
    }*/
  }
}
/*
3 4 2
1 0 1 1
0 1 0 0
1 0 1 0
0 0 0

*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define sint(i) scanf("%d", &i);
#define sintt(i, j) scanf("%d%d", &i, &j);
#define sinttt(i, j, k) scanf("%d%d%d", &i, &j, &k);

#define INF 1010000000

int masu[1000][1000];
int dp[1000][1000];

int n, m, k;
int xx, yy;

void saiki(int x, int y, int masu[1000][1000]) {

  if (x > n || y > m) {
    xx = x + 1;
    yy = y + 1;
    return;
  }

  if (masu[x][y] == 1) {
    saiki(x + 1, y, masu);
  } else {
    saiki(x, y + 1, masu);
  }
}

int main() {
  while (1) {
    sinttt(n, m, k);
    if (n == 0)
      break;
    rep(i, n) {
      rep(j, m) { sint(masu[j][i]); }
    }

    rep(i, 1000) { rep(j, 1000) dp[i][j] = 0; }

    dp[0][0] = k - 1;
    rep(i, n) {
      rep(j, m) {
        if (dp[i][j] % 2 == 0) {
          dp[i][j + 1] += dp[i][j] / 2;
          dp[i + 1][j] += dp[i][j] / 2;
        } else {
          if (masu[i][j] == 1) {
            dp[i][j + 1] += dp[i][j] / 2;
            dp[i + 1][j] += dp[i][j] / 2 + 1;
          } else {
            dp[i][j + 1] += dp[i][j] / 2 + 1;
            dp[i + 1][j] += dp[i][j] / 2;
          }
        }
      }
    }

    rep(i, n) {
      rep(j, m) {
        if (dp[i][j] % 2 == 1) {
          masu[i][j] = !masu[i][j];
        }
      }
    }

    saiki(0, 0, masu);
    printf("%d %d\\n", yy, xx);
    /*
    puts("");
    rep(i,n){
            rep(j,m){
                    printf("%d ",masu[j][i]);
            }puts("");
    }*/
  }
}