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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

int memo[10][10][10][10][51];
int mp[10][10];
int W, H;
int M, Olim, O;

int search(int ny, int nx, int l, int r, int ox) {
  if (ox <= 0)
    return (INT_MAX / 2);
  if (ny == H - 1)
    return (0);
  if (memo[ny][nx][l][r][ox] >= 0)
    return (memo[ny][nx][l][r][ox]);

  int m, o;
  int res = INT_MAX / 2;

  if (ox == 1)
    return (INT_MAX / 2);

  if (nx > 0) {
    m = o = 0;
    if (l > nx - 1) {
      if (mp[ny][nx - 1] < 0)
        m = -mp[ny][nx - 1];
      else
        o = mp[ny][nx - 1];
    }
    res = min(res,
              search(ny, nx - 1, min(l, nx - 1), r, min(Olim, ox + o - 1)) + m);
  }

  if (nx < W - 1) {
    m = o = 0;
    if (nx + 1 > r) {
      if (mp[ny][nx + 1] < 0)
        m = -mp[ny][nx + 1];
      else
        o = mp[ny][nx + 1];
    }
    res = min(res,
              search(ny, nx + 1, l, max(r, nx + 1), min(Olim, ox + o - 1)) + m);
  }

  m = o = 0;
  if (mp[ny + 1][nx] < 0)
    m = -mp[ny + 1][nx];
  else
    o = mp[ny + 1][nx];
  res = min(res, search(ny + 1, nx, nx, nx, min(Olim, ox + o - 1)) + m);

  return (memo[ny][nx][l][r][ox] = res);
}

int main() {
  while (scanf("%d %d", &W, &H) && W) {
    scanf("%d %d %d", &M, &Olim, &O);

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &mp[i][j]);
      }
    }

    int a = INT_MAX / 2;

    memset(memo, -1, sizeof(memo));
    for (int i = 0; i < W; i++) {
      if (O <= 1)
        break;
      int m = 0, o = 0;
      if (mp[0][i] < 0)
        m = -mp[0][i];
      else
        o = mp[0][i];
      a = min(a, search(0, i, i, i, min(Olim, O + o - 1)) + m);
    }

    if (a == INT_MAX / 2 || a > M)
      printf("NA\\n");
    else
      printf("%d\\n", a);
  }

  return (0);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

int memo[10][10][10][10][50];
int mp[10][10];
int W, H;
int M, Olim, O;

int search(int ny, int nx, int l, int r, int ox) {
  if (ox == 0)
    return (INT_MAX / 2);
  if (ny == H - 1)
    return (0);
  if (memo[ny][nx][l][r][ox] >= 0)
    return (memo[ny][nx][l][r][ox]);

  int m, o;
  int res = INT_MAX / 2;

  if (ox == 1)
    return (INT_MAX / 2);

  if (nx > 0) {
    m = o = 0;
    if (!(l <= nx - 1 && nx - 1 <= r)) {
      if (mp[ny][nx - 1] < 0)
        m = -mp[ny][nx - 1];
      else
        o = mp[ny][nx - 1];
    }
    res = min(res,
              search(ny, nx - 1, min(l, nx - 1), r, min(Olim, ox + o - 1)) + m);
  }

  if (nx < W - 1) {
    m = o = 0;
    if (!(l <= nx + 1 && nx + 1 <= r)) {
      if (mp[ny][nx + 1] < 0)
        m = -mp[ny][nx + 1];
      else
        o = mp[ny][nx + 1];
    }
    res = min(res,
              search(ny, nx + 1, l, max(r, nx + 1), min(Olim, ox + o - 1)) + m);
  }

  m = o = 0;
  if (mp[ny + 1][nx] < 0)
    m = -mp[ny + 1][nx];
  else
    o = mp[ny + 1][nx];
  res = min(res, search(ny + 1, nx, nx, nx, min(Olim, ox + o - 1)) + m);

  return (memo[ny][nx][l][r][ox] = res);
}

int main() {
  while (scanf("%d %d", &W, &H) && W) {
    scanf("%d %d %d", &M, &Olim, &O);

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &mp[i][j]);
      }
    }

    int a = INT_MAX / 2;

    memset(memo, -1, sizeof(memo));
    for (int i = 0; i < W; i++) {
      int m = 0, o = 0;
      if (mp[0][i] < 0)
        m = -mp[0][i];
      else
        o = mp[0][i];
      a = min(a, search(0, i, i, i, min(Olim, O + o - 1)) + m);
    }

    if (a == INT_MAX / 2 || a > M)
      printf("NA\\n");
    else
      printf("%d\\n", a);
  }

  return (0);
}