title: Shortest path between two location in 2D matrix
body: <p>Here is a code that does path finder from index[0,0] looking for destination , example the value '9'. Should BFS or DFS do any better than the below algorithm? Any advice for better algorithms to accomplish the same?</p>
  
  <pre><code>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 &lt; rows; i++ )
          {
              for(int j =0; j &lt; cols; j++)
              {
                  solution[i,j] = 0;                  
              }
          }
  
          for(int i=0; i &lt; rows*cols; i++ )
          {
              visited[i] = false; 
          }
  
          CountPath(0, 0, visited, rows, cols, 9);
  
  
          for(int i=0; i &lt; rows; i++ )
          {
              for(int j =0; j &lt; 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 &lt; 0 || row &gt;= rows || col &lt; 0 || col &gt;= 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;
      }
  }
  </code></pre>