From e33f9fcc09b172b86bf6b178a1920c9191290994 Mon Sep 17 00:00:00 2001 From: Anneshu Nag <132702983+NK-Works@users.noreply.github.com> Date: Sun, 10 Nov 2024 01:06:11 +0530 Subject: [PATCH] added dice roll program --- .../Dice Roll Problem/README.md | 34 +++++++++++++++ .../Dice Roll Problem/program.c | 41 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Dynamic Programming/Dice Roll Problem/README.md create mode 100644 Dynamic Programming/Dice Roll Problem/program.c diff --git a/Dynamic Programming/Dice Roll Problem/README.md b/Dynamic Programming/Dice Roll Problem/README.md new file mode 100644 index 00000000..9bb9474f --- /dev/null +++ b/Dynamic Programming/Dice Roll Problem/README.md @@ -0,0 +1,34 @@ +# Dice Roll Sum Problem + +## Description + +The **Dice Roll Sum** problem is a dynamic programming challenge that asks how many ways you can achieve a given target sum by rolling a certain number of dice. Each die has a fixed number of faces (e.g., 6), and each face has an equal probability of appearing. The objective is to calculate the number of distinct ways to reach the target sum using the given dice. + +### Problem Statement +Given: +- `n`: the number of dice, +- `target`: the target sum, +- `faces`: the number of faces on each die (default is 6). + +Find the number of ways to achieve exactly the target sum by rolling the dice. + +### Example + +**Input:** +- Number of dice: `2` +- Target sum: `7` + +**Output:** +- Number of ways to reach target sum: `6` + +**Explanation:** +With 2 dice, there are six ways to achieve a sum of 7: +- (1,6), (2,5), (3,4), (4,3), (5,2), (6,1) + +## Solution Approach + +We use **dynamic programming** to build up the solution by breaking down the problem into smaller subproblems: +1. Define a 2D DP array `dp[i][j]` where `i` is the number of dice and `j` is the target sum. +2. Initialize `dp[0][0] = 1`, representing one way to achieve a sum of 0 with 0 dice. +3. For each dice count `i`, calculate the possible ways to reach each target sum `j` by considering all face values from 1 up to `faces`. +4. The answer will be stored in `dp[n][target]`, representing the number of ways to reach the target sum with `n` dice. \ No newline at end of file diff --git a/Dynamic Programming/Dice Roll Problem/program.c b/Dynamic Programming/Dice Roll Problem/program.c new file mode 100644 index 00000000..359c33cf --- /dev/null +++ b/Dynamic Programming/Dice Roll Problem/program.c @@ -0,0 +1,41 @@ +#include +#include + +#define MAX_DICE 100 +#define MAX_SUM 1000 +#define FACES 6 // Number of faces on each die, can be adjusted as needed + +// DP table to store the number of ways to get each sum with a given number of dice +int dp[MAX_DICE + 1][MAX_SUM + 1]; + +int diceRollSum(int n, int target) { + // Initialize the DP table with zeros + memset(dp, 0, sizeof(dp)); + dp[0][0] = 1; // Base case: 1 way to get sum 0 with 0 dice + + for (int i = 1; i <= n; i++) { // For each dice count + for (int j = 1; j <= target; j++) { // For each possible sum + dp[i][j] = 0; + for (int face = 1; face <= FACES; face++) { // For each face value + if (j - face >= 0) { // Ensure sum is non-negative + dp[i][j] += dp[i - 1][j - face]; + } + } + } + } + + return dp[n][target]; +} + +int main() { + int n, target; + printf("Enter number of dice: "); + scanf("%d", &n); + printf("Enter target sum: "); + scanf("%d", &target); + + int result = diceRollSum(n, target); + printf("Number of ways to reach the target sum %d with %d dice: %d\n", target, n, result); + + return 0; +}