algorithm - Given a 2D character array find all paths from the top left to the the bottom right -
Look for all possible paths in the lower right corner of the top left corner given a 2D character array. I have the following recursive solutions, can anyone explain how? Apart from this, what is a more optimal solution? I am not familiar with dynamic programming, but I think it can be used to solve this problem in any way.
Public array list & lt; ArrayList & lt; Character & gt; & Gt; GetPaths (four [] [] grids) {Return getPaths (grid, 0, 0, new arrelisted & lt; character & gt; ()); } Public Array List & Lieutenant; Outerst & lieutenant; Character & gt; & Gt; GetPaths (four [] [] grids, int x, int y, arrayList & lt; character & gt; path) {ArrayList & lt; ArrayList & lt; Character & gt; & Gt; Allpaths = new arreelists & lt; ArrayList & lt; Character & gt; & Gt; (); Path.add (grid [x] [y]); ArrayList & LT; Character & gt; Path 1 = new arreelist & lt; Character & gt; (Path); ArrayList & LT; Character & gt; Path 2 = New Arrestist & lt; Character & gt; (Path); ArrayList & LT; ArrayList & LT; Character & gt; & Gt; Val1, val2; If (x == grid.length-1 & amp; y == grid [0]. Length -1) {allPaths.add (path); } Else {if (x & lt; grid.length-1) {val1 = getPaths (grid, x + 1, y, path1); (Arrayist & lt; character & gt; v1: val1) all.paths.add (v1); } If (Y + L; Grid [0]. Length -1) {val2 = getpaths (grid, x, y + 1, path 2); (Arrayist & lt; character & gt; v2: val2) all.paths.add (v2); }} Return to all the positions; }
If your allowed moves are only down or right, then another to think The method can be all permutations of this y-1
bottom
and x-1
right
s. For example, a 4x3 grid will be:
ddrrr drdrr drrdr ... rrrdd
In that case, if you wish, instead of Rickshawn, You can use any algorithm that produces pathogens to produce "agorographic sequence" (in this case the string can also be converted to binary). To generate the actual path, you will start with (0,0) and update X and Y whether the character is displayed below or correct.
Comments
Post a Comment