java - 这是什么最短路径迷宫算法?

标签 java algorithm maze

我试图尽可能多地了解和理解算法,但我在比赛数据包中偶然发现了一种奇怪的算法,该算法可以找到解决迷宫的最少步数(最短路径)。我 100% 理解它,但我不知道它是什么算法,数据包也没有说明它的名称。我真的很想知道这个算法的名称(如果它有名称的话),以便我可以对其进行更多研究。

import java.io.File;
import java.util.Scanner;

class spot {

    char type;
    int distance;
    spot closest;

    public spot() {
        type = '.';
        distance = Integer.MAX_VALUE;
        closest = null;
    }
}

public class myalg {

    public static void main(String args[]) throws Exception {
        int moves = 0;

        Scanner keyb = new Scanner(new File("src/mar2014/maze2.txt"));

        int rows = keyb.nextInt();
        int cols = keyb.nextInt();
        spot mat[][] = new spot[rows][cols];
        keyb.nextLine();
        spot startSpot = null;
        spot endSpot = null;    
        for (int i = 0; i < mat.length; i++) {
            String line = keyb.nextLine();
            for (int j = 0; j < mat[i].length; j++) {
                mat[i][j] = new spot();
                mat[i][j].type = line.charAt(j);
                if (mat[i][j].type == 'S') {
                    startSpot = mat[i][j];
                    startSpot.distance = 0;
                    startSpot.type = ' ';
                }
                if (mat[i][j].type == 'E') {
                    endSpot = mat[i][j];
                    endSpot.type = ' ';
                }
            }
        }

        boolean changeMade = true;
        while (changeMade) {
            changeMade = false;
            for (int i = 0; i < mat.length; i++) {
                for (int j = 0; j < mat[i].length; j++) {
                    spot thisSpot = mat[i][j];
                    spot adjacentSpots[] = {null, null, null, null};
                    if (i > 0) {
                        adjacentSpots[0] = mat[i - 1][j];
                    }
                    if (i < cols - 1) {
                        adjacentSpots[1] = mat[i + 1][j];
                    }
                    if (j > 0) {
                        adjacentSpots[2] = mat[i][j - 1];
                    }
                    if (j < rows - 1) {
                        adjacentSpots[3] = mat[i][j + 1];
                    }
                    if (thisSpot.type == ' ') {
                        for (int k = 0; k < 4; k++) {
                            if (adjacentSpots[k] != null && adjacentSpots[k].distance < (thisSpot.distance - 1)) {
                                thisSpot.distance = adjacentSpots[k].distance + 1;
                                thisSpot.closest = adjacentSpots[k];
                                changeMade = true;
                            }
                        }
                    }
                }
            }
        }

        spot spot = endSpot;
        while(spot != startSpot) {
            moves++;
            spot.type = '.';
            spot = spot.closest;
        }

        System.out.println(moves);
    }
}

最佳答案

带回溯的广度优先搜索。

关于java - 这是什么最短路径迷宫算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22467100/

相关文章:

java - Selenium 3.0 Firefx 驱动程序失败,出现 org.openqa.selenium.SessionNotCreatedException : Unable to create new remote session

Java索引ArrayOutOfBounds问题

php - PHP中遍历多维数组的递归方法

java迷宫游戏错误仅朝一个方向进行,使用数组

java - 是否有必要使用 JPA 和 MySQL 获取实体以引用它?

java - Flutter:MainActivity 在 Java Native 代码中无法转换为 FlutterEngine

algorithm - 如何表示可能的最大功率

algorithm - 提取 k 个最大的元素

java - 用java从txt文件中读取迷宫

actionscript-3 - As3 如何翻转影片剪辑以面向运动方向?