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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int grid[1005][1005];
int dp[1005][1005];

int main() {
  //	cut here before submit
  //	freopen ("testcase.walk", "r", stdin );
  int h, w, n;
  while (scanf("%d %d %d", &h, &w, &n), h, w, n) {
    memset(grid, false, sizeof(grid));
    rep(i, h) {
      rep(j, w) {
        int in;
        scanf("%d", &grid[i][j]); // 1 : 東 0 : 南
      }                           // end rep
    }                             // end rep
    memset(dp, 0, sizeof(dp));
    dp[0][0] = n - 1;
    for (int i = 1; i < h; i++) {
      int k = dp[i - 1][0];
      dp[i][0] =
          (k % 2 == 0 ? k / 2 : (!grid[i - 1][0] ? (k + 1) / 2 : (k - 1) / 2));
    } // end for
    for (int j = 1; j < w; j++) {
      int m = dp[0][j - 1];
      dp[0][j] =
          (m % 2 == 0 ? m / 2 : (grid[0][j - 1] ? (m + 1) / 2 : (m - 1) / 2));
    } // end for

    for (int i = 1; i < h; i++) {
      for (int j = 1; j < w; j++) {
        int k = dp[i - 1][j], m = dp[i][j - 1];
        dp[i][j] +=
            (k % 2 == 0 ? k / 2
                        : (!grid[i - 1][j] ? (k + 1) / 2 : (k - 1) / 2));
        dp[i][j] +=
            (m % 2 == 0 ? m / 2 : (grid[i][j - 1] ? (m + 1) / 2 : (m - 1) / 2));
      } // end for
    }   // end for

    int row = 0, col = 0;
    while (row < h && col < w) {
      if ((dp[row][col] + grid[row][col]) % 2 == 0) {
        row++;
      } else {
        col++;
      } // end if
    }   // end while

    printf("%d %d\\n", row + 1, col + 1);
  } // end loop

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
bool grid[1005][1005];

int main() {
  //	cut here before submit
  //	freopen ("testcase.walk", "r", stdin );
  int h, w, n;
  while (scanf("%d %d %d", &h, &w, &n), h, w, n) {
    memset(grid, false, sizeof(grid));
    rep(i, h) {
      rep(j, w) {
        int in;
        scanf("%d", &in);
        grid[i][j] = (in == 1 ? true : false);
      } // end rep
    }   // end rep
    int row = 0, col = 0;
    while (n--) {
      row = 0, col = 0;
      while (row < h && col < w) {
        if (grid[row][col]) { // 1 なら 東
          grid[row][col] = false;
          col++;
        } else { // 0 なら 南
          grid[row][col] = true;
          row++;
        } // end if
      }   // end while
    }     // end while
    printf("%d %d\\n", row + 1, col + 1);
  } // end loop

  return 0;
}