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

#include "bits/stdc++.h"

using namespace std;

// #define int long long
#define PB push_back
#define MK make_pair
#define MKT make_tuple
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

// constexpr int INF = 1LL<<60;
constexpr int INF = 1 << 28;
// constexpr int MOD = 100000;
constexpr int MAX_N = 52;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};

int mem[MAX_N][MAX_N][4][1 << 6][1 << 6], h, w, k, fld[MAX_N][MAX_N];

inline bool Chk(int x, int y, int mask1, int mask2) {
  constexpr int len = 6;
  int cx = x, cy = y;
  for (int i = 0; i < len; ++i) {
    int b1 = (mask1 & (1 << (len - 1 - i))) ? 1 : 0,
        b2 = (mask2 & (1 << (len - 1 - i))) ? 1 : 0;
    int idx = b1 + 2 * b2;
    cx -= dx[idx];
    cy -= dy[idx];
    if (x == cx && y == cy) {
      return true;
    }
  }
  return false;
}

int Solve(int x, int y, int c, int mask1, int mask2) {
  if (x == w + 1 && y == h)
    return 0;
  if (mem[x][y][c][mask1][mask2] != -1)
    return mem[x][y][c][mask1][mask2];

  int cur = Chk(x, y, mask1, mask2) ? 0 : fld[x][y];
  int res = 0;
  bool flg = false;
  for (int i = 0; i < 4; ++i) {
    if (1 < i && k <= c)
      break;
    int tx = x + dx[i], ty = y + dy[i];
    int nc = (1 < i) ? c + 1 : c;
    if (fld[tx][ty] == -1)
      continue;

    int nm1 = (mask1 >> 1) + ((i % 2) << 5),
        nm2 = (mask2 >> 1) + ((1 < i) << 5);
    int tmp = Solve(tx, ty, nc, nm1, nm2);
    if (tmp <= -INF) {
      continue;
    }
    flg = true;
    res = max(res, tmp);
  }

  if (!flg) {
    res = -INF;
    cur = 0;
  }

  return mem[x][y][c][mask1][mask2] = res + cur;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  memset(fld, -1, sizeof(fld));
  cin >> h >> w >> k;
  for (int y = 1; y <= h; ++y) {
    string s;
    cin >> s;
    for (int x = 1; x <= w; ++x) {
      char c = s[x - 1];
      if (c == '#') {
        fld[x][y] = -1;
        continue;
      } else if (c == '.') {
        fld[x][y] = 0;
        continue;
      }
      fld[x][y] = c - '0';
    }
  }
  fld[w + 1][h] = 0;
  memset(mem, -1, sizeof(mem));
  cout << max(0, Solve(1, 1, 0, 0, 0)) << endl;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

// #define int long long
#define PB push_back
#define MK make_pair
#define MKT make_tuple
#define ALL(V) V.begin(), V.end()
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> pipi;

// constexpr int INF = 1LL<<60;
constexpr int INF = 1 << 28;
// constexpr int MOD = 100000;
constexpr int MAX_N = 52;
constexpr int dx[] = {1, 0, -1, 0};
constexpr int dy[] = {0, 1, 0, -1};

int mem[MAX_N][MAX_N][4][1 << 7][1 << 7], h, w, k, fld[MAX_N][MAX_N];

inline bool Chk(int x, int y, int mask1, int mask2) {
  constexpr int len = 6;
  int cx = x, cy = y;
  cout << "start" << x << " " << y << endl;
  for (int i = 0; i < len; ++i) {
    int b1 = mask1 & (1 << (len - 1 - i)) ? 1 : 0,
        b2 = mask2 & (1 << (len - 1 - i)) ? 1 : 0;
    int idx = b1 + 2 * b2;
    cx -= dx[idx];
    cy -= dy[idx];
    if (x == cx && y == cy) {
      return true;
    }
  }
  return false;
}

int Solve(int x, int y, int c, int mask1, int mask2) {
  if (x == w + 1 && y == h)
    return 0;
  if (mem[x][y][c][mask1][mask2] != -1)
    return mem[x][y][c][mask1][mask2];

  int cur = Chk(x, y, mask1, mask2) ? 0 : fld[x][y];
  int res = 0;
  for (int i = 0; i < 4; ++i) {
    if (1 < i && k <= c)
      break;
    int tx = x + dx[i], ty = y + dy[i];
    int nc = (1 < i) ? c + 1 : c;
    if (fld[tx][ty] == -1)
      continue;

    int nm1 = (mask1 >> 1) + ((i % 2) << 5),
        nm2 = (mask2 >> 1) + ((1 < i) << 5);
    res = max(res, Solve(tx, ty, nc, nm1, nm2));
  }

  return mem[x][y][c][mask1][mask2] = res + cur;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  memset(fld, -1, sizeof(fld));
  cin >> h >> w >> k;
  for (int y = 1; y <= h; ++y) {
    string s;
    cin >> s;
    for (int x = 1; x <= w; ++x) {
      char c = s[x - 1];
      if (c == '#') {
        fld[x][y] = -1;
        continue;
      } else if (c == '.') {
        fld[x][y] = 0;
        continue;
      }
      fld[x][y] = c - '0';
    }
  }
  fld[w + 1][h] = 0;
  memset(mem, -1, sizeof(mem));
  cout << Solve(1, 1, 0, 0, 0) << endl;
}