java堆栈迷宫算法

老鼠走迷宫是递回求解的基本题型,我们在二维阵列中使用1表示迷宫墙壁,0表示可以走的路径,使用3来表
示老鼠的行走路径,试以程式求出由入口至出口的路径。
解法老鼠的走法有上、左、下、右四个方向,在每前进一格之后就选一个方向前进,无法前
进时退回选择下一个可前进方向,如此在阵列中依序测试四个方向,直到走到出口为止,这是
递回的基本题,请直接看程式应就可以理解
package com.fishroad;

import java.util.Stack;

class Step{
    int x,y,d;
    public Step(int x,int y,int d) {
        this.x = x;//横坐标
        this.y = y;//纵坐标
        this.d = d;//方向
    }
}

public class MazeTest {

    public static void main(String[] args) {
        // 迷宫定义
        int[][] maze = {{1,1,1,1,1,1,1,1,1,1},
                        {1,0,0,1,1,0,1,1,1,1},
                        {1,1,0,0,0,1,1,1,1,1},
                        {1,0,1,0,0,0,0,0,1,1},
                        {1,0,1,1,1,0,0,1,1,1},
                        {1,1,0,0,1,1,0,0,0,1},
                        {1,0,1,1,0,0,1,1,0,1},
                        {1,1,1,1,1,1,1,1,1,1}};
        //行走的方向东南西北四个方向
        int[][] move = {{0,1},{1,0},{0,-1},{-1,0}};
        //打印迷宫
        for(int i=0;i<=7;i++){
        	for(int j=0;j<=9;j++){
        		if(maze[i][j] == 1){
        			System.out.print("█");
        		}else{
        			System.out.print("*");
        		}
        	}
        	System.out.print("\n");
        }
        Stack s = new Stack();
        Stack s1 = new Stack();
        int a = path(maze, move, s);
        Step aa = null;
        //反转堆栈里面的元素
        Stack s2 = new Stack();
        //将s堆栈里面的元素出栈,并且将元素压到s2的堆栈里面,这样就能保证是按行走的顺序输出坐标,否则是倒序输出的坐标
        while(!s.isEmpty()){
            Step step = (Step) s.pop();
            s2.push(step);
        }
        //打印出最后的坐标顺序
        StringBuffer aaa = new StringBuffer() ;
        while(!s2.isEmpty()){
        	Step step = (Step) s2.pop();
        	aaa.append("("+step.x+","+step.y+")"+"→");
        }
        String str = aaa.toString();
    	System.out.print(str.substring(0, str.length()-1));
    	System.out.println("\r");
    	System.out.println("走过的路径为:");
    	//打印出走过的所有坐标位置
    	for(int i=0;i<=7;i++){
        	for(int j=0;j<=9;j++){
        		if(maze[i][j] == 1){
        			System.out.print("█");
        		}else if(maze[i][j] == -1){
        			System.out.print("#");
        		}else{
        			System.out.print("*");
        		}
        	}
        	System.out.print("\n");
        }
        
    }
    //计算路径
	public static int path(int[][] maze,int[][] move,Stack s){
        Step temp = new Step(1,1,-1); //起点
        s.push(temp);
        while(!s.isEmpty()){
            temp = (Step) s.pop();
            int x = temp.x;
            int y = temp.y;
            int d = temp.d+1;
            //如果d大于4也就是说当前坐标的无路可走,只能后退一步,找后一坐标的下一个方向,在继续寻找坐标
            while(d<4){
                int i = x + move[d][0];
                int j = y + move[d][1];
                if(maze[i][j] == 0){    //该点可达
                    temp = new Step(i,j,d); //到达新点
                    s.push(temp);
                    x = i;
                    y = j;
                    maze[x][y] = -1;  //到达新点,标志已经到达
                    if(x == 6 && y == 8){
                        return 1;  //到达出口,迷宫有路,返回1
                    }else{
                        d = 0;  //重新初始化方向
                    }
                }else{
                    d++; //改变方向
                }
            }
        }
        return 0;
    }

}
结果如下:


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值