如何以非破坏性的方式防止无限递归?

时间:2015-12-03 22:14:25

标签: java recursion

这是我的第一个问题,所以如果我没有进行数据挖掘并且复制问题,我会事先道歉。

所以我试图创建某种游戏,在某些时候你有一个像这样的矩阵:

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

我需要一种方法来检查是否有"元素"仅由' -es组成,如果找到一个,则更改所有字符“' h'对于其他类似于' d'例如,它变为:

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

虽然它不应该改变其他的' -es,但触摸#' -s 我可以使用for-cycles来遍历整个矩阵,或者直接从元素开始,因为我有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;
        }
    }
}

有没有一种方法可以防止无限递归,而不会更改' -es,直到我确定他们都是' h'

1 个答案:

答案 0 :(得分:2)

要做的事情:

每次检查某个区域时创建一个布尔值aray来检查被访问的单元格

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;
        }
    }
}

未来

这是一个需要一点思考和计划的算法领域。对于未来的研究/项目,一定要搜索洪水填充,递归回溯,图搜索和递归迷宫求解。