Java 查找矩阵中从左上角到右下角遍历所需的步数

Java 查找矩阵中从左上角到右下角遍历所需的步数,java,depth-first-search,breadth-first-search,path-finding,Java,Depth First Search,Breadth First Search,Path Finding,矩阵A中有6行4列。 其中'#'=阻塞路径和'.=允许路径 A = [[. . . #], [# . # #], [# . # .], [# . . .], [# . . .], [# . . .] ] 如何找到从左上角到左下角所需的步数。我可以从左上角到右下角遍历矩阵,但无法找到步数(此处为8)。。但下面的代码我得到的答案是12,这是错误的 我的代码如下: private static int numSteps(char[]

矩阵A中有6行4列。 其中
'#'=阻塞路径
'.=允许路径

A = [[. . . #], 
     [# . # #], 
     [# . # .], 
     [# . . .], 
     [# . . .],
     [# . . .]
    ] 
如何找到从左上角到左下角所需的步数。我可以从左上角到右下角遍历矩阵,但无法找到
步数(此处为8)。
。但下面的代码我得到的答案是
12
,这是错误的

我的代码如下:

private static int numSteps(char[][] A) {
        
        int row = A.length;
        
        int col = A[0].length;
        
        // directions array for row and column
        // for north, south, east , west
        int r[] = {-1, 1, 0, 0};
        int c[] = {0, 0, 1, -1};
        
        int steps = 0;
        
        LinkedList<String> queuePos = new LinkedList<String>();
        
        queuePos.add("0,0");
        
        boolean[][] visited = new boolean[row][col];
        
        while(!queuePos.isEmpty()) {
            
                String pos = queuePos.poll();
                int rowPos = Integer.parseInt(pos.split(",")[0]);
                int colPos = Integer.parseInt(pos.split(",")[1]);
                
                if(rowPos >= row - 1 && colPos>= col -1) {
                    
                    return steps;
                    
                }
                
                // looping for the four directions for surrounding nodes/neighbours
                for(int i=0; i<r.length; i++) {
                    
                    int newRow = rowPos + r[i];
                    
                    int newCol = colPos + c[i];
                    
                    if(newRow < 0 || newCol < 0 || newRow >= row || newCol >= col || A[newRow][newCol] == '#' || visited[newRow][newCol]) {
                        
                        continue;
                        
                    }
                    
                    visited[newRow][newCol] = true;
                    
                    queuePos.add(newRow + "," + newCol);
                    
                    if(newRow == row - 1 && newCol == col -1) {
                        
                        return steps;
                        
                    }
                    
            }
            
            
            steps+=1;
            
        }
        
        return steps;
        
    }
private static int numSteps(char[]A){
int行=A.长度;
int col=A[0]。长度;
//行和列的方向数组
//北、南、东、西
int r[]={-1,1,0,0};
int c[]={0,0,1,-1};
int步数=0;
LinkedList queuePos=新建LinkedList();
队列位置添加(“0,0”);
boolean[][]已访问=新的boolean[row][col];
而(!queuePos.isEmpty()){
字符串pos=queuePos.poll();
int rowPos=Integer.parseInt(pos.split(“,”[0]);
int colPos=Integer.parseInt(pos.split(“,”[1]);
if(rowPos>=行-1&&colPos>=列-1){
返回步骤;
}
//为周围节点/邻居的四个方向循环
对于(int i=0;i=row | | newCol>=col | | A[newRow][newCol]=='#'| |已访问[newRow][newCol]){
继续;
}
访问[newRow][newCol]=真;
queuePos.add(newRow+,“+newCol);
if(newRow==行-1&&newCol==列-1){
返回步骤;
}
}
步骤+=1;
}
返回步骤;
}

我不知道应该在哪里将
“steps”
变量增加1

while(!queuePos.isEmpty()) {
    int size = queuePos.size();
    for (int idx = 0; idx < size; idx++) {
    ...
    }
    steps+=1;
}
所以,稍微修改一下的版本是:

    private static int numSteps(char[][] A) {

        int row = A.length;

        int col = A[0].length;

        // directions array for row and column
        // for north, south, east , west
        int r[] = {-1, 1, 0, 0};
        int c[] = {0, 0, 1, -1};

        int steps = 0;

        LinkedList<String> queuePos = new LinkedList<String>();

        queuePos.add("0,0");

        boolean[][] visited = new boolean[row][col];

        while(!queuePos.isEmpty()) {

            int size = queuePos.size();
            for (int idx = 0; idx < size; idx++) {
                String pos = queuePos.poll();
                int rowPos = Integer.parseInt(pos.split(",")[0]);
                int colPos = Integer.parseInt(pos.split(",")[1]);

                if(rowPos >= row - 1 && colPos>= col -1) {

                    return steps;

                }

                // looping for the four directions for surrounding nodes/neighbours
                for(int i=0; i<r.length; i++) {

                    int newRow = rowPos + r[i];

                    int newCol = colPos + c[i];

                    if(newRow < 0 || newCol < 0 || newRow >= row || newCol >= col || A[newRow][newCol] == '#' || visited[newRow][newCol]) {

                        continue;

                    }

                    visited[newRow][newCol] = true;

                    queuePos.add(newRow + "," + newCol);
                }
            }


            steps+=1;

        }

        return steps;

    }
private static int numSteps(char[]A){
int行=A.长度;
int col=A[0]。长度;
//行和列的方向数组
//北、南、东、西
int r[]={-1,1,0,0};
int c[]={0,0,1,-1};
int步数=0;
LinkedList queuePos=新建LinkedList();
队列位置添加(“0,0”);
boolean[][]已访问=新的boolean[row][col];
而(!queuePos.isEmpty()){
int size=queuePos.size();
对于(int-idx=0;idx=行-1&&colPos>=列-1){
返回步骤;
}
//为周围节点/邻居的四个方向循环
对于(int i=0;i=row | | newCol>=col | | A[newRow][newCol]=='#'| |已访问[newRow][newCol]){
继续;
}
访问[newRow][newCol]=真;
queuePos.add(newRow+,“+newCol);
}
}
步骤+=1;
}
返回步骤;
}

谢谢..我错过了一个循环
    private static int numSteps(char[][] A) {

        int row = A.length;

        int col = A[0].length;

        // directions array for row and column
        // for north, south, east , west
        int r[] = {-1, 1, 0, 0};
        int c[] = {0, 0, 1, -1};

        int steps = 0;

        LinkedList<String> queuePos = new LinkedList<String>();

        queuePos.add("0,0");

        boolean[][] visited = new boolean[row][col];

        while(!queuePos.isEmpty()) {

            int size = queuePos.size();
            for (int idx = 0; idx < size; idx++) {
                String pos = queuePos.poll();
                int rowPos = Integer.parseInt(pos.split(",")[0]);
                int colPos = Integer.parseInt(pos.split(",")[1]);

                if(rowPos >= row - 1 && colPos>= col -1) {

                    return steps;

                }

                // looping for the four directions for surrounding nodes/neighbours
                for(int i=0; i<r.length; i++) {

                    int newRow = rowPos + r[i];

                    int newCol = colPos + c[i];

                    if(newRow < 0 || newCol < 0 || newRow >= row || newCol >= col || A[newRow][newCol] == '#' || visited[newRow][newCol]) {

                        continue;

                    }

                    visited[newRow][newCol] = true;

                    queuePos.add(newRow + "," + newCol);
                }
            }


            steps+=1;

        }

        return steps;

    }