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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define INF -(1 << 23)

using namespace std;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {-1, 0, 1, 0};

int main() {
  int H, W, K;

  scanf("%d", &H);
  scanf("%d", &W);
  scanf("%d", &K);

  vector<string> M(H);

  for (int i = 0; i < H; i++) {
    cin >> M[i];
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j] == '.') {
        M[i][j] = '0';
      }
    }
  }

  vector<vector<vector<vector<int>>>> dp(
      H, vector<vector<vector<int>>>(
             W, vector<vector<int>>(K + 1, vector<int>(4096, INF))));

  dp[0][0][0][0] = 0;

  for (int rep = 0; rep <= K; rep++) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (M[i][j] != '#') {
          for (int k = 0; k <= K; k++) {
            for (int l = 0; l < 4096; l++) {
              int dir = l % 4;

              int p6 = l / 1024;
              int p5 = (l / 256) % 4;
              int p4 = (l / 64) % 4;
              int p3 = (l / 16) % 4;
              int p2 = (l / 4) % 4;
              int p1 = l % 4;

              int px1 = j + dx[p1];
              int px2 = px1 + dx[p2];
              int px3 = px2 + dx[p3];
              int px4 = px3 + dx[p4];
              int px5 = px4 + dx[p5];
              int px6 = px5 + dx[p6];
              int py1 = i + dy[p1];
              int py2 = py1 + dy[p2];
              int py3 = py2 + dy[p3];
              int py4 = py3 + dy[p4];
              int py5 = py4 + dy[p5];
              int py6 = py5 + dy[p6];

              bool ok1 = true, ok2 = true, ok3 = true, ok4 = true, ok5 = true,
                   ok6 = true;

              if (0 <= px1 && px1 < W && 0 <= py1 && py1 < H) {
                if (M[py1][px1] > 48) {
                  ok1 = false;
                }
              }

              if (0 <= px2 && px2 < W && 0 <= py2 && py2 < H) {
                if (M[py2][px2] > 48) {
                  ok2 = false;
                }
              }

              if (0 <= px3 && px3 < W && 0 <= py3 && py3 < H) {
                if (M[py3][px3] > 48) {
                  ok3 = false;
                }
              }

              if (0 <= px4 && px4 < W && 0 <= py4 && py4 < H) {
                if (M[py4][px4] > 48) {
                  ok4 = false;
                }
              }

              if (0 <= px5 && px5 < W && 0 <= py5 && py5 < H) {
                if (M[py5][px5] > 48) {
                  ok5 = false;
                }
              }

              if (0 <= px6 && px6 < W && 0 <= py6 && py6 < H) {
                if (M[py6][px6] > 48) {
                  ok6 = false;
                }
              }

              int rate = 1;

              if ((j == px1 && i == py1 && !ok1) ||
                  (j == px2 && i == py2 && !ok2) ||
                  (j == px3 && i == py3 && !ok3) ||
                  (j == px4 && i == py4 && !ok4) ||
                  (j == px5 && i == py5 && !ok5) ||
                  (j == px6 && i == py6 && !ok6)) {
                rate = 0;
              }

              if ((0 <= j + dx[dir] && j + dx[dir] < W && 0 <= i + dy[dir] &&
                   i + dy[dir] < H) &&
                  !((dir % 3) && k == 0)) {
                for (int m = 0; m < 4; m++) {
                  int r = m * 1024 + l / 4;

                  if (dir % 3) {
                    dp[i][j][k][l] = max(
                        dp[i][j][k][l], dp[i + dy[dir]][j + dx[dir]][k - 1][r] +
                                            (M[i][j] - 48) * rate);
                  } else {
                    dp[i][j][k][l] =
                        max(dp[i][j][k][l], dp[i + dy[dir]][j + dx[dir]][k][r] +
                                                (M[i][j] - 48) * rate);
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  int ret = -1;

  for (int i = 0; i <= K; i++) {
    for (int j = 0; j < 4096; j++) {
      ret = max(ret, dp[H - 1][W - 1][i][j]);
    }
  }

  printf("%d\\n", ret);

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#pragma warning(disable : 4996)

#define INF -(1 << 23)

using namespace std;

const vector<int> dx = {0, 1, 0, -1};
const vector<int> dy = {-1, 0, 1, 0};

int main() {
  int H, W, K;

  scanf("%d", &H);
  scanf("%d", &W);
  scanf("%d", &K);

  vector<string> M(H);

  for (int i = 0; i < H; i++) {
    cin >> M[i];
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j] == '.') {
        M[i][j] = '0';
      }
    }
  }

  vector<vector<vector<vector<int>>>> dp(
      H, vector<vector<vector<int>>>(
             W, vector<vector<int>>(K + 1, vector<int>(64, INF))));

  dp[0][0][0][0] = 0;

  for (int rep = 0; rep <= K; rep++) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (M[i][j] != '#') {
          for (int k = 0; k <= K; k++) {
            for (int l = 0; l < 64; l++) {
              int dir = l % 4;

              if ((0 <= j + dx[dir] && j + dx[dir] < W && 0 <= i + dy[dir] &&
                   i + dy[dir] < H) &&
                  (!(dir % 3) && K != 0)) {
                for (int m = 0; m < 4; m++) {
                  int r = m * 16 - l / 4;

                  if (dir % 3) {
                    dp[i][j][k][l] =
                        max(dp[i][j][k][l], dp[i + dy[dir]][j + dx[dir]][k][r]);
                  } else {
                    dp[i][j][k][l] = max(
                        dp[i][j][k][l], dp[i + dy[dir]][j + dx[dir]][k - 1][r] +
                                            (M[i][j] - 48));
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  int ret = -1;

  for (int i = 0; i < K; i++) {
    for (int j = 0; j < 64; j++) {
      ret = max(ret, dp[H - 1][W - 1][i][j]);
    }
  }

  printf("%d\\n", ret);

  return 0;
}