【BFS】【迭代】【Java】迷宫问题

news/2024/6/15 10:32:27 标签: java', v
v id="article_content" class="article_content clearfix"> v id="content_views" class="htmledit_views">
定义一个二维数组:
intmaze[5][5]={
0,1,0,0,0,
0,1,0,1,0,
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入二维数组:

5 5
0,1,0,0,0,
0,1,0,1,0,
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,

输出:

路径: [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 4], [4, 4]]
共用步数 9

va">class Mnode{
    int x;
    int y;
    int deeps=1;
    List list=new ArrayList();

    public Mnode(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class MiGong {
    public static void main(String[] args) {
        //初始化地图
        Scanner scanner=new Scanner(System.in);
        int a[][]=new int[5][5];
        for (int i = 0; i <a.length ; i++) {
            String temp[]=scanner.next().split(",");
            for (int j = 0; j <a[i].length ; j++) {
                a[i][j]= Integer.parseInt(temp[j]);
            }
        }
        //进行选路
        int x=sumit(a);
        if (x==0){
            System.out.println("无法走出");
        }
        else System.out.println("共用步数 "+x);

    }
    public static int sumit(int[][] a){
        int deeps=0; //当前节点深度
        //记录当前节点是否被访问过
        boolean[][] is=new boolean[a.length][a[0].length];
        for (int i = 0; i <a.length ; i++) {
            for (int j = 0; j <a[i].length ; j++) {
                is[i][j]=true;
            }
        }
        int way[][]={{0,1},{1,0},{0,-1},{-1,0}};  //下个节点四个方向
        //初始节点
        Mnode mnode=new Mnode(0,0);
        List il=new ArrayList();
        il.add(0);
        il.add(0);
        mnode.list.add(il);
        is[0][0]=false;
        //初始节点加入队列
        Queue<Mnode> queue=new LinkedList();
        queue.add(mnode);
        //
        while (!queue.isEmpty()){
            Mnode mnode1=queue.poll();
            deeps=mnode1.deeps;
            //到达终点返回
            if (mnode1.x==a[0].length-1&&mnode1.y==a.length-1){
                System.out.println("路径: "+mnode1.list);
                return deeps;
            }
            //遍历该点周围节点
            for (int i = 0; i <4 ; i++) {
                int x=mnode1.x+way[i][0];
                int y=mnode1.y+way[i][1];
                Mnode temp=new Mnode(x,y);
                temp.deeps=mnode1.deeps+1;
                //得到之前路径
                for (int j = 0; j <mnode1.list.size() ; j++) {
                    temp.list.add(mnode1.list.get(j));
                }
                //添加该点进入所走路径
                List list=new ArrayList();
                list.add(temp.x);
                list.add(temp.y);
                temp.list.add(list);
                //判断是否选取该点
                if (isvalued(x,y)&&isway(a[x][y])&&is[x][y]){
                    is[x][y]=false;
                    queue.add(temp);
                }
            }
        }
        return 0;
    }
    //检查是否超过边界
    public static boolean isvalued(int x,int y){
        if (x>=5||y>=5||x<0||y<0) return false;
        else return true;
    }
    //检查是否是路
    public static boolean isway(int a){
        if (a==1){
            return false;
        }
        else return true;
    }
}

 

v>
v> v id="treeSkill">v>

http://www.niftyadmin.cn/n/752607.html

相关文章

Vert.x实战七:TCP设置超时断开

Vert.x系列&#xff1a; Vert.x介绍&#xff1a;https://blog.csdn.net/haoranhaoshi/article/details/89279096 Vert.x实战一&#xff1a;Vert.x通过Http发布数据&#xff1a;https://blog.csdn.net/haoranhaoshi/article/details/89284847 Vert.x实战二&#xff1a;TCP通信&a…

【DFS】【递归】【Java】Leetcode 733. 图像渲染

有一幅以二维整数数组表示的图画&#xff0c;每一个整数表示该图画的像素值大小&#xff0c;数值在 0 到 65535 之间。 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值&#xff08;行 &#xff0c;列&#xff09;和一个新的颜色值 newColor&#xff0c;让你重新上色这幅图像…

wpf 禁用window的systemmenu

private IntPtr WidProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){if (msg 0x112){if (wParam.ToInt32() 0xF093)//单击打开菜单handled true;if (wParam.ToInt32() 0xF100) //键盘打开菜单 handled true;}if (msg 0xa4){if (wParam.ToIn…

Vert.x的TCP服务端和客户端配置

Vert.x系列&#xff1a; Vert.x介绍&#xff1a;https://blog.csdn.net/haoranhaoshi/article/details/89279096 Vert.x实战一&#xff1a;Vert.x通过Http发布数据&#xff1a;https://blog.csdn.net/haoranhaoshi/article/details/89284847 Vert.x实战二&#xff1a;TCP通信&a…

【python】【爬虫】爬取Fate Grand Order wiki所有英灵礼装图鉴

import requests from lxml import etreefor i in range(1,895): url0"https://fgowiki.com/guide/equipdetail/894?ppc" #网页地址resrequests.get(url0)contentres.contenthtmletree.HTML(content)namehtml.xpath(//*[id"row-move"]/div[2]/div/div[2…

TCP解读

TCP 三次握手 第一次: 客户端 - - > 服务端 发送目标&#xff1a;我要连你 第二次: 客户端 < - - 服务端 发送目标&#xff1a;可以 第三次: 客户端 - - > 服务端 发送目标&#xff1a;收到 TCP 详解&#xff1a;https://blog.csdn.net/sinat_36629696/article/detail…

Mysql 内置函数大全

转载并整理&#xff1a; 包括&#xff1a; 数值进制&#xff1b; 字符串长度、截取、填充、删除、拼接等&#xff1b; 文件读取&#xff1b; 数值绝对值、正负判断、取模、取整、次方、对数、开方、三角函数、反三角函数、随机数、弧度和角度、精确度保留、最大值和最小值…

【Hadoop】自定义Hadoop序列化been Demo

package hadoop.mapreduce.serializable;import org.apache.hadoop.io.Writable;import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; /* * 自定义Hadoop序列化 * */ public class MySerializable implements Writable {private String name;pr…