Problem p00464 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int H, W, N;
int board[1010][1010];
int dp[1010][1010]; // N-1回までの（i,j）の通った回数を数える
int main() {
  while (cin >> H >> W >> N, H || W || N) {
    int posx, posy;
    memset(dp, 0, sizeof(dp));

    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> board[i][j];
      }
    }
    N--;
    dp[1][1] = N;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (dp[i][j] % 2 == 0) {
          dp[i][j + 1] += dp[i][j] / 2;
          dp[i + 1][j] += dp[i][j] / 2;
        } else {
          if (board[i][j] == 0) {
            dp[i + 1][j] += (dp[i][j] + 1) / 2;
            dp[i][j + 1] += (dp[i][j] - 1) / 2;
          } else {
            dp[i + 1][j] += (dp[i][j] - 1) / 2;
            dp[i][j + 1] += (dp[i][j] + 1) / 2;
          }
        }
      }
    }
    posx = posy = 1;
    while (true) {
      // cout <<posx<<" "<<posy<<endl;
      if (board[posy][posx] && dp[posy][posx] % 2)
        posy++; // 最初が東で奇数→反転
      else if (board[posy][posx] && !(dp[posy][posx] % 2))
        posx++; // 最初が東で偶数→そのまま
      else if (!board[posy][posx] && dp[posy][posx] % 2)
        posx++; // 最初が南で奇数→反転
      else
        posy++;
      if (posx >= W + 1 || posy >= H + 1)
        break;
    }
    cout << posy << " " << posx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int H, W, N;
int board[1010][1010];
int main() {
  while (cin >> H >> W >> N, H || W || N) {
    int posx, posy;
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        cin >> board[i][j];
      }
    }
    while (N) {
      posx = posy = 1;
      while (true) {
        if (posx >= W + 1 || posy >= H + 1) {
          N--;
          break;
        }
        if (board[posy][posx] == 1) {
          board[posy][posx] = 0;
          posx++;
        } else {
          board[posy][posx] = 1;
          posy++;
        }
      }
    }
    cout << posy << " " << posx << endl;
  }
  return 0;
}