我最近一直在做一个学校项目 - 从 1 和 0 生成一个迷宫,在迷宫内创建随机起点和终点,并跟踪它们之间的最短路线。 1 是路径,0 是墙壁。
我选择模拟流水 - 我从起始坐标开始,一旦出现值 1(上、下、左或右),我就会增加计数器的值,从而形成一条路径的终点。目标是通过在计数器上向后追溯,可以从最终值追踪最短路线。
我遇到的问题是增加计数器来填充迷宫。希望代码能告诉我们更多信息。
public class Labyrinth {
static int jk, ik, is, js; //start and end coordinates
static int tmp[][]= new int[10][10];;
static Integer stala=2; //The counter
//static Integer licz;
public static void main(String[] args) {
//Stworzenie tablicy 0 i 1
int tab[][]= new int[10][10];
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
tab[i][j]= (int) (Math.random() + 0.5);
}
}
//Creating the starting point (99)
is= (int)((Math.random())*10);
js= (int)((Math.random())*10);
tab[is][js]=99;
//Creating the ending point (66)
int esc=0;
while (esc!=1) {
ik= (int)((Math.random())*10);
jk= (int)((Math.random())*10);
if(ik==is || jk==js)
continue;
else
esc=1;
}
tab[ik][jk]=66;
//creating a tmp table to display increased values
for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
tmp[i][j]= tab[i][j];
}
}
//executing a method of a flowing water
przypis(is,js);
//Displaying the maze and the tmp table maze
for (int i=0; i<10; i++) {
if(i>0)
System.out.println();
for (int j=0; j<10; j++) {
System.out.printf("%4d", tab[i][j]);
}
}
System.out.println();
System.out.println("S: "+"("+is+","+js+")");
System.out.println("K: "+"("+ik+","+jk+")");
for (int i=0; i<10; i++) {
if(i>0)
System.out.println();
for (int j=0; j<10; j++) {
System.out.printf("%4d", tmp[i][j]);
}
}
System.out.println();
System.out.println("S: "+"("+is+","+js+")");
System.out.println("K: "+"("+ik+","+jk+")");
}
static void przypis(int a, int b) {
//a,b
try {
if(a!=9) {
if((int)tmp[a+1][b]==1) {
tmp[a+1][b]=(int)stala;
przypis(a+1,b);
}
}
if(b!=9) {
if((int)tmp[a][b+1]==1) {
tmp[a][b+1]=(int)stala;
przypis(a,b+1);
}
}
if(a!=0) {
if((int)tmp[a-1][b]==1) {
tmp[a-1][b]=(int)stala;
przypis(a-1,b);
}
}
if(b!=0) {
if((int)tmp[a][b-1]==1) {
tmp[a][b-1]=(int)stala;
przypis(a,b-1);
}
}
stala++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
我不知道如何让计数器工作 - 这是我的主要问题。我们的目标是让它看起来像这样:
s16574的@property
我这样做的方法是创建一个递归函数,该函数获取迷宫中的当前位置并递归地调用自身到当前位置周围的四个位置:
此类函数的伪代码:
populatesMaze(currentX, currentY, currentCounter, mazeFinishX, mazeFinishY, mazeMatrix):
// Preventing a corner case in which our current position is outside of the maze...
if (currentX < mazeMaxtrix.lines and currentY < mazeMatrix.columns and currentX >= 0 and currentY >= 0):
// Preventing our cursor from walking over walls...
if (mazeMatrix[currentX][currentY] != 0)
mazeMatrix[currentPositionX][currentPositionY] = currentCounter
else:
return
// Until our position is not the final one, we keep going...
if (currentX != mazeFinishX and currentY != mazeFinishY ):
populatesMaze(currentpositionX + 1, currentY, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
populatesMaze(currentpositionX, currentY + 1, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
populatesMaze(currentpositionX - 1, currentY, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
populatesMaze(currentpositionX, currentY - 1, currentCounter + 1, mazeFinishX, mazeFinishY, mazeMatrix)
希望对您的考试有所帮助!
我是一名优秀的程序员,十分优秀!