
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Count number of ways to reach a given score in a game
Let us consider a game, in which a player can get some score with 3, 5 or 10 in each move. A target score is also given. Our task is to find how many possible ways are there to reach that target score with those three points.
By the dynamic programming approach, we will create a list of all score from 0 to n, and for each value of 3, 5, 10, we simply update the table.
Input and Output
Input: The maximum score to reach using 3, 5 and 10. Let the input is 50. Output: Number of ways to reach using (3, 5, 10)50: 14
Algorithm
countWays(n)
There is only 3 possible score, they are 3, 5 and 10
Input: n is the maximum score to reach.
Output − The number of possible ways to reach score n.
Begin create table of size n+1 set all table entries to 0 table[0] := 1 for i := 3 to n, do table[i] := table[i] + table[i-3] done for i := 5 to n, do table[i] := table[i] + table[i-5] done for i := 10 to n, do table[i] := table[i] + table[i-10] done return table[n] End
Example
#include <iostream> using namespace std; // Returns number of ways to reach score n int countWay(int n) { int table[n+1], i; //table to store count for each value of i for(int i = 0; i<=n; i++) { table[i] = 0; // Initialize all table values as 0 } table[0] = 1; //set for 1 for input as 0 for (i=3; i<=n; i++) //try to solve using 3 table[i] += table[i-3]; for (i=5; i<=n; i++) //try to solve using 5 table[i] += table[i-5]; for (i=10; i<=n; i++) //try to solve using 10 table[i] += table[i-10]; return table[n]; } int main() { int n; cout << "Enter max score: "; cin >> n; cout << "Number of ways to reach using (3, 5, 10)" << n <<": " << countWay(n); }
Output
Enter max score: 50 Number of ways to reach using (3, 5, 10)50: 14
- Related Articles
- Count number of ways to reach a given score in a Matrix in C++
- Count number of ways to reach destination in a Maze in C++
- Count ways to reach a score using 1 and 2 with no consecutive 2s in C++
- Count number of ways to jump to reach end in C++
- Count ways to reach the n’th stair
- Count number of ways to divide a number in parts in C++
- Count the number of ways to traverse a Matrix in C++
- Count ways to express a number as sum of powers in C++
- Count ways to spell a number with repeated digits in C++
- Count ways to express a number as sum of consecutive numbers in C++
- Count number of ways to partition a set into k subsets in C++
- Program to find maximum score of brick removal game in Python
- C++ Program to find maximum score of bit removal game
- Program to find maximum score in stone game in Python
- Count number of subsets of a set with GCD equal to a given number in C++

Advertisements