- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

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

4

- Related Questions & Answers
- 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
- Program to find number of restricted paths from first to last node in Python
- Program to count number of intervals which are intersecting at given point in Python
- Program to find minimum cost to paint fences with k different colors in Python
- Program to find minimum cost to hire k workers in Python
- Program to check we can reach end of list by starting from k 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 find number of ways we can reach from top left point to bottom right point in Python
- Program to count number of ways to win at most k consecutive games in Python
- Set an animation with the same speed from start to end with CSS
- Program to find k-length paths on a binary tree in Python

Advertisements