|
| 1 | +/* |
| 2 | +https://www.geeksforgeeks.org/dice-throw-dp-30/ |
| 3 | +
|
| 4 | +Given n dice each with m faces, numbered from 1 to m, find the number of ways to get sum X. X is the summation of values on each face when all the dice are thrown. |
| 5 | +
|
| 6 | +Let the function to find X from n dice is: Sum(m, n, X) |
| 7 | +The function can be represented as: |
| 8 | +Sum(m, n, X) = Finding Sum (X - 1) from (n - 1) dice plus 1 from nth dice |
| 9 | + + Finding Sum (X - 2) from (n - 1) dice plus 2 from nth dice |
| 10 | + + Finding Sum (X - 3) from (n - 1) dice plus 3 from nth dice |
| 11 | + ................................................... |
| 12 | + ................................................... |
| 13 | + ................................................... |
| 14 | + + Finding Sum (X - m) from (n - 1) dice plus m from nth dice |
| 15 | +
|
| 16 | +So we can recursively write Sum(m, n, x) as following |
| 17 | +Sum(m, n, X) = Sum(m, n - 1, X - 1) + |
| 18 | + Sum(m, n - 1, X - 2) + |
| 19 | + .................... + |
| 20 | + Sum(m, n - 1, X - m) |
| 21 | + |
| 22 | +Observe Overlapping Problems |
| 23 | +Sum(6, 3, 8) = Sum(6, 2, 7) + Sum(6, 2, 6) + Sum(6, 2, 5) + |
| 24 | + Sum(6, 2, 4) + Sum(6, 2, 3) + Sum(6, 2, 2) |
| 25 | +
|
| 26 | +To evaluate Sum(6, 3, 8), we need to evaluate Sum(6, 2, 7) which can |
| 27 | +recursively written as following: |
| 28 | +Sum(6, 2, 7) = Sum(6, 1, 6) + Sum(6, 1, 5) + Sum(6, 1, 4) + |
| 29 | + Sum(6, 1, 3) + Sum(6, 1, 2) + Sum(6, 1, 1) |
| 30 | +
|
| 31 | +We also need to evaluate Sum(6, 2, 6) which can recursively written |
| 32 | +as following: |
| 33 | +Sum(6, 2, 6) = Sum(6, 1, 5) + Sum(6, 1, 4) + Sum(6, 1, 3) + |
| 34 | + Sum(6, 1, 2) + Sum(6, 1, 1) |
| 35 | +.............................................. |
| 36 | +.............................................. |
| 37 | +Sum(6, 2, 2) = Sum(6, 1, 1) |
| 38 | +*/ |
| 39 | + |
| 40 | +//DYNAMIC PROGRAMMING - BOTTOM UP APPROACH |
| 41 | + |
| 42 | +public long findWays(int dices, int faces, int sum){ |
| 43 | + //HANDLING EXTREMES |
| 44 | + /* |
| 45 | + Value of sum is too less. |
| 46 | + 1. If it is less than the number of dices we return 0. |
| 47 | + Minimum value for each dice is 1 ==> minimum sum of all faces of all dices == #dices |
| 48 | + 2. If it the sum is equal to the number of dices ==> there is exactly 1 way to get to this sum |
| 49 | + */ |
| 50 | + if(sum <= dices) return (sum == dices); |
| 51 | + |
| 52 | + /* |
| 53 | + Value of sum is too huge. |
| 54 | + max value of all faces of all dices = dices*faces |
| 55 | + */ |
| 56 | + if(sum >= dices*faces) return (sum == dices*faces); |
| 57 | + |
| 58 | + //ways[i][j] indicate the number of ways to attain a sum 'j' when we consider dices [1....i] |
| 59 | + //0th row and 0th column are not used |
| 60 | + long ways[][] = new long[dices+1][sum+1]; |
| 61 | + |
| 62 | + for(int col=0 ; col <= Math.min(sum,faces) ; col++) ways[1][col] = 1; |
| 63 | + |
| 64 | + for(int d=2 ; d <= dices ; d++){ |
| 65 | + for(int s=1 ; s <= sum ; s++){ |
| 66 | + |
| 67 | + //to calculate ways[d][s] we explore all faces |
| 68 | + for(int f=1 ; f <= Math.min(s-1,faces) ; f++){ |
| 69 | + /* |
| 70 | + since the current dice 'd' gives a value 'f', we need to find #ways to form a sum of (s-j) without considering |
| 71 | + the current dice 'd' |
| 72 | + */ |
| 73 | + ways[d][s]+=ways[d-1][s-f]; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return ways[dices][sum]; |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | +Time Complexity - O(dices*sum*faces) |
| 83 | +Space Complexity - O(dices*sum) |
| 84 | +*/ |
0 commit comments