简体   繁体   中英

Count paths in a grid with multiple checkpoints

Given a grid of size NXN . Its bottom-left point is (0,0) and top-right element is (N-1,N-1) .

We can traverse the grid in either top or right direction. We have to find the number of ways to traverse from the bottom-left to top-right point. There are some checkpoints which we have to visit in every path. There is at least one valid path.

Example : Let N=5 and we have 1 checkpoint at (2,2) then here answer is 36 .

Note : I need to count only valid paths and have no concern in finding them.

What can be efficient way to count them ?

you have to know two things :

  1. Rule of product : it means that number of ways from start to end is equal number of ways from start to middle point * number of ways from middle point to end.

  2. C(R,R+U) ( R is number of your right moves and U is the number of up moves it means if you want to go from (a,b) to (c,d) then R = ca and U = db , and C(R,R+U) = (R+U)!/R!U! ) is the answer of how many ways there are from bottom left to top right in a grid.

Example in your example from my second rule we have :

number of moves from (0,0) to (2,2) because after two right move from 0 you reach 2 and after two up move from 0 you reach 2 so R=2 and U=2 so C(2,2+2) = C(2,4) = 4!/2!2! = 6 C(2,2+2) = C(2,4) = 4!/2!2! = 6 . And doing the same for R and U number of moves from (2,2) to (4,4) is C(2,2+2) = C(2,4) = 4!/2!2! = 6 C(2,2+2) = C(2,4) = 4!/2!2! = 6

and from the first rule we have number of all possible moves : 6 * 6 = 36

Use dynamic programming:

dp[k, i, j] = number of paths from checkpoint k to (i, j)

dp[k, i, j] = dp[k, i - 1, j] +        top
              dp[k, i, j - 1]          right

Answer is the product of dp values between the checkpoints.

Note: you can avoid the first dimension by realizing that the actual positions in the matrix don't matter, just the relative distances between the positions and the positions of the checkpoints.

The crux of this particular problem is to find the number of paths between sub points (after sorting based on their respective positions) and then multiplying all the number of paths.

The solution here would be:

1. Sort the points from Source to Destination.

Example: If start point is [0,0] end point is [10,10] and intermediate points being [5,6],[2,4] & [8,8], then the sorted points (including source and destination) would be [0,0], [2,4], [5,6], [8,8], [10,10] in a 11X11 matrix

2. Find the number of Paths (preferably using DP) from source to next sub-destination. (In order of sorted points)

In the above example, the number of paths that need to be calculated would be between the following points:

[0,0] to [2,4]

[2,4] to [5,6]

[5,6] to [8,8]

[8,8] to [10,10]

3. Find the product of all the above 4 sub paths

That's it

Here's a solution for finding number of paths (see DP approach) from a source and destination. You need not pass the matrix as argument as that is just not needed. Only source and destination points are needed in the form of [x,y] [X,Y]. The other part (sorting the points and multiplying) has to be done as explained above apart from the code present in the link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM