2D矩阵中两个位置之间的最短路径

时间:2015-11-04 08:09:58

标签: 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, 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;
    }
}

1 个答案:

答案 0 :(得分:1)

您的算法似乎是一个简单的递归填充搜索。它将搜索每个位置,然后检查所有周围的位置。 你已经正确地实施了一个早期的&#34;对于网格边缘和已经搜索过的位置,看起来你正在将矩阵值0处理为&#34; wall&#34;因为它们也会触发早期&#34;退出。

你说这个问题的方式是非常传统的。您提到搜索值9.大多数此类型的搜索涉及到达特定目标位置(例如,值9位于矩阵的位置4,4),对于该类型的搜索,A *算法或Dijkstra修改将在找到目的地之前搜索更少的位置。

主要是,您需要指定哪些更好?&#39;在这种情况下?我提到的搜索类型的通常判断是减少搜索位置的数量是更好的&#39;。然而,有些情况下代码简单性可能更重要,或者没有必要知道路径是什么(正如您的示例所暗示的那样),有时您需要保证最短路径,有时您只需要一条相当不错的路径。这些案例中的每一个案件都有更详细的考虑!

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

特定递归函数的扩展将导致它垂直向下搜索,直到它碰到墙或网格边缘。然后它将向右搜索一个方格然后再次尝试向下移动。如果您打印出搜索到的位置,当我说这个特定的算法将以非常古怪的方式进行搜索时,您会很快看到我的意思,并且只会对搜索目的地的搜索目的地有效。在扩张模式中提前到来。

相关问题