2017-04-16 122 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)。

希望它有帮助。