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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int main() {
  while (1) {
    static int h, w, n;
    static int a[1005][1005] = {};

    scanf("%d%d%d", &h, &w, &n);
    if (h == 0)
      break;
    rep1(i, h) {
      rep1(j, w) { scanf("%d", &a[i][j]); }
    }

    static int dp[1005][1005];
    rep(i, 1005) rep(j, 1005) dp[i][j] = 0;
    dp[1][1] = n - 1;

    for (int i = 2; i <= w + h; i++) {
      rep1(j, h) {
        if (i - j < 1)
          break;
        if (i - j > w)
          continue;
        dp[j + 1][i - j] += dp[j][i - j] / 2;
        dp[j][i - j + 1] += dp[j][i - j] / 2;
        if (dp[j][i - j] & 1) {
          dp[j + dir_4[a[j][i - j]][0]][i - j + dir_4[a[j][i - j]][1]]++;
          a[j][i - j] ^= 1;
        }
      }
    }

    int x = 1, y = 1;
    while (x <= h && y <= w) {
      if (a[x][y]) {
        y++;
      } else
        x++;
    }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int main() {
  while (1) {
    static int h, w, n;
    static int a[1005][1005] = {};

    scanf("%d%d%d", &h, &w, &n);
    rep1(i, h) {
      rep1(j, w) { scanf("%d", &a[i][j]); }
    }

    static int dp[1005][1005] = {};
    dp[1][1] = n - 1;

    for (int i = 2; i <= w + h; i++) {
      rep1(j, h) {
        if (i - j < 1)
          break;
        if (i - j > w)
          continue;
        dp[j + 1][i - j] += dp[j][i - j] / 2;
        dp[j][i - j + 1] += dp[j][i - j] / 2;
        if (dp[j][i - j] & 1) {
          dp[j + dir_4[a[j][i - j]][0]][i - j + dir_4[a[j][i - j]][1]]++;
          a[j][i - j] ^= 1;
        }
      }
    }

    int x = 1, y = 1;
    while (x <= h && y <= w) {
      if (a[x][y]) {
        y++;
      } else
        x++;
    }

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