alexjercan/bugnet huggingface dataset:
Problem p00464 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;

bool grid[1000][1000];
int num[1001][1001];
int h, w, n, tmp;

int main() {
  while (scanf("%d %d %d", &h, &w, &n) && (h || w || n)) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        scanf("%d", &tmp);
        grid[i][j] = (tmp) ? true : false;
        num[i][j] = 0;
      }

    num[0][0] = n - 1;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        int N = num[i][j];
        if (grid[i][j]) {
          num[i + 1][j] += N / 2;
          num[i][j + 1] += N - N / 2;
        } else {
          num[i][j + 1] += N / 2;
          num[i + 1][j] += N - N / 2;
        }
        if (N & 1)
          grid[i][j] = !grid[i][j];
      }
    }

    int y = 0, x = 0;
    while (y < h && x < w) {
      if (grid[y][x])
        x++;
      else
        y++;
    }
    printf("%d %d\\n", y + 1, x + 1);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
using namespace std;

bool grid[1000][1000];
int num[1000][1000];
int h, w, n, tmp;

int main() {
  while (scanf("%d %d %d", &h, &w, &n) && (h || w || n)) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        scanf("%d", &tmp);
        grid[i][j] = (tmp) ? true : false;
        num[i][j] = 0;
      }

    num[0][0] = n - 1;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int N = num[i][j];
        if (grid[i][j]) {
          num[i + 1][j] += N / 2;
          num[i][j + 1] += N - N / 2;
        } else {
          num[i][j + 1] += N / 2;
          num[i + 1][j] += N - N / 2;
        }
        if (N & 1)
          grid[i][j] = ~grid[i][j];
      }

    int y = 0, x = 0;
    while (y < h && x < w) {
      if (grid[y][x])
        x++;
      else
        y++;
    }
    printf("%d %d\\n", y + 1, x + 1);
  }
}
Problem p00464 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {

  static int h, w, n, grid[1002][1002], dp[1002][1002];

  while (cin >> h >> w >> n, h | w | n) {
    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> grid[i][j];

    fill(dp[0], dp[1002], 0);
    dp[1][1] = n - 1;

    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        dp[i][j + 1] += (dp[i][j] + grid[i][j]) >> 1;
        dp[i + 1][j] += (dp[i][j] + !grid[i][j]) >> 1;
      }
    }

    for (int i = 1; i <= h; i++) {
      for (int j = 0; j <= w; j++) {
        if (dp[i][j] % 2)
          grid[i][j] ^= dp[i][j] & 1;
      }
    }

    int x = 1, y = 1;
    while (true) {
      if (y > h || x > w)
        break;
      if (grid[y][x])
        x++;
      else
        y++;
    }

    cout << y << " " << x << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {

  static int h, w, n, grid[1001][1001], dp[1001][1001];

  while (cin >> h >> w >> n, h | w | n) {
    for (int i = 1; i <= h; i++)
      for (int j = 1; j <= w; j++)
        cin >> grid[i][j];

    fill(dp[0], dp[1001], 0);
    dp[1][1] = n - 1;

    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        dp[i][j + 1] += (dp[i][j] + grid[i][j]) >> 1;
        dp[i + 1][j] += (dp[i][j] + !grid[i][j]) >> 1;
      }
    }

    for (int i = 1; i <= h; i++) {
      for (int j = 0; j <= w; j++) {
        if (dp[i][j] % 2)
          grid[i][j] ^= dp[i][j] & 1;
      }
    }

    int x = 1, y = 1;
    while (true) {
      if (y > h || x > w)
        break;
      if (grid[y][x])
        x++;
      else
        y++;
    }

    cout << y << " " << x << endl;
  }

  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define fr first
#define sc second
int main() {
  int h, w, n, mas[1002][1002], dp[1002][1002], x, y;
  while (cin >> h >> w >> n, h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> mas[i][j];
        dp[i][j] = 0;
      }
    }
    dp[0][0] = n - 1;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (i < h - 1)
          dp[i + 1][j] += (dp[i][j] + (mas[i][j] == 1 ? 0 : 1)) / 2;
        if (j < w - 1)
          dp[i][j + 1] += (dp[i][j] + (mas[i][j] == 0 ? 0 : 1)) / 2;
      }
    }
    x = 0, y = 0;
    while (x != h && y != w)
      (dp[x][y] + mas[x][y]) % 2 == 0 ? x++ : y++;
    cout << x + 1 << " " << y + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define fr first
