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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int h, w, n;
  int dx[] = {0, 1};
  int dy[] = {1, 0};

  while (cin >> h >> w >> n, h || w || n) {
    bool ba[w][h];
    REP(y, h) REP(x, w) cin >> ba[x][y];
    int dp[w][h]; // dp[i][j] : n ñUàµÄ(j,i)
                  // ÉÚ®·éñ
    memset(dp, 0, sizeof(dp));
    dp[0][0] = n;
    REP(y, h) {
      REP(x, w) {
        bool d;
        if (dp[x][y] % 2) {
          d = ba[x][y];
        } else {
          d = !ba[x][y];
        }
        if (d) {
          if (x < w - 1)
            dp[x + 1][y] += (dp[x][y] + 1) / 2;
          if (y < h - 1)
            dp[x][y + 1] += dp[x][y] / 2;
        } else {
          if (x < w - 1)
            dp[x + 1][y] += dp[x][y] / 2;
          if (y < h - 1)
            dp[x][y + 1] += (dp[x][y] + 1) / 2;
        }
      }
    }
    // REP(y,h){
    //   REP(x,w) {
    //     cout<<dp[x][y]<<" ";
    //   }cout << endl;
    // }
    int x = 0, y = 0;
    while (x < w && y < h) {
      //      cout << x << " " << y << endl;
      bool d;
      if (dp[x][y] % 2) {
        d = ba[x][y];
      } else {
        d = !ba[x][y];
      }

      x += dx[d];
      y += dy[d];
    }
    cout << y + 1 << " " << x + 1 << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  typedef vector<vector<bool>> mat;
  int dx[] = {0, 1};
  int dy[] = {1, 0};
  int h, w, n;
  while (cin >> h >> w >> n, h || w || n) {
    mat ba(h, vector<bool>(w));
    REP(i, h)
    REP(j, w) {
      bool aa;
      cin >> aa;
      ba[i][j] = aa;
    }
    typedef pair<int, int> pii;
    int cnt = 0;
    set<mat> mp;
    mat now = ba;
    vector<pii> table;
    while (mp.count(now) == 0 && cnt <= n) {
      // FOR(it, now) {
      //   FOR(jt, *it)
      //     cout << *jt << " ";
      //   cout << endl;
      // }
      int nx = 0, ny = 0;
      mat tmp = now;
      while (nx != w && ny != h) {
        tmp[ny][nx] = !tmp[ny][nx];
        //                cout << now[ny][nx];
        if (now[ny][nx])
          nx++;
        else
          ny++;
        // printf("(%d,%d)",ny,nx);
      }
      table.push_back(pii(ny, nx));
      mp.insert(now);
      now = tmp;
      cnt++;
    }
    n--;
    cout << table[n % cnt].first + 1 << " " << table[n % cnt].second + 1
         << endl;
  }
}