Problem p00464 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define DEB 0

int field[128][128];
int dp[128][128];
int h, w, n;

int main() {

  while (scanf("%d%d%d", &h, &w, &n), h | w | n) {
    memset(dp, 0, sizeof(dp));
    rep(i, h) {
      rep(j, w) { scanf("%d", &field[i + 1][j + 1]); }
    }
    dp[1][1] = n - 1;
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) {
        int k1, k2;
        if (dp[i - 1][j] % 2 && field[i - 1][j] == 0) {
          k1 = 1;
        } else {
          k1 = 0;
        }
        if (dp[i][j - 1] % 2 && field[i][j - 1] == 1) {
          k2 = 1;
        } else {
          k2 = 0;
        }

        dp[i][j] += (dp[i - 1][j] + k1) / 2 + (dp[i][j - 1] + k2) / 2;
      }
    }
#if DEB
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) { printf("%2d ", dp[i][j]); }
      puts("");
    }
#endif

    // Nツ嘉アツ姪堋づ個湘ウツ妥板づーツ青カツ青ャ
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) { field[i][j] = (field[i][j] + dp[i][j]) % 2; }
    }
#if DEB
    puts("field");
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) { printf("%2d ", field[i][j]); }
      puts("");
    }
#endif

    int x = 1;
    int y = 1;
    for (;;) {
      if (x > w || y > h)
        break;
      if (field[y][x] == 1) {
        x++;
      } else {
        y++;
      }
    }

    printf("%d %d\n", y, x);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define DEB 0

int field[1024][1024];
int dp[1024][1024];
int h, w, n;

int main() {

  while (scanf("%d%d%d", &h, &w, &n), h | w | n) {
    memset(dp, 0, sizeof(dp));
    rep(i, h) {
      rep(j, w) { scanf("%d", &field[i + 1][j + 1]); }
    }
    dp[1][1] = n - 1;
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) {
        int k1, k2;
        if (dp[i - 1][j] % 2 && field[i - 1][j] == 0) {
          k1 = 1;
        } else {
          k1 = 0;
        }
        if (dp[i][j - 1] % 2 && field[i][j - 1] == 1) {
          k2 = 1;
        } else {
          k2 = 0;
        }

        dp[i][j] += (dp[i - 1][j] + k1) / 2 + (dp[i][j - 1] + k2) / 2;
      }
    }
#if DEB
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) { printf("%2d ", dp[i][j]); }
      puts("");
    }
#endif

    // Nツ嘉アツ姪堋づ個湘ウツ妥板づーツ青カツ青ャ
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) { field[i][j] = (field[i][j] + dp[i][j]) % 2; }
    }
#if DEB
    puts("field");
    REP(i, 1, h + 1) {
      REP(j, 1, w + 1) { printf("%2d ", field[i][j]); }
      puts("");
    }
#endif

    int x = 1;
    int y = 1;
    for (;;) {
      if (x > w || y > h)
        break;
      if (field[y][x] == 1) {
        x++;
      } else {
        y++;
      }
    }

    printf("%d %d\n", y, x);
  }
  return 0;
}