二维矩阵迷宫中最便宜路径的寻路算法

时间:2019-06-24 02:52:49

标签: java path-finding maze

我必须找到矩阵成本最低的路径,并用Java实现解决方案。我不确定哪种算法最适合解决此问题。

我有一个MxN矩阵,可以在每个单元格中包含0、1、2、3或4作为输入,以及一个起始位置和一个目标位置(例如:起始位置(0,0)和目标位置(5,7))。

0表示通过该单元格的费用为1。

1表示通过该单元格的成本为2。

2表示通过该单元格的成本为3。

3表示通过该单元格的成本为4。

4表示有一堵​​墙,所以您无法穿过它。

您可以在任何方向上上下左右移动(但是,如果垂直移动,通过任何单元格的成本将增加一倍)。

BFS是好的算法还是与二进制迷宫效果最佳?

1 个答案:

答案 0 :(得分:0)

感谢您的建议!我可以使用以下代码解决它(或者至少对我有用):

public class Pathfinder {
private static final int M = 5;
private static final int N = 5;
private static final int fila[] = {-1, 0, 0, 1, -1, -1, 1, 1};
private static final int columna[] = {0, -1, 1, 0, -1, 1, -1, 1};
private static boolean esValido(int matriz[][], boolean visitado[][], int fil, int col, int k) {
    if(k < 4)
        return ((fil >= 0) && (fil < M) && (col >= 0) && (col < N) && matriz[fil][col] < 4 && !visitado[fil][col]);
    else if (k == 4)
        return ((fil >= 0) && (fil < M) && (col >= 0) && (col < N) && matriz[fil][col] < 4 && !visitado[fil][col] && esValido(matriz, visitado, fil, col + 1, 0) && esValido(matriz, visitado, fil + 1, col, 0));
    else if (k == 5)
        return ((fil >= 0) && (fil < M) && (col >= 0) && (col < N) && matriz[fil][col] < 4 && !visitado[fil][col] && esValido(matriz, visitado, fil, col - 1, 0) && esValido(matriz, visitado, fil + 1, col, 0));
    else if (k == 6)
        return ((fil >= 0) && (fil < M) && (col >= 0) && (col < N) && matriz[fil][col] < 4 && !visitado[fil][col] && esValido(matriz, visitado, fil, col + 1, 0) && esValido(matriz, visitado, fil - 1, col, 0));
    else
        return ((fil >= 0) && (fil < M) && (col >= 0) && (col < N) && matriz[fil][col] < 4 && !visitado[fil][col] && esValido(matriz, visitado, fil, col - 1, 0) && esValido(matriz, visitado, fil - 1, col, 0));
}
private static int absoluto (int n) {
    return n > 0 ? n : -n;
}

public static void BFS(int matriz[][], int i, int j, int x, int y) {
    boolean [][] visitado = new boolean [M][N];
    PriorityQueue<Nodo> cola = new PriorityQueue<Nodo>();
    visitado[i][j] = true;
    cola.add(new Nodo(i, j, 0, absoluto(x - i) + absoluto(y - j)));
    int mincost = Integer.MAX_VALUE;
    int cost, aux;
    while(!cola.isEmpty()) {
        Nodo nodo = cola.poll();
        i = nodo.x;
        j = nodo.y;
        cost = nodo.cost;
        if(i == x && j == y) {
            mincost = cost;
            break;
        }
        for(int k = 0; k < 8; k++) {
            aux = 0;
            if(esValido(matriz, visitado, i + fila[k], j + columna[k], k)) {
                if(k < 4)
                    aux = 1;
                else
                    aux = 2;
                visitado[i + fila[k]][j + columna[k]] = true;
                switch(matriz[i + fila[k]][j + columna[k]]) {
                case 0:
                    cola.add(new Nodo(i + fila[k], j + columna[k], cost + aux, absoluto(x - i + fila[k]) + absoluto(y - j + columna[k])));
                case 1:
                    cola.add(new Nodo(i + fila[k], j + columna[k], cost + aux*2, absoluto(x - i + fila[k]) + absoluto(y - j + columna[k])));
                case 2:
                    cola.add(new Nodo(i + fila[k], j + columna[k], cost + aux*3, absoluto(x - i + fila[k]) + absoluto(y - j + columna[k])));
                case 3:
                    cola.add(new Nodo(i + fila[k], j + columna[k], cost + aux*4, absoluto(x - i + fila[k]) + absoluto(y - j + columna[k])));
                }   
            }
        }
    }

    if(mincost != Integer.MAX_VALUE)
        System.out.print("The path of minimum cost to the destination is " + mincost + ".");
    else
        System.out.print("Destination cannot be reached.");
}
}

我使用的节点结构如下:

public class Nodo implements Comparable<Nodo> {
public int x, y, cost, dist, total;
public Nodo(int x, int y, int cost, int dist){
    this.x = x;
    this.y = y;
    this.cost = cost;
    this.dist = dist;
    this.total = this.cost + this.dist;
}
public int compareTo(Nodo n) {
    if(this.total < n.total)
        return -1;
    else if(this.total > n.total)
        return 1;
    else
        return 0;
}
}