gpt4 book ai didi

java - 具有寻路算法的迷宫

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:05 33 4
gpt4 key购买 nike

我正在学习如何使用 A* 算法来查找路径,我想看看实现此目的的最佳方法。这就是我的想法,我想要一个起点和终点,然后通过构建函数构建迷宫,然后用产科填充它,然后运行 ​​A* 算法,以类似表格的格式打印路线,基本上将 0 更改为 3 以显示所采取的路径(产科将等于 1)。这听起来是个好计划吗?

我遇到的问题是我不知道将产科放入数组中的最佳方法。这是我到目前为止所拥有的:

public class Maze {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {


//start and end points in the array
int startx = 115;
int starty = 655;
int endx = 380;
int endy = 560;
//number of collums and rows
int row = 700;
int col = 500;
//size of maze
int maze [][] = new int [row][col];

makeMaze(row, col, maze);
printMaze(row, col, maze);



}

//fill mazz with 0
private static void makeMaze(int row, int col, int maze[][])
{
//fill maze with 0 for initilization
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
maze[i][j] = 0;
}

}
}
//print out array/maze
private static void printMaze(int row, int col, int maze[][])
{
//... Print array in rectangular form
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
System.out.print(" " + maze[i][j] );
}
System.out.println("");
}

}
//fill the array with obsticals
private void makeObsticals()
{
//obstical 1
//this represent the corners of the object
int ob1Point1 [][] = new int [220][616];
int ob1Point2 [][] = new int [220][666];
int ob1Point3 [][] = new int [251][670];
int ob1Point4 [][] = new int [272][647];

//object 2
int ob2Point1 [][] = new int [341][655];
int ob2Point2 [][] = new int [359][667];
int ob2Point3 [][] = new int [374][651];
int ob2Point4 [][] = new int [366][577];

//obejct 3
int ob3Point1 [][] = new int [311][530];
int ob3Point2 [][] = new int [311][559];
int ob3Point3 [][] = new int [339][578];
int ob3Point4 [][] = new int [361][560];
int ob3Point5 [][] = new int [361][528];
int ob3Point6 [][] = new int [113][516];

//object 4
int ob4Point1 [][] = new int [105][628];
int ob4Point2 [][] = new int [151][670];
int ob4Point3 [][] = new int [180][629];
int ob4Point4 [][] = new int [156][577];
int ob4Point5 [][] = new int [113][587];

//object 5
int ob5Point1 [][] = new int [118][517];
int ob5Point2 [][] = new int [245][517];
int ob5Point3 [][] = new int [245][577];
int ob5Point4 [][] = new int [118][577];

//object 6
int ob6Point1 [][] = new int [280][583];
int ob6Point2 [][] = new int [333][583];
int ob6Point3 [][] = new int [333][665];
int ob6Point4 [][] = new int [280][665];

//object 7
int ob7Point1 [][] = new int [252][594];
int ob7Point2 [][] = new int [290][562];
int ob7Point3 [][] = new int [264][538];

//object 8
int ob8Point1 [][] = new int [198][635];
int ob8Point2 [][] = new int [217][574];
int ob8Point3 [][] = new int [182][574];


}
//astar algorithum
private void findPath()
{
}

}

感谢您对此提供的任何帮助

最佳答案

抱歉,但我不明白为什么你为障碍物声明了这么多二维数组......如你所说。 。 。

//obstacle1
//this represent the corners of the object
int ob1Point1 [][] = new int [220][616];
int ob1Point2 [][] = new int [220][666];
int ob1Point3 [][] = new int [251][670];
int ob1Point4 [][] = new int [272][647];

我认为从上面的代码中你想要表示(220,616),(220,666),(251,670),(272,647)是1个障碍物的角点。

如果是这样,那么我建议不要采用4个二维数组,而是用无穷大(即最大整数编号)标记迷宫[][]数组中障碍物覆盖的区域。 (假设为 10000)

对于其他 (x,y) 位置,在 maze[x][y] 中输入每个位置的启发值(这意味着从该 (x,y) 位置到达目的地 (endx,endy) 的成本)

然后应用A*算法从头到尾达到。

关于java - 具有寻路算法的迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12355272/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com