-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Merge pull request #1830 from NK-Works/dice-roll
Dice roll Algo added
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#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; | ||
} |