#define sc second
int main() {
  int h, w, n, mas[1002][1002], x, y;
  while (cin >> h >> w >> n, h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> mas[i][j];
      }
    }
    for (int i = 0; i < n; i++) {
      x = 0, y = 0;
      while (x != h && y != w) {
        if (mas[x][y] == 1) {
          mas[x][y] = 0;
          y++;
        } else {
          mas[x][y] = 1;
          x++;
        }
      }
    }
    cout << x + 1 << " " << y + 1 << endl;
  }
}
Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

const int Height = 1000;
const int Width = 1000;
bool b_map[Height][Width];
int i_map[Height + 1][Width + 1];

int main() {
  int h, w, n;
  while (cin >> h >> w >> n, h | w | n) {
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        cin >> b_map[y][x];
        i_map[y][x] = 0;
      }
    for (int y = 0; y < h; y++)
      i_map[y][w] = 0;
    for (int x = 0; x < w; x++)
      i_map[h][x] = 0;
    i_map[0][0] = n - 1;
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        if (!i_map[y][x])
          continue;
        int num = i_map[y][x] >> 1;
        if (i_map[y][x] & 1) {
          if (b_map[y][x]) {
            i_map[y][x + 1] += num + 1;
            i_map[y + 1][x] += num;
          } else {
            i_map[y][x + 1] += num;
            i_map[y + 1][x] += num + 1;
          }
          b_map[y][x] = !b_map[y][x];
        } else {
          i_map[y + 1][x] += num;
          i_map[y][x + 1] += num;
        }
      }
    int x, y;
    x = y = 0;
    do {
      if (b_map[y][x])
        x++;
      else
        y++;
    } while (x < w && y < h);
    cout << y + 1 << ' ' << x + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

const int Height = 1000;
const int Width = 1000;
bool map[Height][Width];

int main() {
  int h, w, n;
  while (cin >> h >> w >> n, h | w | n) {
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
        cin >> map[y][x];
      }
    int x, y;
    while (n--) {
      x = y = 0;
      do {
        if (map[y][x])
          map[y][x++] = false;
        else
          map[y++][x] = true;
      } while (x < w && y < h);
    }
    cout << y + 1 << ' ' << x + 1 << endl;
  }
  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int h, w, n;
int dp[1005][1005];

