Problem p00504 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; // 00右,01下,10左,11上

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int h, w, k, mask, memo[50][50][4][1 << 12];
char fld[50][50];

bool Check(int y, int x) {
  return 0 <= y && y < h && 0 <= x && x < w && fld[y][x] != '#';
}

int Max(int y, int x, int c, int bit) {
  int &res = memo[y][x][c][bit];
  if (res != -1)
    return res;
  if (y == h - 1 && x == w - 1 && c == 0)
    return res = 0;

  bool done = false;
  int py = y, px = x;
  rep(i, 0, 2 * k) {
    py -= dy[(bit >> (i * 2)) & 3];
    px -= dx[(bit >> (i * 2)) & 3];
    if (y == py && x == px) {
      done = true;
      break;
    }
  }
  int gift = 0;
  if (!done && fld[y][x] != '.')
    gift = fld[y][x] - '0';

  res = -INF;
  rep(i, 0, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (!Check(ny, nx))
      continue;
    if (i < 2) {
      res = max(res, Max(ny, nx, c, ((bit << 2) | i) & mask) + gift);
    } else if (c >= 1) {
      res = max(res, Max(ny, nx, c - 1, ((bit << 2) | i) & mask) + gift);
    }
  }

  return res;
}

void Solve() {
  fill_n((int *)memo, 50 * 50 * 4 * (1 << 12), -1);
  cin >> h >> w >> k;
  mask = (1 << (4 * k)) - 1;
  rep(i, 0, h) rep(j, 0, w) cin >> fld[i][j];

  cout << Max(0, 0, k, 0) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; // 00右,01下,10左,11上

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int h, w, k;
char fld[50][50];
int memo[50][50][4][1 << 12];

bool Check(int y, int x) {
  return 0 <= y && y < h && 0 <= x && x < w && fld[y][x] != '#';
}

int Max(int y, int x, int c, int bit) {
  int &res = memo[y][x][c][bit];
  if (res != -1)
    return res;
  if (y == h - 1 && x == w - 1 && c == 0)
    return res;

  res = 0;
  rep(i, 0, 4) {
    int ny = y + dy[i], nx = x + dx[i];
    if (!Check(ny, nx))
      continue;
    if (i < 2) {
      res = max(res, Max(ny, nx, c, ((bit << 2) & ((1 << 12) - 1)) | i));
    } else if (c >= 1) {
      res = max(res, Max(ny, nx, c - 1, ((bit << 2) & ((1 << 12) - 1)) | i));
    }
  }

  if (!('1' <= fld[y][x] && fld[y][x] <= '9'))
    return res;

  bool done = false;
  int py = y, px = x;
  rep(i, 0, 6) {
    py += -dy[(bit >> (i * 2)) & 3];
    px += -dx[(bit >> (i * 2)) & 3];
    if (y == py && x == px) {
      done = true;
      break;
    }
  }
  if (!done)
    res += fld[y][x] - '0';

  return res;
}

void Solve() {
  fill_n((int *)memo, 64 * 64 * 4 * (1 << 12), -1);
  cin >> h >> w >> k;
  rep(i, 0, h) rep(j, 0, w) cin >> fld[i][j];

  cout << Max(0, 0, k, 0) << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}