2017-04-16 66 views
1

給定一個填充0和1的2維字符數組,其中0代表牆,1代表有效路徑,我開發了一個名爲findPath(int r,int c)的遞歸方法來查找出口在標有'x'的迷宮中。該方法接受迷宮的當前行和列,並通過N,E,S,W方向,直到找到有效路徑並用「+」標記該有效路徑。給定所有方向被發現被牆堵塞的情況,則該方法假設回退,直到不再是這種情況,然後用'F'標記該路徑以表示壞路徑。遞歸迷宮求解器方法問題

現在我無法弄清楚爲什麼findPath方法似乎並不貫穿所有的方向,因爲我的顯示方法只是顯示程序從我傳入的座標開始,而不是從那裏移動到任何地方,爲什麼可以這是?

這裏是我的Driver類

public class MazeMain2 
{ 
    public static void main(String[]args) 
    { 

    char[][] mazeArr = {{'0','0','0','1','0','0','0','0','0','0','0','0','0','0','0'}, 
       {'0','0','0','1','0','0','0','0','1','0','0','0','0','1','0'}, 
       {'0','0','0','1','1','1','1','1','1','1','1','1','0','0','0'}, 
       {'0','0','0','1','0','0','0','0','0','0','0','1','0','0','0'}, 
       {'0','0','0','1','1','1','1','1','0','0','0','1','0','0','0'}, 
       {'0','0','0','0','0','0','0','1','0','0','0','1','0','0','0'}, 
       {'0','0','0','0','1','1','1','1','0','0','0','1','0','0','0'}, 
       {'0','0','0','0','1','0','0','1','0','0','0','1','0','1','0'}, 
       {'0','0','0','0','1','0','0','1','0','0','0','0','0','0','0'}, 
       {'0','0','0','0','1','0','0','0','0','0','0','0','0','0','0'}, 
       {'0','0','0','0','1','1','1','1','1','1','1','0','0','0','0'}, 
       {'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'}, 
       {'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'}, 
       {'0','0','0','0','0','1','0','0','0','0','1','1','1','1','0'}, 
       {'0','0','0','0','0','0','0','0','0','0','1','0','0','0','0'}}; 

    MazeSolver2 mazeS = new MazeSolver2(mazeArr); 

    mazeS.markEntry(); 
    mazeS.markExit(); 

    mazeS.solve(0, mazeS.start); 


    } 
} 

這裏是我的迷宮求解器類與findPath方法

public class MazeSolver2 
{ 
    int start; 
    int exit; 

    char[][] maze; 

public MazeSolver2(char[][] currentMaze) 
{ 
    maze = currentMaze; 
} 

//Finds where the first 1 is in the top row of the 
//maze (entrance) 
public void markEntry() 
{ 
    for(int x = 0; x < maze.length; x++) 
    { 
     if(maze[0][x] == '1') 
     { 
      maze[0][x] = 'E'; 
      start = x; 
     } 
    } 
} 

//Finds where the last 1 is in the bottom row of the 
//maze (exit) 
public void markExit() 
{ 
    for(int x = 0; x < maze.length; x++) 
    { 
     if(maze[maze.length - 1][x] == '1') 
     { 
      maze[maze.length - 1][x] = 'x'; 
      exit = x; 
     } 
    } 
} 

public void solve(int x, int y) 
{ 
    if(findPath(x, y)) 
    { 
     System.out.println(maze[x][y]); 
    } 
    else 
     System.out.println("No solution"); 

} 

public boolean findPath(int r, int c) 
{ 
    displayMaze(maze); 

    //Found the exit 
    if(maze[r][c] == 'x') 
    { 
     return true; 
    } 


    if(maze[r][c] == '0' || maze[r][c] == '+' || maze[r][c] == 'F') 
    { 
     return false; 
    } 

    maze[r][c] = '+'; 

    //If row is currently at zero then don't check north 
    //direction because it will be outside of the maze 
    if(r <= 0) 
    { 
     if(findPath(r, c++)) 
     { 
      return true; 
     } 


     if(findPath(r++, c)) 
     { 
      return true; 
     } 

     if(findPath(r, c--)) 
     { 
      return true; 
     } 

    } 

    else 
    { 
     //check N, E, S, W directions 
     if(findPath(r--, c) || findPath(r, c++) || 
      findPath(r++, c) || findPath(r, c--)) 
     { 
      return true; 
     } 
    } 

    //Marking the bad path 
    maze[r][c] = 'F'; 

    return false; 

} 

//Displays maze 
public void displayMaze(char[][] maze) 
{ 

     for(int row = 0; row < maze.length; row++) 
     { 
      for(int col = 0; col < maze.length; col++) 
      { 
       if(col == 14) 
       { 
        System.out.print(maze[row][col]); 
        System.out.println(); 
       } 
       else 
       { 
        System.out.print(maze[row][col]); 
       } 
      } 
     } 

    System.out.println(); 
    } 
} 
+0

你是[post increment operator]的受害者(http://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work -in-java的)。總是增加一條新線。 – abhipil

+0

僅供參考 - 如果您的入口點位於(0,0),您的代碼將拋出異常。查看'if(findPath(r,c - ))' –

回答

1

你的算法本身的幾個流動,這我不覺得權利指出。你可以搜索迷宮遍歷問題,並獲得很多好的教程。

但是,注意方法調用。請注意,如果findPath(int r, int c)被撥打電話findPath(5, 5),則對findPath(r, c++)的呼叫將再次通過值findPath(5, 5),而不是findPath(5, 6)

因爲在這種情況下findPath(r, c++)被調用當前值c,之後c++被執行。

同去的findPath(r, c--) findPath(r++ , c)等,等

一個好主意,瞭解的事實是,在方法findPath()的開始打印值int r, int c。 也可以使用後增加/減少(x ++/- x)和前增加/減少(++ x/- x)。

希望它有幫助。