Skip to content
Advertisement

Strictly Increasing Path in Grid with Python

At each step we can go the one of the left,right,up or down cells only if the that cell is strictly greater thab our current cell. (We cannot move diagonally). We want to find all the paths that we can go from the top-left cell to the bottom-right cell.

[[1,4,3], [5,6,7]]

In our example, these paths are 1->4->6->7 and 1->5->6->7.

How can i solve it in reversible use?

Advertisement

Answer

First, note that the number of such paths is exponential (in the size of the graph).

For example, look at the graph:

JavaScript

In here, you have exactly n rights and n down – which gives you exponential number of such choices – what makes “efficient” solution irrelevant (as you stated, you are looking for all paths).

One approach to find all solutions, is using a variation of DFS, where you can go to a neighbor cell if and only if it’s strictly higher.

It should look something like:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement