Java 有限网格中的路径搜索

Java 有限网格中的路径搜索,java,recursion,path-finding,Java,Recursion,Path Finding,我有一个7x7的网格(一个城市)。我想从起点(x1,y1)移动到终点(x2,y2),并以多个(x,y)字符串(由COMA分隔)的形式将路径显示为输出。我想使用递归执行此操作,如果路径存在,则输出true,否则输出false。 我该怎么做?我在考虑用4种方法来检查4个方向。如有任何意见,将不胜感激 现在,我刚刚实现了类的构造函数:- map = new boolean[row][column]; for(int i = 0; i < 7; i++) {

我有一个7x7的网格(一个城市)。我想从起点
(x1,y1)
移动到终点
(x2,y2)
,并以多个
(x,y)
字符串(由COMA分隔)的形式将路径显示为输出。我想使用递归执行此操作,如果路径存在,则输出true,否则输出false。
我该怎么做?我在考虑用4种方法来检查4个方向。如有任何意见,将不胜感激

现在,我刚刚实现了类的构造函数:-

    map = new boolean[row][column];

    for(int i = 0; i < 7; i++)
    {
        for(int j = 0; j < 7; j++)
        { 
            map[i][j] = false; 
        }
    }
map=new boolean[行][列];
对于(int i=0;i<7;i++)
{
对于(int j=0;j<7;j++)
{ 
map[i][j]=假;
}
}
导入java.io.PrintStream;
/**
*德夫帕齐瓦尔酒店
*日期:2020年10月27日时间:18:28
*我对自己的生活充满信心,这来自于我自己的双脚站得很高。
*/
公共类路径{
静态打印流输出=System.out;
静态布尔pathfound=false;
//////////主要/////////
公共静态void main(字符串$[]){
布尔型arr[][]=新布尔型[5][5];

对于(int i=0;i假设城市是一个迷宫类型的东西,你可以尝试这样做:路径查找实际上不是一项试验任务,但有许多不同的算法。其中之一是
a*
,它是
Dijkstra算法的一个变体。
。在第一个for循环中,我设置路径。我选择了5x5矩阵u,可以根据你的意愿给出任何大小。在travel的第一个参数是网格,然后开始行和列,然后是目标行和列。非常感谢,我想知道如果我正在制作一个方法,比如goSouthWest,它只解决了西南方向的问题,使用递归,任何想法都会很受欢迎。基本上,我的目标是制作4个方法
goSouthWest
goSouthEast
goNorthWest
goNorthWest
。这些方法都是通过方法
givePath
访问的,该方法基本上检查destRow或destCol是否小于、等于或大于startRow或startCol,并调用上述4种方法之一来解决问题ethod递归地求解它,并以(x,y)(x,y)(x,y)…的形式输出字符串,其中
(x,y)
是这条路的一步。我想你想从一个单元朝8个方向走,即北、南、东、西、东北、西北、东南和西南?是的,这个代码基本上分为几个方向,水平和垂直搜索,它也按对角线搜索。我想确认一下,你的对角线方向是我的吗方法正确吗?正如在西南
中一样,[r+1][c-1]基本上告诉我们向上移动一行,然后后退一列,这只不过是西北。如果这是一个错误,它也存在于其他方法中。如果不是,你能解释一下这里发生了什么吗?@Nzed假设你在单元格
[4,4]
,目标在
[5,3]
然后
[r+1][c-1]
它的说明
在一行中下移
,在一列中后退。
import java.io.PrintStream;

/**
 * Dev Parzival
 * Date : 27/10/2020 Time : 18:28
 * I have a confidence about my life that comes from standing tall on my own two feet.
 */

public class Path {
    static PrintStream out=System.out;
    static boolean pathfound=false;
    //////////Main/////////
    public static void main(String $[]){
        boolean arr[][]=new boolean[5][5];
        for(int i=0;i<arr.length;i++)
            for(int j=0;j<arr[0].length;j++) {
                if (i==0)
                    arr[i][j] = true;
                if(j==arr[0].length-1)
                    arr[i][j]=true;
            }
        travel(arr,0,0,arr.length-1,arr[0].length-1);
        if(pathfound)
            out.println("Path is found");
        else
            out.println("Path is not found");
    }
    
    static boolean travel(boolean a[][],int r,int c,int targetR,int targetC){
    //Target is found
    if(r==targetR && c==targetC){
        pathfound=true;
    }
    //South
    if(!pathfound && r+1<a.length)
        if(a[r+1][c]){
            a[r+1][c]=false;
            pathfound=travel(a,r+1,c,targetR,targetC);
        }
    //North
    if(!pathfound && r-1>=0)
        if(a[r-1][c]){
            a[r-1][c]=false;
            pathfound=travel(a,r-1,c,targetR,targetC);
        }
    //East
    if(!pathfound && c+1<a[0].length)
        if(a[r][c+1]){
            a[r][c+1]=false;
            pathfound=travel(a,r,c+1,targetR,targetC);
        }
    //West
    if(!pathfound && c-1>=0)
        if(a[r][c-1]){
            a[r][c-1]=false;
            pathfound=travel(a,r,c-1,targetR,targetC);
        }
        if(pathfound)
            out.println(r+" "+c);
        return pathfound;
    }
}
import java.io.PrintStream;

/**
 * Dev Parzival
 * Date : 27/10/2020 Time : 18:28
 * I have a confidence about my life that comes from standing tall on my own two feet.
 */

public class Path {
    static PrintStream out=System.out;
    static boolean pathfound=false;
    //////////Main/////////
    public static void main(String $[]){
        boolean arr[][]=new boolean[5][5];
        for(int i=0;i<arr.length;i++)
            for(int j=0;j<arr[0].length;j++) {
                if (i==j)
                    arr[i][j] = true;
//                if(j==arr[0].length-1)
//                    arr[i][j]=true;
            }
        travel(arr,0,0,arr.length-1,arr[0].length-1);
        if(pathfound)
            out.println("Path is found");
        else
            out.println("Path is not found");
    }

    static boolean travel(boolean a[][],int r,int c,int targetR,int targetC){

        //Target is found
        if(r==targetR && c==targetC){
            pathfound=true;
        }
        //South
        if(!pathfound && r+1<a.length)
            if(a[r+1][c]){
                a[r+1][c]=false;
                pathfound=travel(a,r+1,c,targetR,targetC);
            }
        //North
        if(!pathfound && r-1>=0)
            if(a[r-1][c]){
                a[r-1][c]=false;
                pathfound=travel(a,r-1,c,targetR,targetC);
            }
        //East
        if(!pathfound && c+1<a[0].length)
            if(a[r][c+1]){
                a[r][c+1]=false;
                pathfound=travel(a,r,c+1,targetR,targetC);
            }
        //West
        if(!pathfound && c-1>=0)
            if(a[r][c-1]){
                a[r][c-1]=false;
                pathfound=travel(a,r,c-1,targetR,targetC);
            }
        //North-West
        if(!pathfound && c-1>=0 && r-1>=0)
            if(a[r-1][c-1]){
                a[r-1][c-1]=false;
                pathfound=travel(a,r-1,c-1,targetR,targetC);
            }
        //Noth-East
        if(!pathfound && c+1<a[0].length && r-1>=0)
            if(a[r-1][c+1]){
                a[r-1][c+1]=false;
                pathfound=travel(a,r-1,c+1,targetR,targetC);
            }
        //South-East
        if(!pathfound && c+1<a[0].length && r+1<a.length)
            if(a[r+1][c+1]){
                a[r+1][c+1]=false;
                pathfound=travel(a,r+1,c+1,targetR,targetC);
            }
        //South-West
        if(!pathfound && c-1>=0 && r+1<a.length)
            if(a[r+1][c-1]){
                a[r+1][c-1]=false;
                pathfound=travel(a,r+1,c-1,targetR,targetC);
            }
        //Print the cell position
        if(pathfound)
            out.println(r+" "+c);
        return pathfound;
    }
}
4 4
3 3
2 2
1 1
0 0
Path is found