`
橡树心
  • 浏览: 46508 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

老鼠走迷宫(Mouse)

阅读更多
问题说明:

        老鼠走迷宫是循环求解的基本类型,我们在二维数组中用2来表示迷宫的墙壁,使用1来表示老鼠的行走路径,并用程序求出从入口到出口的距离。
public class Mouse {
    private int startI, startJ;  // 入口 
    private int endI, endJ;  // 出口
    private boolean success = false;
    
    public static void main(String[] args) {
        int[][] maze = {{2, 2, 2, 2, 2, 2, 2}, 
                        {2, 0, 0, 0, 0, 0, 2}, 
                        {2, 0, 2, 0, 2, 0, 2}, 
                        {2, 0, 0, 2, 0, 2, 2}, 
                        {2, 2, 0, 2, 0, 2, 2}, 
                        {2, 0, 0, 0, 0, 0, 2}, 
                        {2, 2, 2, 2, 2, 2, 2}};
        
        System.out.println("显示迷宫:"); 
        for(int i = 0; i < maze.length; i++) { 
            for(int j = 0; j < maze[0].length; j++) 
                if(maze[j] == 2) 
                    System.out.print("█"); 
                else 
                    System.out.print("  "); 
            System.out.println(); 
        }

        Mouse mouse = new Mouse();
        mouse.setStart(1, 1);
        mouse.setEnd(5, 5);
        
        if(!mouse.go(maze)) {
            System.out.println("\n没有找到出口!");
        }
        else {
            System.out.println("\n找到出口!");
            for(int i = 0; i < maze.length; i++) { 
                for(int j = 0; j < maze[0].length; j++) { 
                    if(maze[j] == 2) 
                        System.out.print("█"); 
                    else if(maze[j] == 1) 
                        System.out.print("◇"); 
                    else 
                        System.out.print("  "); 
                } 
                System.out.println(); 
            }            
        }
    }
    
    public void setStart(int i, int j) {
        this.startI = i;
        this.startJ = j;
    }
    
    public void setEnd(int i, int j) {
        this.endI = i;
        this.endJ = j;
    }
    
    public boolean go(int[][] maze) {
        return visit(maze, startI, startJ);
    }
    
    private boolean visit(int[][] maze, int i, int j) {
        maze[j] = 1; 

        if(i == endI && j == endJ) 
            success = true; 

        if(!success && maze[j+1] == 0) 
            visit(maze, i, j+1); 
        if(!success && maze[i+1][j] == 0)
            visit(maze, i+1, j); 
        if(!success && maze[j-1] == 0)
            visit(maze, i, j-1); 
        if(!success && maze[i-1][j] == 0)
            visit(maze, i-1, j); 

        if(!success) 
            maze[j] = 0; 
        
        return success; 
    }
}



由于迷宫的设计,老鼠从迷宫的入口到出口的路径可能不只一条,下面我们显示所有路径。

public class Mouse {
    private int startI, startJ;  // 入口 
    private int endI, endJ;  // 出口
    
    public static void main(String[] args) {
        int maze[][] = {{2, 2, 2, 2, 2, 2, 2, 2, 2},
                        {2, 0, 0, 0, 0, 0, 0, 0, 2},
                        {2, 0, 2, 2, 0, 2, 2, 0, 2},
                        {2, 0, 2, 0, 0, 2, 0, 0, 2},
                        {2, 0, 2, 0, 2, 0, 2, 0, 2},
                        {2, 0, 0, 0, 0, 0, 2, 0, 2},
                        {2, 2, 0, 2, 2, 0, 2, 2, 2},
                        {2, 0, 0, 0, 0, 0, 0, 0, 2},
                        {2, 2, 2, 2, 2, 2, 2, 2, 2}};
        
        System.out.println("显示迷宫:"); 
        for(int i = 0; i < maze.length; i++) { 
            for(int j = 0; j < maze[0].length; j++) 
                if(maze[i][j] == 2) 
                    System.out.print("█"); 
                else 
                    System.out.print("  "); 
            System.out.println(); 
        }

        Mouse mouse = new Mouse();
        mouse.setStart(1, 1);
        mouse.setEnd(7, 7);
        
        mouse.go(maze);
    }
    
    public void setStart(int i, int j) {
        this.startI = i;
        this.startJ = j;
    }
    
    public void setEnd(int i, int j) {
        this.endI = i;
        this.endJ = j;
    }
    
    public void go(int[][] maze) {
        visit(maze, startI, startJ);
    }
    
    private void visit(int[][] maze, int i, int j) {
        maze[i][j] = 1; 

        if(i == endI && j == endJ) {
            System.out.println("\n找到出口!");
            for(int m = 0; m < maze.length; m++) { 
                for(int n = 0; n < maze[0].length; n++) { 
                    if(maze[m][n] == 2) 
                        System.out.print("█"); 
                    else if(maze[m][n] == 1) 
                        System.out.print("◇"); 
                    else 
                        System.out.print("  "); 
                } 
                System.out.println();
            }
        }

        if(maze[i][j+1] == 0) 
            visit(maze, i, j+1); 
        if(maze[i+1][j] == 0)
            visit(maze, i+1, j); 
        if(maze[i][j-1] == 0)
            visit(maze, i, j-1); 
        if(maze[i-1][j] == 0)
            visit(maze, i-1, j); 
       
        maze[i][j] = 0;
    }
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics