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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1000000;

int w, h;
int f, m, o;
vector<vector<int>> cell;

enum { Y, X };
const int NEXT[3][2]{{1, 0}, {0, -1}, {0, 1}};

vector<vector<vector<vector<int>>>> memo;

int dfs(int y, int x, int air, int used, int cost) {
  if (memo[y][x][air][used] <= cost)
    return INF;
  memo[y][x][air][used] = cost;
  //     if(y == h) return cost;
  //     if(air == 0) return INF;

  //     cout<<""<<y<<","<<x<<"    air:"<<air<<"    cost:"<<cost<<""<<endl;

  int result = INF;
  for (const auto &d : NEXT) {
    int ny = y + d[Y];
    int nx = x + d[X];
    int nair = air - 1;
    int nused = y == ny ? 0 : used;

    if (h < ny || nx < 0 || w <= nx)
      continue;
    if (ny == h) {
      result = min(result, cost);
      continue;
    }
    if (nair == 0)
      continue;

    if (cell[ny][nx] == 0)
      result = min(result, dfs(ny, nx, nair, nused, cost));
    else if (cell[ny][nx] < 0)
      result = min(result, dfs(ny, nx, nair, nused, cost - cell[ny][nx]));
    else {
      if ((nused & (1 << nx)) == 0) {
        nair = min(nair + cell[ny][nx], m);
        nused |= 1 << nx;
      }
      result = min(result, dfs(ny, nx, nair, nused, cost));
    }
  }
  return result;
}

int main() {
  while (cin >> w >> h, w | h) {
    cin >> f >> m >> o;
    cell = vector<vector<int>>(h, vector<int>(w));
    for (auto &line : cell)
      for (auto &v : line)
        cin >> v;
    cell.push_back(vector<int>(w, 0));

    int answer = INF;
    for (int x = 0; x < w; ++x) {
      // cout<<"go"<<endl;
      int y = 0;
      int cost = 0;
      int air = o - 1;
      int used = 0;
      memo = vector<vector<vector<vector<int>>>>(
          h + 1, vector<vector<vector<int>>>(
                     w, vector<vector<int>>(51, vector<int>(1 << 10, INF))));
      if (air < 1)
        continue;
      if (cell[y][x] < 0)
        cost -= cell[y][x];
      if (cell[y][x] > 0) {
        air = min(air + cell[y][x], m);
        used |= 1 << x;
      }
      answer = min(answer, dfs(y, x, air, used, cost));
    }
    if (answer > f)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;

const int INF = 1000000;

int w, h;
int f, m, o;
vector<vector<int>> cell;

enum { Y, X, AIR };
const int NEXT[3][2]{{1, 0}, {0, -1}, {0, 1}};
typedef tuple<int, int, int> Memo;

vector<vector<vector<vector<int>>>> memo;

int dfs(int y, int x, int air, int used, int cost) {
  if (memo[y][x][air][used] <= cost)
    return INF;
  memo[y][x][air][used] = cost;
  if (y == h)
    return 0;
  if (air == 0)
    return INF;
  int result = INF;
  for (const auto &d : NEXT) {
    int ny = y + d[Y];
    int nx = x + d[X];
    if (ny < 0 || h < ny || nx < 0 || w <= nx)
      continue;
    int nair = air - 1;
    int nused = y == ny ? 0 : used;
    if (cell[ny][nx] == 0)
      result = min(result, dfs(ny, nx, nair, nused, cost));
    else if (cell[ny][nx] < 0)
      result = min(result, dfs(ny, nx, nair, nused, cost - cell[ny][nx]) -
                               cell[ny][nx]);
    else {
      if (nused & (1 << nx)) {
      } else {
        nair = min(nair + cell[ny][nx], m);
        nused |= 1 << nx;
      }
      result = min(result, dfs(ny, nx, nair, nused, cost));
    }
  }
  return result;
}

int main() {
  while (cin >> w >> h, w | h) {
    cin >> f >> m >> o;
    cell = vector<vector<int>>(h, vector<int>(w));
    for (auto &line : cell)
      for (auto &v : line)
        cin >> v;
    cell.push_back(vector<int>(w, 0));

    int answer = INF;
    for (int x = 0; x < w; ++x) {
      int y = 0;
      int cost = 0;
      int air = o - 1;
      int used = 0;
      memo = vector<vector<vector<vector<int>>>>(
          h + 1, vector<vector<vector<int>>>(
                     w, vector<vector<int>>(51, vector<int>(1 << 10, INF))));
      if (cell[y][x] < 0)
        cost -= cell[y][x];
      if (cell[y][x] > 0) {
        air = min(air + cell[y][x], m);
        used |= 1 << x;
      }
      answer = min(answer, dfs(y, x, air, used, cost) + cost);
    }
    if (answer > f)
      cout << "NA" << endl;
    else
      cout << answer << endl;
  }
}