Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在矩阵中找到从一点到另一点的逃生路线?_Java_Recursion_Matrix - Fatal编程技术网

Java 如何在矩阵中找到从一点到另一点的逃生路线?

Java 如何在矩阵中找到从一点到另一点的逃生路线?,java,recursion,matrix,Java,Recursion,Matrix,这不是家庭作业。这只是一个练习题 给定一个矩阵,求出从(0,0)到(N,N)的可能逃生路线数不能沿对角线移动。 “0”位置表示打开的单元格,“1”表示被阻止的单元格。我从(0,0)开始旅行,必须到达(N,N) 输入格式 4 第一行是一个奇数正整数,T(=0) { 如果(arr[m-1][n]==0) { 校验路径(m-1,n,“u”); } } } } }//阶级 我会保留第二个2D布尔数组来标记您已经访问过的单元格,如下面的代码片段所示。我还简化了代码的其他部分,以减少代码重复 当然,您需要

这不是家庭作业。这只是一个练习题

给定一个矩阵,求出从(0,0)到(N,N)的可能逃生路线数不能沿对角线移动。

“0”位置表示打开的单元格,“1”表示被阻止的单元格。我从(0,0)开始旅行,必须到达(N,N)

输入格式

4
第一行是一个奇数正整数,T(=0) { 如果(arr[m-1][n]==0) { 校验路径(m-1,n,“u”); } } } } }//阶级
我会保留第二个2D布尔数组来标记您已经访问过的单元格,如下面的代码片段所示。我还简化了代码的其他部分,以减少代码重复

当然,您需要在构造函数中初始化
visted
,就像使用
visted=new boolean[matrixLength][matrixLength]
初始化
arr
一样

int[][] arr;
boolean[][] visited;
final int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public boolean isValid(int x, int y) {
    return 0 <= x && x < matrixLength 
        && 0 <= y && y < matrixLength 
        && arr[x][y] == 0
        && !visited[x][y];
}


public void checkPaths(int x, int y) {
    if (x == matrixLength-1 && y == matrixLength-1) {
        no_of_escaped++;
    } else {
        for (int[] d : directions) {
            if (isValid(x + d[0], y + d[1])) {
                visited[x + d[0]][y + d[1]] = true;
                checkPaths(x + d[0], y + d[1]);
                visited[x + d[0]][y + d[1]] = false;
            }
        }
    }
}
int[]arr;
访问的布尔值[];
最终int[][]方向={{-1,0},{1,0},{0,-1},{0,1};
公共布尔值是有效的(整数x,整数y){

返回0我认为您得到了stackoverflow,因为您显示的实例中可能存在一个循环。检查路径时,请确保不要使用同一单元格两次。我应该如何对此进行检查?另一个数据结构还是有更简单的方法?
import java.io.BufferedReader;
import java.io.InputStreamReader;

class testclass {
int no_of_escapes = 0 ;
int[][] arr;
int matrixlength;
public static void main(String[] args) throws Exception 
{

    testclass obj = new testclass();
    obj.checkpaths(0,0,"");
    System.out.print(obj.no_of_escapes);

}//main

testclass()
{
    try
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    matrixlength =Integer.parseInt(br.readLine());      
     arr = new int[matrixlength][matrixlength];
    for( int k = 0; k < matrixlength; k++){

        String str = br.readLine();
        int count = 0;
        for(int j=0 ; j< ((2*matrixlength)-1); j++){
            int v = (int)str.charAt(j) - 48;
            if(v == -16){}
            else{
            arr[k][count] = v;
            count++;
            }

        }//for j

    }//for k

}
catch(Exception e){}
}

public void checkpaths(int m, int n,String direction){

    if((m == matrixlength -1) && (n == matrixlength-1))
    {
        no_of_escapes = no_of_escapes +1;
        return;
    }

    if(!direction.equals("l"))
    {
        if(m < matrixlength && n < matrixlength)
            {
                if((n+1) < matrixlength )
                    {
                        if(arr[m][n+1]==0 )
                            {
                                checkpaths(m,n+1,"r");
                            }
                    }
            }
    }

    if(!direction.equals("u"))
    {
        if((m+1) < matrixlength )
        {
            if(arr[m+1][n]==0 )
            {
            checkpaths(m+1,n,"d");                  
            }
        }
    }

    if(!direction.equals("r"))
    {
        if(m < matrixlength && n < matrixlength)
            {
                if((n+1) < matrixlength )
                    {
                        if(arr[m][n+1]==0 )
                            {
                                checkpaths(m,n+1,"l");
                            }
                    }
            }
    }

    if(!direction.equals("d"))
    {
        if((m-1)>=0)
        {
            if(arr[m-1][n]==0 )
            {
            checkpaths(m-1,n,"u");                  
            }

        }

    }


}
}//class
int[][] arr;
boolean[][] visited;
final int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public boolean isValid(int x, int y) {
    return 0 <= x && x < matrixLength 
        && 0 <= y && y < matrixLength 
        && arr[x][y] == 0
        && !visited[x][y];
}


public void checkPaths(int x, int y) {
    if (x == matrixLength-1 && y == matrixLength-1) {
        no_of_escaped++;
    } else {
        for (int[] d : directions) {
            if (isValid(x + d[0], y + d[1])) {
                visited[x + d[0]][y + d[1]] = true;
                checkPaths(x + d[0], y + d[1]);
                visited[x + d[0]][y + d[1]] = false;
            }
        }
    }
}