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


#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int h, w, k;
char board[100][100];

int getboard(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w) {
    return -1;
  }
  return (board[i][j] == '#')
             ? -1
             : ((board[i][j] == '.') ? 0 : (board[i][j] - '0'));
}

const int PMAX = 1 << 12;
int memo[50][50][4][PMAX];
const int hsy[4] = {1, 0, -1, 0};
const int hsx[4] = {0, 1, 0, -1};
const int INF = 123456789;
int dp(int i, int j, int x, int path) {
  if (x > k || x < 0)
    return -INF;
  if (i < 0 || j < 0 || i >= h || j >= w)
    return -INF;
  if (memo[i][j][x][path] != -1)
    return memo[i][j][x][path];
  int ans = -INF;
  int kk = x;
  if (!(getboard(i, j) < 0)) {
    for (int d = 0; d < 4; ++d) {
      if (d >= 2) {
        kk = x - 1;
        if (kk < 0)
          continue;
      }
      int d2 = d << 10;
      int idp = (path >> 2) % (1 << 10) + d2;
      int cans = dp(i - hsy[path % 4], j - hsx[path % 4], kk, idp);
      int u = hsy[path % 4], v = hsx[path % 4];
      bool flag = false;
      for (int bit = 0; bit < 6; ++bit) {
        u += hsy[idp % 4];
        v += hsx[idp % 4];
        idp /= 4;
        if (u == 0 && v == 0)
          flag = true;
      }
      if (!flag)
        cans += getboard(i, j);
      ans = max(ans, cans);
    }
  }
  return memo[i][j][x][path] = ans;
}

int main(void) {
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 0; i < h; ++i)
    scanf("%s", board[i]);
  memset(memo, 0xFF, sizeof(memo));
  memo[0][0][0][0] = 0;
  int ans = 0;
  for (int x = 0; x <= k; ++x) {
    for (int p = 0; p < PMAX; ++p) {
      int p2 = p;
      int bcount = 0;
      for (int bit = 0; bit < 6; ++bit) {
        if (p2 % 4 >= 2)
          ++bcount;
        p2 /= 4;
      }
      if (bcount > k - x)
        continue;
      ans = max(ans, dp(h - 1, w - 1, x, p));
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: -11

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int h, w, k;
char board[50][51];

int getboard(int i, int j) {
  if (i < 0 || j < 0 || i >= h || j >= w) {
    return -1;
  }
  return (board[i][j] == '#')
             ? -1
             : ((board[i][j] == '.') ? 0 : (board[i][j] - '0'));
}

const int PMAX = 1 << 12;
int memo[50][50][4][PMAX];
const int hsy[4] = {1, 0, -1, 0};
const int hsx[4] = {0, 1, 0, -1};
const int INF = 123456789;
int dp(int i, int j, int x, int path) {
  if (x > k || x < 0)
    return -INF;
  if (i < 0 || j < 0)
    return -INF;
  if (memo[i][j][x][path] != -1)
    return memo[i][j][x][path];
  int ans = -INF;
  int kk = x;
  if (!(getboard(i, j) < 0)) {
    for (int d = 0; d < 4; ++d) {
      if (d >= 2) {
        kk = x - 1;
        if (kk < 0)
          continue;
      }
      int d2 = d << 10;
      int idp = (path >> 2) % (1 << 10) + d2;
      int cans = dp(i - hsy[path % 4], j - hsx[path % 4], kk, idp);
      int u = hsy[path % 4], v = hsx[path % 4];
      bool flag = false;
      for (int bit = 0; bit < 6; ++bit) {
        u += hsy[idp % 4];
        v += hsx[idp % 4];
        idp /= 4;
        if (u == 0 && v == 0)
          flag = true;
      }
      if (!flag)
        cans += getboard(i, j);
      ans = max(ans, cans);
    }
  }
  return memo[i][j][x][path] = ans;
}

int main(void) {
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 0; i < h; ++i)
    scanf("%s", board[i]);
  memset(memo, 0xFF, sizeof(memo));
  memo[0][0][0][0] = 0;
  int ans = 0;
  for (int x = 0; x <= k; ++x) {
    for (int p = 0; p < PMAX; ++p) {
      int p2 = p;
      int bcount = 0;
      for (int bit = 0; bit < 6; ++bit) {
        if (p2 % 4 >= 2)
          ++bcount;
        p2 /= 4;
      }
      if (bcount > k - x)
        continue;
      ans = max(ans, dp(h - 1, w - 1, x, p));
    }
  }
  printf("%d\\n", ans);
  return 0;
}