递归_迷宫

package agrisom;

import java.util.EmptyStackException;
import java.util.Stack;

/**
 * 递归_迷宫
 */

/**
 * 位置类
 */
class Position{
    public int row;
    public int col;
    public Position(int row,int col){
        this.row=row;
        this.col=col;
    }
}
public class Maze {
    public Stack<Position> stack=new Stack<Position>(); //保存迷宫路径
    public int array[][] ={
                            {1,1,1,1,1,1,1,1},
                            {1,0,0,0,1,0,0,1},
                            {1,0,1,1,1,0,0,1},
                            {0,0,1,0,1,0,0,0},
                            {1,1,1,0,1,0,0,1},
                            {1,1,0,1,1,1,1,1},
                            {0,0,0,1,0,0,0,0},
                            {0,0,1,1,1,1,1,1},
                                             };
    //定义的方格数组需在迷宫内
    private boolean value(int hang,int lie){
        if(hang >= 0 && hang < array.length &&
                lie >=0 && lie< array.length &&
                        array[hang][lie] == 1){
            return true;
        }else{
            return false;
        }
    }
    private boolean discovery(int row, int col){
        boolean pass =false;
        if(value(row,col)){
            //将走过的位置标记为2
            array[row][col] = 2;
            Position pos=new Position(row,col);
            this.stack.push(pos);
            //判断是否到达终点.
            if(row == array.length -1 && col == array.length - 1){
                pass = true;
            }else{//往右走
                 pass = discovery(row,col + 1);
                  //往下走
                 if(pass==false)
                 pass = discovery(row + 1, col);
                  //往左走
                 if(pass==false)
                 pass = discovery(row , col-1);
                  //往上走
                 if(pass==false)
                 pass = discovery(row - 1, col);
            }
            if(pass==true) {
                array[row][col] = 3;
            }else{
                this.stack.pop();
            }
        }
        return pass;
    }
    public static void main(String[] args) {
        Maze mi = new Maze();
        int[][] c = mi.array;
        //输出走之前的迷宫
        for(int i = 0;i<c.length;i++){
            for(int j = 0; j<c.length;j++){
//                System.out.print(mi.array[i][j]+" ");
                if(mi.array[i][j] == 1)
                    System.out.print("□"+" ");
                if(mi.array[i][j] == 0)
                    System.out.print("■"+" ");
            }
            System.out.println();
        }
        System.out.println(mi.discovery(0,0));
        //输出走过之后的迷宫
        for(int i = 0 ;i<= c.length-1;i++){
            for(int j = 0; j<= c.length-1;j++){
                
//                System.out.print(c[i][j]+" ");
                if(c[i][j] == 1)
                    System.out.print("□"+" ");
                if(c[i][j] == 0)
                    System.out.print("■"+" ");
                if(c[i][j] == 3)
                    System.out.print("○"+" ");
                if(c[i][j] == 2)
                    System.out.print("●"+" ");
            }
            System.out.println();
        }
        System.out.println();
        //输出路径
        Stack<Position> stack=new Stack<Position>();
        Position pos=mi.stack.pop();
        while(pos!=null){
            stack.push(pos);
            try{
                pos=mi.stack.pop();
            }catch(EmptyStackException e){
                break;
            }
            
        }
        pos=stack.pop();
        int length=stack.size();
        while(pos!=null){
            if(1==length){
                System.out.print("("+pos.row+","+pos.col+")");
            }else{
                System.out.print("("+pos.row+","+pos.col+")→");
                length=stack.size();
            }
            try{
                pos=stack.pop();
            }catch(EmptyStackException e){
                break;
            }
        }
    }
}
/**
运行结果:
□ □ □ □ □ □ □ □
□ ■ ■ ■ □ ■ ■ □
□ ■ □ □ □ ■ ■ □
■ ■ □ ■ □ ■ ■ ■
□ □ □ ■ □ ■ ■ □
□ □ ■ □ □ □ □ □
■ ■ ■ □ ■ ■ ■ ■
■ ■ □ □ □ □ □ □
true
○ ○ ○ ○ ○ ● ● ●
□ ■ ■ ■ ○ ■ ■ ●
□ ■ □ □ ○ ■ ■ ●
■ ■ □ ■ ○ ■ ■ ■
□ □ □ ■ ○ ■ ■ ●
□ □ ■ ○ ○ ● ● ●
■ ■ ■ ○ ■ ■ ■ ■
■ ■ □ ○ ○ ○ ○ ○

(0,0)→(0,1)→(0,2)→(0,3)→(0,4)→(1,4)→(2,4)→(3,4)→(4,4)→(5,4)→(5,3)→(6,3)→(7,3)→(7,4)→(7,5)→(7,6)→(7,7)
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值