java实现矩阵中避障最短路径遍历算法

news/2024/7/3 4:33:53 标签: android, 矩阵, 路径, 迷宫, 算法

原理是遍历所有节点,除了已经到最后目标不遍历外,其它都遍历。

package com.boeyu.matrixlib;

import java.util.ArrayList;
import java.util.List;

public class PathAirthmetic {
    private int width; //矩阵宽
    private int height; //矩阵高
    private Pos mStartPos; //起点坐标
    private Pos mEndPos; //终点坐标
    private int[][] mMatrix;
    private List<List<Pos>> mPathList = new ArrayList<>(); //最终成功路径


    /**
     * 开始计算
     *
     * @param matrix 二维矩阵数组,0代表可通行,1不可通行
     * @param startX 开始坐标x
     * @param startY 开始坐标y
     * @param endX   结束坐标x
     * @param endY   结束坐标y
     * @return
     */
    public List<Pos> start(int[][] matrix, int startX, int startY, int endX, int endY) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return null;
        }
        width = matrix[0].length;
        height = matrix.length;
        System.out.println("width=" + width + ",height=" + height);
        mMatrix = matrix;
        mStartPos = new Pos(startX, startY);
        mEndPos = new Pos(endX, endY);
        mPathList.clear();

        long startTime = System.currentTimeMillis();
        List<Pos> posList = new ArrayList<>();
        posList.add(mStartPos);
        boolean success = enumNextPos(posList, startX, startY);
        System.out.println("time=" + (System.currentTimeMillis() - startTime));
        if (mPathList.size() > 0) {
            System.out.println("path=" + mPathList.size());
            List<Pos> minPath = mPathList.get(0);
            for (List<Pos> poses : mPathList) {
                //System.out.println(poses.toString());
                if (poses.size() < minPath.size()) {
                    minPath = poses;
                }
            }
            return minPath;
        } else {
            return null;
        }
    }

    /**
     * 开始计算
     *
     * @param matrix 一维矩阵数组
     * @param width 矩阵列数
     * @param startX 开始坐标x
     * @param startY 开始坐标y
     * @param endX   结束坐标x
     * @param endY   结束坐标y
     * @return
     */
    public List<Pos> start(int[] matrix, int width, int startX, int startY, int endX, int endY) {
        if (matrix == null || matrix.length == 0) {
            return null;
        }
        int[][] mMatrix = new int[matrix.length / width][width];
        for (int i = 0; i < matrix.length; i++) {
            int x = i % width;
            int y = i / width;
            mMatrix[y][x] = matrix[i];
        }
        return start(mMatrix, startX, startY, endX, endY);
    }

    /**
     * 遍历下一个节点
     *
     * @param path
     * @param x
     * @param y
     * @return
     */
    private boolean enumNextPos(List<Pos> path, int x, int y) {
        mMatrix[y][x] = 1;
        List<Pos> subPos = new ArrayList<>();
        if (x - 1 >= 0 && mMatrix[y][x - 1] == 0) {
            subPos.add(new Pos(x - 1, y));
        }
        if (x + 1 < width && mMatrix[y][x + 1] == 0) {
            subPos.add(new Pos(x + 1, y));
        }
        if (y - 1 >= 0 && mMatrix[y - 1][x] == 0) {
            subPos.add(new Pos(x, y - 1));
        }
        if (y + 1 < height && mMatrix[y + 1][x] == 0) {
            subPos.add(new Pos(x, y + 1));
        }
        boolean success = false;
        for (Pos pos : subPos) {
            if (isEquals(pos.x, pos.y, mEndPos.x, mEndPos.y)) {
                List<Pos> mList = new ArrayList<>(path);
                mList.add(pos);
                mMatrix[pos.y][pos.x] = 0;
                mPathList.add(mList);
                return true;
            }
            List<Pos> mList = new ArrayList<>(path);
            mList.add(pos);
            success |= enumNextPos(mList, pos.x, pos.y);
            mMatrix[pos.y][pos.x] = 0;
        }
        return success;
    }

    private boolean isEquals(int x1, int y1, int x2, int y2) {
        return x1 == x2 && y1 == y2;
    }

    public static class Pos {
        int x;
        int y;

        public Pos(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public String toString() {
            return "(" + x + "," + y + ")";
        }
    }
}
package com.boeyu.matrixlib;

import java.util.List;