int main() {
  while (cin >> h >> w >> n) {
    if (h == 0 && w == 0 && n == 0)
      break;

    vector<vector<int>> v(h, vector<int>(w));
    rep(i, h) {
      rep(j, w) { cin >> v[i][j]; }
    }

    memset(dp, 0, sizeof(dp));
    dp[0][0] = n;

    rep(i, h) {
      rep(j, w) {
        int s = dp[i][j] / 2;
        int t = dp[i][j] - s;

        if (v[i][j] % 2 == 1) {
          dp[i][j + 1] += t;
          dp[i + 1][j] += s;
        } else {
          dp[i + 1][j] += t;
          dp[i][j + 1] += s;
        }
      }
    }

    int y = 0, x = 0;
    while (y != h && x != w) {
      if (v[y][x] == 1) {
        if (dp[y][x] % 2 == 1) {
          x++;
        } else {
          y++;
        }
      } else {
        if (dp[y][x] % 2 == 1) {
          y++;
        } else {
          x++;
        }
      }
    }

    cout << y + 1 << " " << x + 1 << endl;
    // rep(i, h) {
    // 	rep(j, w) {
    // 		cout << dp[i][j] << " ";
    // 	}
    // 	cout << endl;
    // }
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int h, w, n;
vector<vector<int>> v(1000, vector<int>(1000));
vector<vector<int>> t(1000, vector<int>(1000));
map<int, P> m;

int main() {
  while (cin >> h >> w >> n) {
    if (h == 0 && w == 0 && n == 0)
      break;

    m.clear();

    rep(i, h) {
      rep(j, w) {
        cin >> v[i][j];
        t[i][j] = v[i][j];
      }
    }

    int cnt = 0;

    while (true) {
      int y = 0, x = 0;
      while (y != h && x != w) {
        if (v[y][x] == 1) {
          v[y][x] = 0;
          x++;
        } else {
          v[y][x] = 1;
          y++;
        }
      }

      m[cnt + 1] = mp(y, x);

      bool flag = true;
      rep(i, h) {
        rep(j, w) {
          if (t[i][j] == v[i][j])
            continue;
          flag = false;
        }
      }

      if (flag)
        break;
      cnt++;
    }

    P p = m[n % cnt];
    cout << p.first + 1 << " " << p.second + 1 << endl;
  }

  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int H, W, N;
  int cMap[1013][1013];
  int Map[1013][1013];
  while (cin >> H >> W >> N && H && W && N) {
    for (int i = 0; i < H + 3; i++)
      for (int J = 0; J < W + 3; J++)
        cMap[J][i] = 0;
    if (H + W + N == 0)
      break;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> Map[i][j];
      }
    }
    cMap[1][1] = N; // 奇数ならそのまま偶数なら別
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {

        cMap[i][j + 1] += cMap[i][j] / 2;
        cMap[i + 1][j] += cMap[i][j] / 2;
        if (cMap[i][j] % 2 != 0) {
          if (Map[i][j] == 1) {
            cMap[i][j + 1]++;
          } else {
            cMap[i + 1][j]++;
          }
        }
      }
    }

    int x, y;
    x = y = 1;
    while (1 <= y && y <= H && 1 <= x && x <= W) {
      if (cMap[y][x] % 2 == 0) {
        if (Map[y][x] == 1)
          y++;
        else
          x++;
      } else {
        if (Map[y][x] == 1)
          x++;
        else
          y++;
      }
    }
    cout << y << " " << x << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int H, W, N;
  int cMap[100][100];
  int Map[100][100];
  while (true) {

    for (int i = 0; i < 1100; i++)
      for (int J = 0; J < 1100; J++)
        cMap[J][i] = 0;
    cin >> H >> W >> N;
    if (H + W + N == 0)
      break;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> Map[i][j];
      }
    }
    cMap[1][1] = N; // 奇数ならそのまま偶数なら別
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {

        cMap[i][j + 1] += cMap[i][j] / 2;
        cMap[i + 1][j] += cMap[i][j] / 2;
        if (cMap[i][j] % 2 != 0) {
          if (Map[i][j] == 1) {
            cMap[i][j + 1]++;
          } else {
            cMap[i + 1][j]++;
          }
        }
      }
    }

    int x, y;
    x = y = 1;
    while (1 <= y && y <= H && 1 <= x && x <= W) {
      if (cMap[y][x] % 2 == 0) {
        if (Map[y][x] == 1)
          y++;
        else
          x++;
      } else {
        if (Map[y][x] == 1)
          x++;
        else
          y++;
      }
    }
    cout << y << " " << x << endl;
  }

  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Runtime Error


#include <stdio.h>
#include <string.h>

int board[1234][1234];
int mark[1234][1234];

int main() {
  for (;;) {
    int h, w, n;
    scanf("%d %d %d", &h, &w, &n);
    if (h == 0 && w == 0 && n == 0)
      break;
    memset(board, 255, sizeof(board));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        scanf("%d", &board[i][j]);
      }
    }
    memset(mark, 0, sizeof(mark));
    mark[0][0] = n - 1;
    for (int sum = 0; sum < w + h - 1; ++sum) {
      for (int vr = 0; vr <= sum; ++vr) {
        if (vr >= h)
          break;
        int hr = sum - vr;
        if (hr >= w)
          continue;
        if (hr < 0)
          break;
        if (board[vr][hr] == 0) {
          mark[vr][hr + 1] += mark[vr][hr] / 2;
          mark[vr + 1][hr] += mark[vr][hr] / 2 + (mark[vr][hr] % 2);
        } else {
          mark[vr][hr + 1] += mark[vr][hr] / 2 + (mark[vr][hr] % 2);
          mark[vr + 1][hr] += mark[vr][hr] / 2;
        }
        if (mark[vr][hr] % 2) {
          board[vr][hr] = (board[vr][hr] == 0) ? 1 : 0;
        }
      }
    }
    int py = 0, px = 0;
    while (board[py][px] >= 0) {
      if (board[py][px] == 0) {
        ++py;
      } else {
        ++px;
      }
    }
    printf("%d %d\\n", py + 1, px + 1);
  }
  return 0;
}

Error: 0

