java - Grid Recursion Not Functioning -


i'm trying solve problem on project euler (project 16) using grid recursion , isn't working out - i'm ending seems infinite loop. goal count how many routes can take top left of 20x20 grid bottom right of grid, start origin , placed number in mygrid[20][20] indicates it's @ bottom right corner.

private static int[][] mygrid;  public static int numroutes(int x, int y){     if (x < 0 || x >= 20 || y < 0 || y>= 20){         return 0;     }     if (mygrid[x][y] == 1){         return 1;     }     int count = 0;     count += numroutes(x+1, y);     count += numroutes(x, y+1);     return count; }  public static void main (string args[]) throws exception{     mygrid = new int[21][21];     (int = 0; < 21; i++){         arrays.fill(mygrid[i], 0);     }     mygrid[20][20] = 1;     int num = numroutes(0,0);     system.out.println(num); } 

this comprises of recursion, , think have of appropriate base cases. thoughts on what's going wrong?

your program not going infinite loop. has many options looks that... test it, reduce array size 5x5.... , see ends...

that being said, result 0 because of bug...

if (x < 0 || x >= 20 || y < 0 || y>= 20){ 

should be

if (x < 0 || x > 20 || y < 0 || y > 20){ 

instead...

also, don't think algorithm gives count of paths between 2 points... gives count of shortest paths... since flow goes top bottom , left right... not in other direction...


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -