algorithm - printing the turns of a recursive maze solution in Java -


so i've been working on following problem:

i buried sapphire started walking. walked in straight line following compass direction (n, s, e, w). when stopped, made 90 degree turn , continued walking. might have crossed path, don’t remember. below number of meters travelled in each direction. i’m lost , must abandon record while search way out. i’m placing note under rock @ final location. perhaps lucky adventurer decode note , retrace steps earn treasure. unfortunately, there no record of in ruins note found. instead, must write program find treasure. input first line contains 2 integers x y, representing number of rows , columns in ruins. maximum of 20 rows , 50 columns. next x lines show grid map of space. period “.” empty square. hash “#” large boulder, marking square cannot entered. next line has integer n, count of straight paths walked. maximum of 20 paths. last line contains n integers separated spaces, showing successive path-lengths.. 5 10

####

........#

.#...##.#

...#....#

#### 8 2 4 2 2 2 5 2 1 output program must print same map, location of both sapphire (s) , final

location of message (f) marked. also, label every turning point successive lowercase letters (if same point used more once, print letter later turn.) there 1 route follows path-lengths in list.

####

b.e.a..f#

.#...##.#

c.d#s.fg#

#

and have made recursive method checks every direction starting every open position of maze until finds solution, output of problem needs mazes turns.

the problem is, when use recursive solution , edit actual char[][] map, never knows path lead actual finish, create output this:

d...d

.....

cbabc

d...d

but instead show 1 path, this:

....d

.....

..abc

.....

here incomplete solution:

import java.util.scanner;  public class sapphiresearch {      private static int rs; // row size     private static int cs; // column size      private static int sr; // save row (saves solution row)     private static int sc; // save col (saves solution col)     private static direction sd; // save direction (saves solution dir)      private static char[][] map; // maze traverse      private static int n; // number of turns      private static int[] go; // length of turns      public static void main(string[] args) {         getinput();         (int r = 0; r < rs; r++)             (int c = 0; c < cs; c++)                 (direction d : direction.values())                     solve(sr = r, sc = c, sd = d, 0, false);     }      public static void solve(int r, int c, direction d, int start,             boolean printing) {         if (issolid(r, c))             return;         if (printing) {             if (start == 0)                 map[r][c] = 's';             else                 map[r][c] = (char) (start - 1 + 'a');             if (start == n) {                 map[r][c] = 'f';                 return;             }         }         if (start == n - 1 && !printing) {             solve(sr, sc, sd, 0, true);             printarray(map);             system.exit(0);         }         int count = 0;         while (start < go.length && count < go[start]) {             count++;             r += d.dr;             c += d.dc;             if (issolid(r, c))                 return;         }         (direction t : d.turn())             solve(r, c, t, start + 1, printing);     }      public static boolean issolid(int r, int c) {         return map[r][c] == '#';     }      public static void printarray(char[][] o) {         (int r = 0; r < o.length; r++) {             (int c = 0; c < o[r].length; c++)                 system.out.print(o[r][c]);             system.out.println();         }     }      private static void getinput() {         scanner s = new scanner(system.in);         rs = s.nextint();         cs = s.nextint();         s.nextline(); // clear buffer         map = new char[rs][cs];         (int r = 0; r < rs; r++) {             int c = 0;             char[] f = s.nextline().trim().tochararray();             (char t : f)                 map[r][c++] = t;         }         n = s.nextint();         go = new int[n];         (int = 0; < n; i++)             go[i] = s.nextint();     } }  enum direction {     // deltar, deltac     up(-1, 0), down(1, 0), left(0, -1), right(0, 1);      public int dr;     public int dc;      private direction(int dr, int dc) {         this.dr = dr;         this.dc = dc;     }      public direction[] turn() {         direction[] out = new direction[2];         switch (this) {         case up:         case down:             out[0] = left;             out[1] = right;             break;         case left:         case right:             out[0] = up;             out[1] = down;         }         return out;     } } 

the question is: building upon recursive solve algorithm, best way print solution path (where doesn't print out every path tries take)?

you need build list of turns recursive search (i'm listing direction here simplicity store object co-ordinates example).

if path (n,e,n,w,s) , save exit.

to keep partial list far , each recursive call copy list far , add it.

i.e.:

n

ne nw fail

nen nes fail

nenw

etc.

at end can either return completed solution or if need handle multiple solutions have final results list of lists insert completed 1 into.

the key step copy list far recursion branches cannot interfere each other.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -