How to prevent infinite recursion in a non-destructive way?

that my first question is here, so I apologize in advance if I have not received the data, good enough and duplicated the question.

So, I'm trying to create some kind of game where at some point you have such a matrix:

 _ _ _ _ _ _
|_|_|h|h|h|h|
|_|_|_|_|_|_|
|_|#|#|#|_|_|
|_|_|_|#|_|_|
|_|h|_|_|_|_|
|_|h|#|_|_|_|   

And I need a method that checks if there are "elements" created only by "h'-es", and if he finds it, to change all the characters from "h" to something else, like "d", for example, so it becomes:

 _ _ _ _ _ _
|_|_|d|d|d|d|
|_|_|_|_|_|_|
|_|#|#|#|_|_|
|_|_|_|#|_|_|
|_|h|_|_|_|_|
|_|h|#|_|_|_|   

"h'-es" , "#" - s for- , arr [y] [x]. , , , ... StackOverflow ? :

public class matrixRec {
    public static void main(String[] args) {
        char[][] matrix = {
            {' ',' ',' ',' ',' ',' '},
            {' ','h','h','h','h',' '},
            {' ',' ',' ',' ',' ',' '},
            {' ',' ',' ','#','#',' '},
            {' ','h',' ',' ','#','#'},
            {' ','h','#',' ',' ',' '}
        };
        System.out.println(elementChange(matrix, 1, 2));
    }
    static boolean elementChange(char[][] matrix, int y, int x){

        if (matrix[y][x] == ' '){
            return true;
        }
        else if (matrix[y][x] == '#'){
            return false;
        }
        else if (matrix[y][x] == 'h'){
            return (elementChange(matrix, y, x-1) && elementChange(matrix, y, x+1));
        }
        else {
            return false;
        }
    }
}

, , "h'-es" , , "h"?

+4
1

, :

,

public class matrixRec {
    public static void main(String[] args) {
        char[][] matrix = {
            {' ',' ',' ',' ',' ',' '},
            {' ','h','h','h','h',' '},
            {' ',' ',' ',' ',' ',' '},
            {' ',' ',' ','#','#',' '},
            {' ','h',' ',' ','#','#'},
            {' ','h','#',' ',' ',' '}
        };
        boolean visited[][] = new boolean[6][6] //boolean arrays default to false
        System.out.println(elementChange(matrix, 1, 2, visited));
    }
    static boolean elementChange(char[][] matrix, int y, int x. boolean[][] visited){
        if(visited[y][x]) return ((matrix[y][x] == ' ' || matrix[y][x] == 'h')? true: false);
        visited[y][x] = true; //We have visited this cell
        if (matrix[y][x] == ' '){
            return true;
        }
        else if (matrix[y][x] == '#'){
            return false;
        }
        else if (matrix[y][x] == 'h'){
            return elementChange(matrix, y, x-1,visited) && elementChange(matrix, y, x+1,visited);
        }
        else {
            return false;
        }
    }
}

? ?

public class matrixRec {
    public static void main(String[] args) {
        char[][] matrix = {
            {' ',' ',' ',' ',' ',' '},
            {' ','h','h','h','h',' '},
            {' ',' ',' ',' ',' ',' '},
            {' ',' ',' ','#','#',' '},
            {' ','h',' ',' ','#','#'},
            {' ','h','#',' ',' ',' '}
        };
        boolean visited[][] = new boolean[6][6] //boolean arrays default to false
        System.out.println(elementChange(matrix, 1, 2, visited));
    }
    static boolean elementChange(char[][] matrix, int y, int x. boolean[][] visited){
        if(visited[y][x]) return ((matrix[y][x] == ' ' || matrix[y][x] == 'h')? true: false);
        visited[y][x] = true; //We have visited this cell
        if (matrix[y][x] == ' '){
            return true;
        }
        else if (matrix[y][x] == '#'){
            return false;
        }
        else if (matrix[y][x] == 'h'){
            return elementChange(matrix, y, x-1,visited) && elementChange(matrix, y, x+1,visited) && elementChange(matrix, y-1, x,visited) && elementChange(matrix, y+1, x,visited);
        }
        else {
            return false;
        }
    }
}

,

public class matrixRec {
    public static void main(String[] args) {
        char[][] matrix = {
            {' ',' ',' ',' ',' ',' '},
            {' ','h','h','h','h',' '},
            {' ',' ',' ',' ',' ',' '},
            {' ',' ',' ','#','#',' '},
            {' ','h',' ',' ','#','#'},
            {' ','h','#',' ',' ',' '}
        };
        boolean visited[][] = new boolean[6][6] //boolean arrays default to false
        System.out.println(elementChange(matrix, 1, 2, visited));
    }
    static boolean elementChange(char[][] matrix, int y, int x. boolean[][] visited){
        if(visited[y][x]) return ((matrix[y][x] == ' ' || matrix[y][x] == 'h')? true: false);
        visited[y][x] = true; //We have visited this cell
        if (matrix[y][x] == ' '){
            return true;
        }
        else if (matrix[y][x] == '#'){
            return false;
        }
        else if (matrix[y][x] == 'h'){

            if((elementChange(matrix, y, x-1,visited) && elementChange(matrix, y, x+1,visited) && elementChange(matrix, y-1, x,visited) && elementChange(matrix, y+1, x,visited))
                matrix[y][x] = 'd'; //Will only change areas that were searched to not be next to a #
        }
        else {
            return false;
        }
    }
}

, . / , , .

+2

Source: https://habr.com/ru/post/1618498/


All Articles