- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to count number of paths with cost k from start to end point in Python

Suppose we have a 2d binary matrix and another value k. Now starting from the top-left cell, we have to go to the bottom right cell. In one step, we can only go down, or right. Now the score of a path is the sum of the values on the cells on the path. We have to find the number of paths from the start cell to end cell with score k. If there are huge possible ways then return result mod 10^9+7.

So, if the input is like

0 | 0 | 1 |

1 | 0 | 1 |

0 | 1 | 0 |

K = 2, then the output will be 4, as the paths with score 2 are [R,R,D,D], [D,R,R,D], [D,D,R,R], [D,R,D,R] here D is down and R is right.

To solve this, we will follow these steps −

deno := 10^9 + 7

m := row count of matrix, n := column count of matrix

Define a function dfs() . This will take i, j, pts

if i >= m or j >= n, then

return 0

pts := pts + matrix[i, j]

if i is same as m - 1 and j is same as n - 1, then

return 1 when pts is same as k otherwise 0

return dfs(i + 1, j, pts) + dfs(i, j + 1, pts)

From the main method do the following −

return dfs(0, 0, 0) mod deno

## Example

Let us see the following implementation to get better understanding −

class Solution: def solve(self, matrix, k): m, n = len(matrix), len(matrix[0]) def dfs(i=0, j=0, pts=0): if i >= m or j >= n: return 0 pts += matrix[i][j] if i == m - 1 and j == n - 1: return int(pts == k) return dfs(i + 1, j, pts) + dfs(i, j + 1, pts) return dfs() % (10 ** 9 + 7) ob = Solution() matrix = [ [0, 0, 1], [1, 0, 1], [0, 1, 0] ] k = 2 print(ob.solve(matrix, k))

## Input

[ [0, 0, 1], [1, 0, 1], [0, 1, 0] ], 2

## Output

4

- Related Articles
- Program to count number of paths whose sum is k in python
- Program to find number of starting point from where we can start travelling in Python
- Program to count number of unique paths that includes given edges in Python
- Program to count number of sublists with exactly k unique elements in Python
- C++ program to find minimum number of steps needed to move from start to end
- Program to find number of restricted paths from first to last node in Python
- MySQL query to count days in date range with start and end date
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to check we can reach end of list by starting from k in Python
- Program to find minimum cost to paint fences with k different colors in Python
- Program to find k-length paths on a binary tree in Python
- Program to count number of intervals which are intersecting at given point in Python
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python