数据结构 05 - 递归回溯问题(一):小球找路问题

在这里插入图片描述

一、思路分析

1.先创建一个二维数组模拟迷宫,把相应点置为1
2.规则:0表示该点没有走过;1表示墙;2表示通路可以走;3表示该点已经走过,但走不通
3.策略:下->右->上->左

二、代码实现

public class MG {
    public static void main(String[] args) {
        //0:表示没走过;1:表示墙;2:表示可以走通;3:表示走过但不通;策略:上右下左
        int[][] map = new int[8][7];
        for (int i = 0; i < 7; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        for (int i = 0; i < 8; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        map[3][1] = 1;
        map[3][2] = 1;
        list(map);
        setWay4(map,1,1);
        list(map);
    }
    //实现方法:利用递归回溯来解决小球路线问题
    //1.上右下左
    public static boolean setWay1(int[][] map,int i,int j){
        //先判定是否到了终点
        if(map[6][5] == 2){
            return true;
        }
        if(map[i][j] == 0){
            map[i][j] = 2;
            if(setWay1(map,i-1,j)){
                return true;
            }else if(setWay1(map,i,j+1)){
                return true;
            }else if(setWay1(map,i+1,j)){
                return true;
            }else if(setWay1(map,i,j-1)){
                return true;
            }else {
                map[i][j] = 3;
                return false;
            }
        }else {
            return false;
        }
    }
    //2.右下左上
    public static boolean setWay2(int[][] map,int i,int j){
        //先判定是否到了终点
        if(map[6][5] == 2){
            return true;
        }
        if(map[i][j] == 0){
            map[i][j] = 2;
            if(setWay2(map,i,j+1)){
                return true;
            }else if(setWay2(map,i+1,j)){
                return true;
            }else if(setWay2(map,i,j-1)){
                return true;
            }else if(setWay2(map,i-1,j)){
                return true;
            }else {
                map[i][j] = 3;
                return false;
            }
        }else {
            return false;
        }
    }
    //3.下左上右
    public static boolean setWay3(int[][] map,int i,int j){
        //先判定是否到了终点
        if(map[6][5] == 2){
            return true;
        }
        if(map[i][j] == 0){
            map[i][j] = 2;
            if(setWay3(map,i+1,j)){
                return true;
            }else if(setWay3(map,i,j-1)){
                return true;
            }else if(setWay3(map,i-1,j)){
                return true;
            }else if(setWay3(map,i,j+1)){
                return true;
            }else {
                map[i][j] = 3;
                return false;
            }
        }else {
            return false;
        }
    }
    //4.左上右下
    public static boolean setWay4(int[][] map,int i,int j){
        //先判定是否到了终点
        if(map[6][5] == 2){
            return true;
        }
        if(map[i][j] == 0){
            map[i][j] = 2;
            if(setWay4(map,i,j-1)){
                return true;
            }else if(setWay4(map,i-1,j)){
                return true;
            }else if(setWay4(map,i,j+1)){
                return true;
            }else if(setWay4(map,i+1,j)){
                return true;
            }else {
                map[i][j] = 3;
                return false;
            }
        }else {
            return false;
        }
    }
    //遍历地图
    public static void list(int[][] map){
        System.out.println("目前地图的情况:");
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 7; j++) {
                System.out.print(map[i][j] + "\t");
            }
            System.out.println();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值