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

#include <bits/stdc++.h>
#define M 1005
#define N 105
#define B 12
using namespace std;

int HP_init, HP_Max, R, C, S;

string s[N], dir;

int cost[N * 3], P, p[B];

int dp[M][(1 << B)], y[M], x[M], idx[N * 3];

int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};

bool solve() {

  idx['U'] = 0;
  idx['R'] = 1;

  idx['D'] = 2;
  idx['L'] = 3;

  memset(dp, -1, sizeof(dp));

  dp[0][0] = HP_init;

  y[0] = 0, x[0] = 0;

  int m = dir.size();

  for (int i = 0; i < m; i++) {

    for (int j = 0; j < (1 << P); j++) {

      if (dp[i][j] < 0)
        continue;

      y[i + 1] = y[i] + dy[idx[(int)dir[i]]];

      x[i + 1] = x[i] + dx[idx[(int)dir[i]]];

      for (int k = 0; k < P; k++) {

        if (j & (1 << k))
          continue;

        int nhp = min(HP_Max, dp[i][j] + p[k]);

        dp[i][j | (1 << k)] = max(dp[i][j | (1 << k)], nhp);

        nhp = max(0, nhp - cost[(int)s[y[i + 1]][x[i + 1]]]);

        if (!nhp)
          continue;

        dp[i + 1][j | (1 << k)] = max(dp[i + 1][j | (1 << k)], nhp);
      }

      int nhp = max(0, dp[i][j] - cost[(int)s[y[i + 1]][x[i + 1]]]);

      if (!nhp)
        continue;

      dp[i + 1][j] = max(dp[i + 1][j], nhp);
    }
  }

  for (int i = 0; i < (1 << P); i++)
    if (dp[m][i] > 0)
      return true;

  return false;
}

int main() {

  while (1) {

    cin >> HP_init >> HP_Max;

    if (!HP_init && !HP_Max)
      break;

    cin >> R >> C;

    for (int i = 0; i < R; i++)
      cin >> s[i];

    int T;

    cin >> T;

    for (int i = 0; i < T; i++) {

      char c;
      int d;

      cin >> c >> d;

      cost[(int)c] = d;
    }

    cin >> S;

    dir = "";

    for (int i = 0; i < S; i++) {

      char c;
      int num;

      cin >> c >> num;

      for (int j = 0; j < num; j++)
        dir += c;
    }

    cin >> P;

    for (int i = 0; i < P; i++)
      cin >> p[i];

    if (solve())
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define M 1005
#define N 105
#define B 12
using namespace std;

int HP_init, HP_Max, R, C, S;

string s[N], dir;

int cost[N * 3], P, p[B];

int dp[M][(1 << B)], y[M], x[M], idx[N * 3];

int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1};

bool solve() {

  idx['U'] = 0;
  idx['R'] = 1;

  idx['D'] = 2;
  idx['L'] = 3;

  memset(dp, -1, sizeof(dp));

  dp[0][0] = HP_init;

  y[0] = 0, x[0] = 0;

  int m = dir.size();

  for (int i = 0; i < m; i++) {

    for (int j = 0; j < (1 << P); j++) {

      if (dp[i][j] < 0)
        continue;

      y[i + 1] = y[i] + dy[idx[dir[i]]];

      x[i + 1] = x[i] + dx[idx[dir[i]]];

      for (int k = 0; k < P; k++) {

        if (j & (1 << k))
          continue;

        int nhp = min(HP_Max, dp[i][j] + p[k]);

        dp[i][j | (1 << k)] = max(dp[i][j | (1 << k)], nhp);

        nhp = max(0, nhp - cost[s[y[i + 1]][x[i + 1]]]);

        if (!nhp)
          continue;

        dp[i + 1][j | (1 << k)] = max(dp[i + 1][j | (1 << k)], nhp);
      }
    }
  }

  for (int i = 0; i < (1 << P); i++)
    if (dp[m][i] > 0)
      return true;

  return false;
}

int main() {

  while (1) {

    cin >> HP_init >> HP_Max;

    if (!HP_init && !HP_Max)
      break;

    cin >> R >> C;

    for (int i = 0; i < R; i++)
      cin >> s[i];

    int T;

    cin >> T;

    for (int i = 0; i < T; i++) {

      char c;
      int d;

      cin >> c >> d;

      cost[(int)c] = d;
    }

    cin >> S;

    for (int i = 0; i < S; i++) {

      char c;
      int num;

      cin >> c >> num;

      for (int j = 0; j < num; j++)
        dir += c;
    }

    cin >> P;

    for (int i = 0; i < P; i++)
      cin >> p[i];

    if (solve())
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }

  return 0;
}