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

#include <array>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

const int kForward = 1;
const int kGain = 2;
const int kLost = 3;

void Solve(double dp[51][5000], int value[51], int board[51], int goal,
           int roulette[4], int num_roulette) {
  double prob_roulette = 1.0 / num_roulette;
  dp[0][0] = 1.0;

  for (int i = 0; i < goal; i++) {
    for (int n = 0; n < num_roulette; n++) {
      int npos = (i + roulette[n] <= goal) ? i + roulette[n] : goal;
      if (board[npos] == kForward) {
        npos = (npos + value[npos] <= goal) ? npos + value[npos] : goal;
        for (int m = 0; m < 5000; m++) {
          dp[npos][m] += prob_roulette * dp[i][m];
        }
      } else if (board[npos] == kGain) {
        for (int m = 0; m < 5000; m++) {
          dp[npos][m + value[npos]] += prob_roulette * dp[i][m];
        }
      } else if (board[npos] == kLost) {
        for (int m = 0; m < 5000; m++) {
          int nm = (m - value[npos] < 0) ? 0 : m - value[npos];
          dp[npos][nm] += prob_roulette * dp[i][m];
        }
      } else {
        for (int m = 0; m < 5000; m++) {
          dp[npos][m] += prob_roulette * dp[i][m];
        }
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    double dp[51][5000] = {{0.0}};
    int board[51] = {0};
    int value[51] = {0};
    int roulette[4] = {0};
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if (X == 0) {
      return 0;
    }
    for (int i = 0; i < X; i++) {
      cin >> roulette[i];
    }

    for (int i = 0; i < Z; i++) {
      int N, E, A;
      cin >> N >> E >> A;
      board[N] = E;
      value[N] = A;
    }

    Solve(dp, value, board, Y, roulette, X);

    double sum = 0.0;
    for (int m = 1; m < 5000; m++) {
      sum += m * dp[Y][m];
    }
    cout << static_cast<int>(sum) << endl;
  }
}

Error: 0

Fixed submission:

#include <array>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

const int kForward = 1;
const int kGain = 2;
const int kLost = 3;

void Solve(double dp[51][5000], int value[51], int board[51], int goal,
           int roulette[4], int num_roulette) {
  double prob_roulette = 1.0 / num_roulette;
  dp[0][0] = 1.0;

  for (int i = 0; i < goal; i++) {
    for (int n = 0; n < num_roulette; n++) {
      int npos = (i + roulette[n] <= goal) ? i + roulette[n] : goal;
      if (board[npos] == kForward) {
        for (int m = 0; m < 5000; m++) {
          dp[npos + value[npos]][m] += prob_roulette * dp[i][m];
        }
      } else if (board[npos] == kGain) {
        for (int m = 0; m < 5000; m++) {
          dp[npos][m + value[npos]] += prob_roulette * dp[i][m];
        }
      } else if (board[npos] == kLost) {
        for (int m = 0; m < 5000; m++) {
          int nm = (m - value[npos] < 0) ? 0 : m - value[npos];
          dp[npos][nm] += prob_roulette * dp[i][m];
        }
      } else {
        for (int m = 0; m < 5000; m++) {
          dp[npos][m] += prob_roulette * dp[i][m];
        }
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (1) {
    double dp[51][5000] = {{0.0}};
    int board[51] = {0};
    int value[51] = {0};
    int roulette[4] = {0};
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if (X == 0) {
      return 0;
    }
    for (int i = 0; i < X; i++) {
      cin >> roulette[i];
    }

    for (int i = 0; i < Z; i++) {
      int N, E, A;
      cin >> N >> E >> A;
      board[N] = E;
      value[N] = A;
    }

    Solve(dp, value, board, Y, roulette, X);

    double sum = 0.0;
    for (int m = 1; m < 5000; m++) {
      sum += m * dp[Y][m];
    }
    cout << static_cast<int>(sum) << endl;
  }
}