# Count number of ways to reach a given score in a Matrix in C++

C++Server Side ProgrammingProgramming

Given a square matrix[][] containing non negative numbers as its elements. Also given a variable score. The goal is to count the ways to reach the given score by adding elements from matrix[][] such that only moves allowed are right moves and down moves.

Starting from matrix[0][0] only moves can be, move to matrix[0][1]  ( right move ) or move to matrix[1][0] ( down move ) and add value to reach sum=score.

## Let us understand with examples.

For Example

Input -  matrix[row][col] = { {1, 1}, { 1, 1} } score=3

Output - Count of number of ways to reach a given score in a Matrix are: 2

Explanation - The score can be reached in following ways:

Way 1: adding element at index (0,0) + (0,1) + (1,1) = 1+1+1 = 3

Way 2: adding element at index (0,0) + (1,0) + (1,1) = 1+1+1 = 3

Input -  matrix[row][col] = {  {1,1,2},{ 2,1,1}, {1,2,2} } score=7

Output - Count of number of ways to reach a given score in a Matrix are: 2

Explanation - The score can be reached in following ways:

Way 1: adding element at index (0,0) + (0,1) + (1,1) + (1,2) + (2,2) = 1+1+1+2+2 = 7

Way 2: adding element at index (0,0) + (0,1) + (1,1) + (2,1) + (2,2) = 1+1+1+2+2 = 7

## Approach used in the below program is as follows

In this approach we will use dynamic programming to solve the problem. We will use two arrays arr[row][col][size] and check[row][col][size].The array check will mark the cells of matrix[][] if they are visited as true. Array arr[][][] is used to store the number of ways to reach a particular cell from matrix[0][0]. Recursively we will calculate the ways.

• Take the 2D array matrix for storing numbers.
• Take variable score as input.
• Take two arrays int arr[row][col][size] and bool check[row][col][size].
• Function matrix_score(int matrix[row][col], int rows, int cols, int sc) is used to return the count of the number of ways to reach a given score in a Matrix.
• If the score sc is less than 0 then return 0. ( To end the recursion or in case of wrong input)
• If the number of rows or columns are less than 0 then return 0. ( to end recursion ).
• If the first cell is equal to the sc (input score) then return 1 as the only way. If it is not then return 0.
• If the current cell is already visited, then return the number of ways at this cell as arr[rows][cols][sc].
• If all above conditions do not hold then mark the current cell as visited. Using check[rows][cols][sc] = true.
• Calculate temp_1 = matrix_score(matrix, rows-1, cols, sc-matrix[rows][cols])
• Calculate temp_2 = matrix_score(matrix, rows, cols-1, sc-matrix[rows][cols])
• Set number of ways as arr[rows][cols][sc] = temp_1 + temp_2.
• At the end return arr[rows][cols][sc].

## Example

Live Demo

#include <iostream>

using namespace std;
#define row 2
#define col 2
#define size 30
int arr[row][col][size];
bool check[row][col][size];

int matrix_score(int matrix[row][col], int rows, int cols, int ways) {
if (ways < 0) {
return 0;
}
if (rows < 0 || cols < 0) {
return 0;
}
if (rows == 0) {
if (cols == 0) {
if (ways == matrix[0][0]) {
return 1;
} else {
return 0;
}
}
}
if (check[rows][cols][ways]) {
return arr[rows][cols][ways];
}
check[rows][cols][ways] = true;
int temp_1 = matrix_score(matrix, rows - 1, cols, ways - matrix[rows][cols]);
int temp_2 = matrix_score(matrix, rows, cols - 1, ways - matrix[rows][cols]);
arr[rows][cols][ways] = temp_1 + temp_2;
return arr[rows][cols][ways];
}
int main() {
int matrix[row][col] = {
{
1,
1
},
{
1,
1
}
};
int ways = 3;
cout << "Count of number of ways to reach a given score in a Matrix are: " << matrix_score(matrix, row - 1, col - 1, ways);
return 0;
}

If we run the above code it will generate the following output −

## Output

Count of number of ways to reach a given score in a Matrix are: 2
Published on 29-Jan-2021 12:16:07