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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
// ライブラリゾーン！！！！
typedef long long ll;
typedef long double ld;
#define str string
#define rep(i, j) for (ll i = 0; i < (long long)(j); i++)
const ll Mod = 1000000007;
const ll gosenchou = 5000000000000000;
short gh[2][4] = {{0, 0, -1, 1}, {-1, 1, 0, 0}};
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { // 隣接リスト表現
  ll to, cost;
};
struct E { // 辺の情報を入れる変数
  ll from, to, cost;
};
bool operator<(E a, E b) { return a.cost < b.cost; }
struct H {
  ll x, y;
};
bool operator<(H a, H b) {
  if (a.x != b.x)
    return a.x < b.x;
  return a.y < b.y;
}
bool operator>(H a, H b) {
  if (a.x != b.x)
    return a.x > b.x;
  return a.y > b.y;
}
bool operator==(H a, H b) { return a.x == b.x && a.y == b.y; }
bool operator!=(H a, H b) { return a.x != b.x || a.y != b.y; }
ll gcm(ll i, ll j) { // 最大公約数
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcm(j % i, i);
}
ld rad(H a, H b) {
  return sqrt(pow(a.x - b.x, 2.0) + pow(a.y - b.y, 2.0));
} // rad＝座標上の2点間の距離
ll ari(ll a, ll b, ll c) { return (a + b) * c / 2; } // 等差数列の和
bool suf(ld a, ld b, ld c, ld d) {
  if (b <= c || d <= a)
    return 0;
  return 1;
} //[a,b),[c,d)
ll fact(ll i, ll z) {
  ll sum = 1;
  for (ll j = max(z, 2LL); j <= i; j++)
    sum = (sum * j) % Mod;
  return sum;
} // 階乗(正）
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % Mod;
    x = x * x % p;
    n >>= 1;
  }
  return res;
}
#define int long long
const long long inf = 4523372036854775807;
const int iinf = 1500000000;
//----------------------------------------------------
//++++++++++++++++++++++++++++++++++++++++++++++++++++
int h, w, n;
bool a[1000][1000];
int dp[1001][1001];
H ans;
void solve(int x, int y) {
  if (x == h || y == w) {
    ans = H{x, y};
    return;
  }
  if (a[x][y])
    solve(x, y + 1);
  else
    solve(x + 1, y);
}
signed main() {
  while (cin >> h >> w >> n && h + w + n > 0) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> a[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 (a[i][j]) {
          dp[i][j + 1] += ceil(dp[i][j] / 2.0);
          dp[i + 1][j] += floor(dp[i][j] / 2.0);
        } else {
          dp[i + 1][j] += ceil(dp[i][j] / 2.0);
          dp[i][j + 1] += floor(dp[i][j] / 2.0);
        }
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (dp[i][j] % 2 == 1) {
          a[i][j] = !a[i][j];
        }
      }
    }
    solve(0, 0);
    cout << ans.x + 1 << " " << ans.y + 1 << endl;
  }
  getchar();
  getchar();
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//---------------------------------------------------
//????????????????????????????????????
#define int long long
#define str string
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
typedef long long ll;
typedef long double ld;
const ll inf = 4523372036854775807;
struct P {
  ll pos, cost;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct B { //???£??\\????????????¨???
  ll to, cost;
};
struct S { //????????????±?????\\????????????°
  int from, to, cost;
};
struct H {
  int x, y;
};
ll gcd(ll i, ll j) { //??????§???¬??´????°
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcd(j % i, i);
}
ld rad(ld a, ld b, ld c, ld d) {
  return sqrt(pow(a - c, 2) + pow(b - d, 2));
} // rad??????§??¨???????2???????????????¢
//---------------------------------------------------
//+++++++++++++++++++++++++++++++++++++++++++++++++++
struct A {
  int x, y;
  string s;
};
bool operator<(A a, A b) { return a.s < b.s; }
bool operator>(A a, A b) { return a.s > b.s; }
priority_queue<A, vector<A>, greater<A>> p;
bool b[1000][1000];
int h, w, n;
vector<H> c;
signed main() {
  while (cin >> h >> w >> n && h + w + n > 0) {
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> b[i][j];
    c.clear();
    p.push(A{0, 0, ""});
    while (!p.empty()) {
      A t = p.top();
      p.pop();
      if (t.x == h || t.y == w) {
        c.push_back(H{t.x, t.y});
        continue;
      }
      if (b[t.x][t.y]) {
        p.push(A{t.x, t.y + 1, t.s + "0"});
        p.push(A{t.x + 1, t.y, t.s + "1"});
      } else {
        p.push(A{t.x + 1, t.y, t.s + "0"});
        p.push(A{t.x, t.y + 1, t.s + "1"});
      }
    }
    int k = c.size();
    k = n % k;
    cout << c[k].x + 1 << " " << c[k].y + 1 << endl;
    for (int i = 0; i < c.size(); i++) {
      cout << c[i].x << " " << c[i].y << endl;
    }
  }
  getchar();
  getchar();
}