java - When navigating a 2D array, check neighboring elements for valid path relative to point of entry? -
the nature of problem has changed since submission, question isn't fit deletion. i've answered problem below , marked community post.
i'm writing recursive path-navigating function , final piece need involves knowing cell came from, , determining go next.
the stageyou given 2d array 0's denote invalid path , 1's denote valid path. far know, allowed manipulate info of array you're navigating, mark traveled path 2's.
you need recursively find , print paths origin exit. there 4 mazes, multiple paths, dead ends, or loops.
i've written code can correctly handle 3 cases, except method finding next path flawed in starts @ fixed location relative current index, , checks travelled path; if encounter it, it's supposed retreat.
while works in cases, fails in case when first place checks happens place came from. @ point, returns out , ends prematurely.
because of this, need find way intelligently start scanning (clockwise or anti-clockwise) based on came from, that place last place checked.
here code describing process (note: border cases handled prior this, don't need worry that):
private static void main() { int startx = ;//any arbitrary x int starty = ;//any arbitrary y string path = ""; //recursive calls tack on location , print when exit path found. int[][] myarray = ;//we given array, edit go navigator(startx, starty, path, myarray); } private static void navigator(int locx, int locy, string path, int[][] myarray) { int newx = 0; int newy = 0; path = path.concat("["+locx+","+locy+"]"); //case 1: you're on border of maze boolean bisonedge = (locx == 0 || locx == myarray.length-1 || locy == 0 || locy == myarray[0].length-1); if (bisonedge) { system.out.println(path); return; } int[][] surroundings = surroundingsfinder(locx, locy, myarray); (int = 0; <= 7; i++) { //case 2: path encountered if (surroundings[0][i] == 1) { myarray[locx][locy] = 2; newx = surroundings[1][i]; newy = surroundings[2][i]; navigator(newx, newy, myarray, path); } //case 3: breadcrumb encountered if (surroundings[0][i] == 2) { myarray[locx][locy] = 1; return; } } } //generates 2d array of surroundings clockwise n nw //this part needs improved, starts @ a. // // h b // g - c // f e d // static int[][] surroundingsfinder(int locx, int locy, int[][] myarray) { int[][] surroundings = new int[3][8]; (int = -1; <= 1; i++) { (int j = -1; j <= 1; j++) { } } //can done simpler, done way clarity int xa = locx-1; int ya = locy; int vala = myarray[xa][ya]; int xb = locx-1; int yb = locy+1; int valb = myarray[xb][yb]; int xc = locx; int yc = locy+1; int valc = myarray[xc][yc]; int xd = locx+1; int yd = locy+1; int vald = myarray[xd][yd]; int xe = locx+1; int ye = locy; int vale = myarray[xe][ye]; int xf = locx+1; int yf = locy-1; int valf = myarray[xf][yf]; int xg = locx; int yg = locy-1; int valg = myarray[xg][yg]; int xh = locx-1; int yh = locy-1; int valh = myarray[xh][yh]; int[][] surroundings = new int[3][8]; surroundings[0][0] = vala; surroundings[1][0] = xa; surroundings[2][0] = ya; surroundings[0][1] = valb; surroundings[1][1] = xb; surroundings[2][1] = yb; surroundings[0][2] = valc; surroundings[1][2] = xc; surroundings[2][2] = yc; surroundings[0][3] = vald; surroundings[1][3] = xd; surroundings[2][3] = yd; surroundings[0][4] = vale; surroundings[1][4] = xe; surroundings[2][4] = ye; surroundings[0][5] = valf; surroundings[1][5] = xf; surroundings[2][5] = yf; surroundings[0][6] = valg; surroundings[1][6] = xg; surroundings[2][6] = yg; surroundings[0][7] = valh; surroundings[1][7] = xh; surroundings[2][7] = yh; homecoming surroundings; } can help me this? can see, surroundingsfinder finds a first, b way h. fine if , only if entered h. if fails on cases entered a, need create way intelligently determine start finding. 1 time know this, can adapt logic no longer utilize 2d array of values, well. far can't come logic smart searcher!
note: aware java not optimize middle-recursion. seems impossible tail recursion working problem this.
your issue seems with:
if (surroundings[0][i] == 2) { myarray[locx][locy] = 1; return; } perhaps should changed to:
if (surroundings[0][i] == 2) { // not sure why need if it's 1 myarray[locx][locy] = 1; // go next iteration of "i" loop // , maintain looking next available path continue; } your recursive method automatically homecoming when none of surrounding cells satisfy status if (surroundings[0][i] == 1).
ps: it's conventional name variables using little letter first character. example: surroundings, path, startx or myvar
java arrays recursion multidimensional-array
No comments:
Post a Comment