Fixed submission:


#include <stdio.h>
#include <string.h>

int board[1234][1234];
int mark[1234][1234];

int main() {
  for (;;) {
    int h, w, n;
    scanf("%d %d %d", &h, &w, &n);
    if (h == 0 && w == 0 && n == 0)
      break;
    memset(board, 255, sizeof(board));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        scanf("%d", &board[i][j]);
      }
    }
    memset(mark, 0, sizeof(mark));
    mark[0][0] = n - 1;
    for (int sum = 0; sum < w + h - 1; ++sum) {
      for (int vr = 0; vr <= sum; ++vr) {
        int hr = sum - vr;
        if (hr >= w)
          continue;
        if (hr < 0)
          break;
        if (board[vr][hr] == 0) {
          mark[vr][hr + 1] += mark[vr][hr] / 2;
          mark[vr + 1][hr] += mark[vr][hr] / 2 + (mark[vr][hr] % 2);
        } else {
          mark[vr][hr + 1] += mark[vr][hr] / 2 + (mark[vr][hr] % 2);
          mark[vr + 1][hr] += mark[vr][hr] / 2;
        }
        if (mark[vr][hr] % 2) {
          board[vr][hr] = (board[vr][hr] == 0) ? 1 : 0;
        }
      }
    }
    int py = 0, px = 0;
    while (board[py][px] >= 0) {
      if (board[py][px] == 0) {
        ++py;
      } else {
        ++px;
      }
    }
    printf("%d %d\\n", py + 1, px + 1);
  }
  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int a[1000][1000], dp[1000][1000];
