遍历2D阵列并显示所采用的路径

时间:2015-04-18 00:15:51

标签: java arrays

我试图遍历一个2D数组,首先打印出一个0和1的随机数组,然后循环遍历同一个数组,将0更改为2s,显示从顶部到底部的路径。它编译,但我无法弄清楚它是否可以在不忘记随机数组的情况下更改变量。我尝试使用另一组for循环,这可能不是正确的方法......

以下是我的想法。

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

它会将底部的一些数字更改为2s,只是抛出错误或生成大量的1而不会停止。

我遇到的一些错误......

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at SearchMaze.main(SearchMaze.java:52)

Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: -1
at SearchMaze.main(SearchMaze.java:42)

1 个答案:

答案 0 :(得分: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