Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.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
C# 二维矩阵中两个位置之间的最短路径_C#_Path_Depth First Search_Breadth First Search_Pathfinder - Fatal编程技术网

C# 二维矩阵中两个位置之间的最短路径

C# 二维矩阵中两个位置之间的最短路径,c#,path,depth-first-search,breadth-first-search,pathfinder,C#,Path,Depth First Search,Breadth First Search,Pathfinder,下面是一段代码,它从索引[0,0]执行路径查找,查找目的地,例如值“9”。BFS或DFS应该比下面的算法做得更好吗?有没有更好的算法来实现同样的目标的建议 using System; public class Test { public static int[,] matrix = { {1, 2, 8, 4}, {3, 0, 3, 4}, {4, 0, 2, 3}, {5,

下面是一段代码,它从索引[0,0]执行路径查找,查找目的地,例如值“9”。BFS或DFS应该比下面的算法做得更好吗?有没有更好的算法来实现同样的目标的建议

using System;

public class Test
{
    public static int[,] matrix =
        {
            {1, 2, 8, 4},
            {3, 0, 3, 4},
            {4, 0, 2, 3},
            {5, 0, 32, 9}
        };

    public static   int[,] solution = new int[4,4];


    public static void Main()
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

         FindPath( matrix, 9, rows, cols);
        // your code goes here
    }

    public static void FindPath(int[,] matrix, int destination, int rows, int cols)
    {
        bool[] visited = new bool[rows * cols];

        for(int i=0; i < rows; i++ )
        {
            for(int j =0; j < cols; j++)
            {
                solution[i,j] = 0;                  
            }
        }

        for(int i=0; i < rows*cols; i++ )
        {
            visited[i] = false; 
        }

        CountPath(0, 0, visited, rows, cols, 9);


        for(int i=0; i < rows; i++ )
        {
            for(int j =0; j < cols; j++)
            {
                Console.Write(" " + solution[i,j]);
            }

            Console.WriteLine("");
        }
    }

    public static bool CountPath(int row, int col, bool[] visited, int rows, int cols, int destination)
    {
        if(row < 0 || row >= rows || col < 0 || col >= cols || visited[row * cols + col] || matrix[row,col] == 0)
        {
            Console.WriteLine("False...");
            return false;   
        }
        Console.WriteLine(matrix[row,col]);
        Console.WriteLine(matrix[row,col]);


        if(matrix[row,col] == destination)
        {

            solution[row, col] = matrix[row,col];
            Console.Write("Found\n");
            return true;    
        }

        visited[row * cols + col] = true;
        solution[row, col] = matrix[row,col];

        Console.WriteLine(row);
        Console.Write(col);
        Console.WriteLine("-------------");

        bool hasdestination = CountPath(row + 1, col, visited, rows, cols, destination) ||
        CountPath(row , col + 1, visited, rows, cols, destination) ||
        CountPath(row - 1, col, visited, rows, cols, destination) ||
        CountPath(row , col - 1, visited, rows, cols, destination);

        if(!hasdestination)
        {
            Console.WriteLine("No luck go back...");
            solution[row, col] = 0;
            return false;           
        }

        return true;
    }
}
使用系统;
公开课考试
{
公共静态int[,]矩阵=
{
{1, 2, 8, 4},
{3, 0, 3, 4},
{4, 0, 2, 3},
{5, 0, 32, 9}
};
公共静态int[,]解决方案=新int[4,4];
公共静态void Main()
{
int rows=matrix.GetLength(0);
int cols=matrix.GetLength(1);
FindPath(矩阵,9,行,列);
//你的密码在这里
}
公共静态void FindPath(int[,]矩阵,int目标,int行,int列)
{
bool[]已访问=新bool[rows*cols];
对于(int i=0;i=行| |列<0 | |列>=列| | | | | | | | |列>=列| | | | |列|列| |列
{
控制台。WriteLine(“假…”);
返回false;
}
控制台写入线(矩阵[行,列]);
控制台写入线(矩阵[行,列]);
if(矩阵[行,列]==目的地)
{
解决方案[行,列]=矩阵[行,列];
Console.Write(“Found\n”);
返回true;
}
已访问[行*列+列]=真;
解决方案[行,列]=矩阵[行,列];
控制台写入线(世界其他地区);
控制台。写入(col);
Console.WriteLine(“--------------”;
bool hasdestination=CountPath(行+1,列,已访问,行,列,目的地)||
CountPath(行、列+1、已访问、行、列、目的地)||
CountPath(第1行,列,已访问,行,列,目的地)||
CountPath(行、列-1、已访问、行、列、目的地);
如果(!hasdestination)
{
Console.WriteLine(“没有运气回去…”);
解决方案[行,列]=0;
返回false;
}
返回true;
}
}

您的算法似乎是一个简单的递归洪水填充搜索。它将搜索每个位置,然后检查周围的所有位置。 您已经为网格边和已搜索的位置正确地实现了“提前退出”,并且看起来您正在将矩阵值0视为“墙”,因为它们也会触发“提前退出”退出

你提出这个问题的方式很不传统。您提到搜索值9。这种类型的搜索主要涉及到达特定目标位置(例如,值9位于矩阵的位置4,4),对于这种类型的搜索,a*算法或Dijkstra修改将在找到目标之前搜索较少的位置

原则上,您需要指定在这种情况下什么“更好”?对于我提到的搜索类型,通常的判断是减少搜索位置的数量“更好”。然而,在某些情况下,代码的简单性可能更为重要,或者不需要知道路径是什么(正如您的示例所暗示的),有时您需要保证的最短路径,有时您只需要一条合理合理的路径。这些案例中的每一个以及更多案例都经过了详细的考虑

您可能还想重新表述您的问题,因为您所做的并不是严格意义上的路径查找。您提供的算法仅指示路径是否存在,而不是该路径的步骤


特定递归函数的扩展将导致它垂直向下搜索,直到碰到墙或网格边缘。然后它将搜索右边的一个正方形,然后再次尝试向下搜索。如果你打印出搜索的位置,你会很快明白我的意思,当我说这个特定的算法将以一种非常古怪的方式搜索,并且只对“碰巧”在扩展模式早期出现的搜索目的地有效。

尽管你的代码不会返回路径,你为什么要搜索一个值的路径?pathfinding是您正在寻找的正确术语吗?既然您要求使用另一种算法,我建议您看看a*算法:重新措辞:我正在寻找的是图形中的最短路径。我现在更新了代码以包含该路径。但这对最短路径不起作用,只是对任何最先找到的路径都不起作用。实际问题是图中的最短路径。我现在修改了代码以显示路径,但正如您所指出的,它在时尚上确实非常古怪,并且仍然找不到最短的路径。Dijkistra是找到最短路径的唯一方法吗。。无法在上面记录每个路径的计数吗?@sunny yes可以使用类似于递归函数的搜索,为每个位置存储“访问时间”值,如果您再次使用较短路径访问该位置,则更新该位置。我想不起这种类型的寻径器的名字了,它通常与电子器件(例如芯片中的电路布局)有关。通常,当您希望找到到每个可能网格位置的最短路径时,该方法最有用。A*(发音为“A星”)或Dijkstra对于单路径更快。