int main() {
  int h, w, n;
  while (scanf("%d%d%d", &h, &w, &n), h) {
    rep(i, h) rep(j, w) scanf("%d", &a[i][j]);
    memset(dp, 0, sizeof(dp));
    dp[0][0] = n - 1;
    rep(i, h) rep(j, w) {
      if (i) {
        if (a[i - 1][j])
          dp[i][j] += dp[i - 1][j] / 2;
        else
          dp[i][j] += (dp[i - 1][j] + 1) / 2;
      }
      if (j) {
        if (a[i][j - 1])
          dp[i][j] += (dp[i][j - 1] + 1) / 2;
        else
          dp[i][j] += dp[i][j - 1] / 2;
      }
    }
    rep(i, h) rep(j, w) {
      if (dp[i][j] & 1)
        a[i][j] = !a[i][j];
    }
    int x = 0, y = 0;
    while (x < h && y < w) {
      if (a[x][y])
        y++;
      else
        x++;
    }
    printf("%d %d\\n", x + 1, y + 1);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int a[1000][1000];
int main() {
  int h, w, n;
  while (scanf("%d%d%d", &h, &w, &n), h) {
    rep(i, h) rep(j, w) scanf("%d", &a[i][j]);
    rep(i, n) {
      int x = 0, y = 0;
      while (x < h && y < w) {
        a[x][y] = !a[x][y];
        if (a[x][y])
          x++;
        else
          y++;
      }
      if (i == n - 1)
        printf("%d %d\\n", x + 1, y + 1);
    }
  }
}
Problem p00464 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> pi;

int f[1001][1001];
int ct[1001][1001];

int main() {
  int H, W, N;
  while (scanf(" %d %d %d", &H, &W, &N), H) {
    rep(i, H) rep(j, W) scanf(" %d", &f[i][j]);
#define IN(x, y) (0 <= x && x < W && 0 <= y && y < H)

    memset(ct, 0, sizeof(ct));
    ct[0][0] = N - 1;

    int dx[2] = {0, 1}, dy[2] = {1, 0};
    rep(s, H + W + 1) {
      int l = max(0, s - H), r = min(W - 1, s);
      for (int x = l; x <= r; ++x) {
        int y = s - x;
        rep(i, 2) {
          int nx = x + dx[i], ny = y + dy[i];
          if (!IN(nx, ny))
            continue;

          int nct = ct[y][x] / 2;
          if (ct[y][x] % 2 == 1 && f[y][x] == i)
            ++nct;

          ct[ny][nx] += nct;
        }
      }
    }

    int X = 0, Y = 0;
    while (IN(X, Y)) {
      int d = (f[Y][X] + ct[Y][X]) % 2;
      X += dx[d];
      Y += dy[d];
    }
    printf("%d %d\\n", Y + 1, X + 1);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \\
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second

typedef pair<int, int> pi;

int f[1001][1001];
int ct[1001][1001];

int main() {
  int H, W, N;
  while (scanf(" %d %d %d", &H, &W, &N), H) {
    rep(i, H) rep(j, W) scanf(" %d", &f[i][j]);

    queue<pi> que;
    que.push(pi(0, 0));
    ct[0][0] = N - 1;

#define IN(x, y) (0 <= x && x < W && 0 <= y && y < H)
    int dx[2] = {0, 1}, dy[2] = {1, 0};
    while (!que.empty()) {
      pi p = que.front();
      que.pop();

      int x = p.se, y = p.fi;
      rep(i, 2) {
        int nx = x + dx[i], ny = y + dy[i];
        if (IN(nx, ny)) {
          int nct = ct[y][x] / 2;
          if (ct[y][x] % 2 == 1 && f[y][x] == i)
            ++nct;

          ct[ny][nx] = nct;
          que.push(pi(ny, nx));
        }
      }
    }

    int X = 0, Y = 0;
    while (IN(X, Y)) {
      int d = (f[Y][X] + ct[Y][X]) % 2;
      // printf("X,Y %d %d  : f= %d, ct= %d -> d= %d\\n",
      // X,Y,f[Y][X],ct[Y][X],d);

      X += dx[d];
      Y += dy[d];
    }
    printf("%d %d\\n", Y + 1, X + 1);
  }
  return 0;
}
Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
// constexpr int MAX_N = 1005;
constexpr int MAX_H = 1005;
constexpr int dx[] = {0, 1};
constexpr int dy[] = {1, 0};

int h, w, n, fld[MAX_H][MAX_H], dp[MAX_H][MAX_H];

bool Solve() {
  cin >> h >> w >> n;
  if (!h)
    return false;

  memset(fld, -1, sizeof(fld));
  memset(dp, 0, sizeof(dp));

  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      cin >> fld[x][y];
    }
  }

  dp[1][1] = n - 1;
  for (int y = 1; y <= h; ++y) {
    for (int x = (y == 1 ? 2 : 1); x <= w; ++x) {
      for (int i = 0; i < 2; ++i) {
        int tx = x - dx[i], ty = y - dy[i];
        dp[x][y] +=
            dp[tx][ty] / 2 + (dp[tx][ty] % 2 && fld[tx][ty] == i ? 1 : 0);
      }
    }
  }

  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      fld[x][y] += dp[x][y];
      fld[x][y] %= 2;
    }
  }

  {
    int x = 1, y = 1;

    while (fld[x][y] != -1) {
      int nx = x + dx[fld[x][y]];
      y += dy[fld[x][y]];
      x = nx;
    }

    cout << y << " " << x << endl;
  }

  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while (Solve())
    ;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> ppiii;

constexpr int INF = 1LL << 60;
constexpr int MOD = 1000000007;
// constexpr int MAX_N = 1005;
constexpr int MAX_H = 1005;
constexpr int dx[] = {0, 1};
constexpr int dy[] = {1, 0};

int h, w, n, fld[MAX_H][MAX_H], dp[MAX_H][MAX_H];

void Sim(int x, int y, int k) {
  if (fld[x][y] == -1) {
    cout << y << " " << x << endl;
  }

  Sim(x + dx[fld[x][y]], y + dy[fld[x][y]], k);
}

bool Solve() {
  cin >> h >> w >> n;
  if (!h)
    return false;

  memset(fld, -1, sizeof(fld));

  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      cin >> fld[x][y];
    }
  }

  dp[1][1] = n - 1;
  for (int y = 1; y <= h; ++y) {
    for (int x = (y == 1 ? 2 : 1); x <= w; ++x) {
      for (int i = 0; i < 2; ++i) {
        int tx = x - dx[i], ty = y - dy[i];
        dp[x][y] =
            dp[tx][ty] / 2 + (dp[tx][ty] % 2 && fld[tx][ty] == i) ? 1 : 0;
      }
    }
  }

  for (int y = 1; y <= h; ++y) {
    for (int x = 1; x <= w; ++x) {
      fld[x][y] += dp[x][y];
      fld[x][y] %= 2;
    }
  }

  Sim(1, 1, n);

  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  while (Solve())
    ;
}