Count number of paths whose weight is exactly X and has at | PHP program to count the number of paths ; Function to find the number of paths ; If the summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; else Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths
< ? php $ max = 4 ; $ c = 2 ; function countPaths ( $ sum , $ get , $ m , $ n , & $ dp ) { global $ max , $ c ; if ( $ sum < 0 ) return 0 ; if ( $ sum == 0 ) return $ get ; if ( $ dp [ $ sum ] [ $ get ] != -1 ) return $ dp [ $ sum ] [ $ get ] ; $ res = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i == $ m ) $ res += countPaths ( $ sum - $ i , 1 , $ m , $ n , $ dp ) ; $ res += countPaths ( $ sum - $ i , $ get , $ m , $ n , $ dp ) ; } $ dp [ $ sum ] [ $ get ] = $ res ; return $ dp [ $ sum ] [ $ get ] ; } $ n = 3 ; $ m = 2 ; $ x = 3 ; $ dp = array_fill ( 0 , $ max + 1 , NULL ) ; for ( $ i = 0 ; $ i <= $ max ; $ i ++ ) for ( $ j = 0 ; $ j < 2 ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo countPaths ( $ x , 0 , $ m , $ n , $ dp ) ; ? >