public class MainClass {
    public static void main(String[] args) {


        //从左上角行走至右下角
        //************ 二维矩阵演示 ****************
        List<PathAirthmetic.Pos> posList = new PathAirthmetic().start(new int[][]{
                {0, 1, 0, 1, 0, 1, 0, 0},
                {0, 1, 0, 0, 1, 0, 0, 0},
                {0, 1, 0, 0, 0, 0, 1, 1},
                {0, 0, 0, 1, 0, 0, 1, 0},
                {0, 0, 1, 1, 0, 1, 0, 0},
                {0, 0, 0, 1, 0, 0, 0, 0}
        }, 0, 0, 7, 3);
        if (posList != null) {
            System.out.println("min path:" + posList.toString());
        } else {
            System.out.println("fail");
        }

        //************ 一维矩阵演示 ****************
        List<PathAirthmetic.Pos> posList2 = new PathAirthmetic().start(new int[]{
                0, 1, 0, 1, 0, 1, 0, 0,
                0, 1, 0, 0, 1, 0, 0, 0,
                0, 1, 0, 0, 0, 0, 1, 0,
                0, 0, 0, 1, 0, 0, 1, 0,
                0, 0, 1, 1, 0, 0, 1, 0,
                0, 0, 0, 1, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 0, 0, 0,
                0, 0, 1, 1, 0, 1, 1, 0
        }, 8, 0, 0, 7, 7);
        if (posList2 != null) {
            System.out.println("min path:" + posList2.toString());
        } else {
            System.out.println("fail");
        }


    }
}

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

相关文章

微软MCPD for .NET 3.5 考试全攻略

周日收到了微软发来的祝贺信&#xff0c;终于确认我已经拿到了MCPD .NET 3.5 For Enterprise 的证书了。现将MCPD证书的考试经验与大家分享。 微软 .NET 3.5 开发类考试介绍 微软的.NET 3.5 开发类考试和.NET 2.0 的情况类似&#xff0c;分为两级。 低级的叫做TS(Technology Sp…

今天,我们请来一波超龄儿童画出了他们眼中的AI……

上周&#xff0c;海外科技网站The Next Web发起了一次有趣的活动&#xff0c;上街请The Next Web科技会议的与会者们画出他们眼中的AI&#xff0c;结果得到了各种大开的脑洞。 还想要过儿童节的超龄儿童们&#xff0c;怎么能放过这种发挥童真想象力的事情&#xff01; 今天&…

AnySeeker更新至1.1.1_Beta

支持Android 11以上系统&#xff0c;可搜索Android/data目录。https://download.csdn.net/download/zzmzzff/87426294?spm1001.2014.3001.5503

AndEngine 精灵的镜像翻转

在AndEngine中对精灵提供了直接镜像的方法&#xff0c;非常简单便可以实现精灵的水平、垂直以及水平垂直同时的镜像翻转&#xff0c;简单做个记录。 /*** Title: setFlippedHorizontal* Description: 水平翻转* param pFlippedHorizontal*/ public void setFlippedHorizontal(f…

B-tree哈夫曼算法

接下来学习B-tree和哈夫曼算法&#xff0c;只是浅显的了解一下。 B-Tree B-tree就是balanced tree&#xff0c;即多路平衡查找树&#xff0c;其相比于上次学习的二叉查找树有很多优点&#xff0c;初步的映象就是能存储更多的数据&#xff0c;在做磁盘IO时一次读取一个page的数据…

区块链目录篇

为什么80%的码农都做不了架构师&#xff1f;>>> 第【1】篇&#xfeff; 到底什么才是区块链&#xff1f;第【2】篇 区块链到底是怎么运行的&#xff1f;第【3】篇 区块链共识机制第【4】篇 如何理解数字货币&#xff1f;它与区块链又是什么样的关系&#xff1f; 第…

记一次Burpsuite安装SQLmap的sqlipy的小坑

前言 打算使用Burpsuite中的ssqlmap插件&#xff0c;结果死活安装不上&#xff0c;遂有了此篇文章&#xff0c;插件安装参考&#xff1a;https://www.jianshu.com/p/4c3d64a0d5da 占坑 下载Jython后再Burp中加载进去后如下 坑位就在这里&#xff0c;使用了中文路径 就会导致下载…

ECharts测量图,功率图

/*** 测量图&#xff0c;功率图1&#xff0c;仪表盘*/   mainpage.prototype.initEcharsGLT1 function(oneJZ){ //if(myChartGLT1 null && myChartGLT1 ! "" && myChartGLT1 ! undefined) {myChartGLT1.dispose(); //每次加载之前清除之前的echar…