Count number of ways to reach a given score in a Matrix | C # implementation of the approach ; To store the states of dp ; To check whether a particular state of dp has been solved ; Function to find the ways using memoization ; Base cases ; If required score becomes negative ; If current state has been reached before ; Set current state to visited ; Driver code
using System ; class GFG { static int n = 3 ; static int MAX = 30 ; static int [ , , ] dp = new int [ n , n , MAX ] ; static bool [ , , ] v = new bool [ n , n , MAX ] ; static int findCount ( int [ , ] mat , int i , int j , int m ) { if ( i == 0 && j == 0 ) { if ( m == mat [ 0 , 0 ] ) return 1 ; else return 0 ; } if ( m < 0 ) return 0 ; if ( i < 0 j < 0 ) return 0 ; if ( v [ i , j , m ] ) return dp [ i , j , m ] ; v [ i , j , m ] = true ; dp [ i , j , m ] = findCount ( mat , i - 1 , j , m - mat [ i , j ] ) + findCount ( mat , i , j - 1 , m - mat [ i , j ] ) ; return dp [ i , j , m ] ; } public static void Main ( ) { int [ , ] mat = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int m = 5 ; Console . WriteLine ( findCount ( mat , n - 1 , n - 1 , m ) ) ; } }