Java 遍历二维数组并显示所采用的路径
我尝试遍历一个2D数组,首先打印出一个0和1的随机数组,然后循环遍历该数组,将0更改为2,显示从顶部到底部的路径。它可以编译,但我不太明白如何让它在不忘记随机数组的情况下更改变量。我尝试使用另一组for循环,这可能不是正确的方法 这是我想到的Java 遍历二维数组并显示所采用的路径,java,arrays,Java,Arrays,我尝试遍历一个2D数组,首先打印出一个0和1的随机数组,然后循环遍历该数组,将0更改为2,显示从顶部到底部的路径。它可以编译,但我不太明白如何让它在不忘记随机数组的情况下更改变量。我尝试使用另一组for循环,这可能不是正确的方法 这是我想到的 import java.util.*; public class SearchMaze{ //Variables to set the values of the 2 arrays private static int n = 8
import java.util.*;
public class SearchMaze{
//Variables to set the values of the 2 arrays
private static int n = 8;
private static int m = 7;
private static int[][] maze = new int[n][m];
private static int i = 0;
private static int j = 0;
public static void main(String [] args){
//Randomly select 0s and 1s for the array
Random rand = new Random();
System.out.print(n + "\t" + m);
System.out.println("");
//creates the random array of 0s and 1s
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
maze[i][j] = rand.nextInt(2);
System.out.print(maze[i][j]);
}System.out.println("");
}
//here I was trying to loop through the array again while changing the 0s to 1s
//to show if a "path" from the top to the bottom exists
//but in doing this I am really just creating a different array of random 0s and 1s..
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
maze[i][j] = rand.nextInt(2);
while(i < n - 1 && j < m - 1){
if(maze[i+1][j] == 0)
{
maze[i+1][j] = 2;
i++;
}
else if(maze[i-1][j] == 0)
{
maze[i-1][j] = 2;
i++;
}
else if(maze[i][j+1] == 0)
{
maze[i][j+1] = 2;
j++;
}
else if(maze[i][j-1] == 0)
{
maze[i][j-1] = 2;
j++;
}
else
{
maze[i][j] = 1;
}
System.out.print(maze[i][j]);
}System.out.println();
}//end second for loop
}//end first for loop
}//end main method
}//end searchmaze
我想这就是你需要的
import java.util.*;
public class SearchMaze{
//Variables to set the values of the 2 arrays
private static int n = 8;
private static int m = 7;
private static int[][] maze = new int[n][m];
private static int i = 0;
private static int j = 0;
/*
* This function is used for searching a way from top to bottom exist or not.
*/
public static boolean searchMaze(int maze[][], int i, int j, int n, int m) {
if(i == n-1 && j > 0 && j < m && maze[i][j] == 0) {
// This is the condition when there is a way to the bottom.
return true;
} else if(i < 0 || i > m-1 || j < 0 || j > m-1 || maze[i][j] == 1) {
// This is the condition when path is end in some where in middle.
return false;
} else {
// There is three way to go bottom
return searchMaze(maze, i+1, j-1, n, m) || searchMaze(maze, i+1, j, n, m) || searchMaze(maze, i+1, j+1, n, m);
}
}
public static void main(String [] args){
//Randomly select 0s and 1s for the array
Random rand = new Random();
System.out.print(n + "\t" + m);
System.out.println("");
//creates the random array of 0s and 1s
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
maze[i][j] = rand.nextInt(2);
System.out.print(maze[i][j]);
}
System.out.println("");
}
//here I was trying to loop through the array again while changing the 0s to 1s
//to show if a "path" from the top to the bottom exists
//but in doing this I am really just creating a different array of random 0s and 1s..
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
// This is used to search a path from i,j to the bottom.
if(searchMaze(maze, i, j, n, m)) {
System.out.println("There is a way from "+i+" & "+j);
}
}//end second for loop
}//end first for loop
}//end main method
}//end searchmaze
我很难理解你的双for循环是如何在0到2之间完成你想要做的事情的?。如果你产生了大量的1,那可能是因为你陷入了你的else迷宫[i][j]=1;案例位于文件末尾附近。你得到一个无限循环,因为你既不改变i也不改变j。您能否编辑您的帖子,以包含您收到的确切错误消息,以及一些示例输出?@egracer添加了我遇到的一些错误,除了我得到的1和2的无限循环,原因很明显:p您尝试过如何调试?我还不清楚你到底在寻求什么样的帮助。看起来发生您发布的错误是因为您试图在i=0和j=0时获取maze[i-1][j]和maze[i][j-1],这会导致您的越界异常,因为数组的索引未低于0。
import java.util.*;
public class SearchMaze{
//Variables to set the values of the 2 arrays
private static int n = 8;
private static int m = 7;
private static int[][] maze = new int[n][m];
private static int i = 0;
private static int j = 0;
/*
* This function is used for searching a way from top to bottom exist or not.
*/
public static boolean searchMaze(int maze[][], int i, int j, int n, int m) {
if(i == n-1 && j > 0 && j < m && maze[i][j] == 0) {
// This is the condition when there is a way to the bottom.
return true;
} else if(i < 0 || i > m-1 || j < 0 || j > m-1 || maze[i][j] == 1) {
// This is the condition when path is end in some where in middle.
return false;
} else {
// There is three way to go bottom
return searchMaze(maze, i+1, j-1, n, m) || searchMaze(maze, i+1, j, n, m) || searchMaze(maze, i+1, j+1, n, m);
}
}
public static void main(String [] args){
//Randomly select 0s and 1s for the array
Random rand = new Random();
System.out.print(n + "\t" + m);
System.out.println("");
//creates the random array of 0s and 1s
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
maze[i][j] = rand.nextInt(2);
System.out.print(maze[i][j]);
}
System.out.println("");
}
//here I was trying to loop through the array again while changing the 0s to 1s
//to show if a "path" from the top to the bottom exists
//but in doing this I am really just creating a different array of random 0s and 1s..
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
// This is used to search a path from i,j to the bottom.
if(searchMaze(maze, i, j, n, m)) {
System.out.println("There is a way from "+i+" & "+j);
}
}//end second for loop
}//end first for loop
}//end main method
}//end searchmaze