SoFunction
Updated on 2025-04-05

C# Depth-first Search Algorithm

The examples in this article share with you the specific code of the C# depth priority search algorithm for your reference. The specific content is as follows

//The paper needs to use its improved algorithm, here's a demo testusing System;
using ;
using ;
using ;
using ;

namespace DFS
{
  class Program
  {
    public int[,] map = new int[100, 100];
    public int[] road = new int[120];
    public int n, x, y;
    public int m = 1;
    public int[] visited = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    static void Main(string[] args)
    {
      Program pro = new ();
      int i, j;
       = (());
      = (());
      = (());
    
      for (i = 0; i < ; i++)
      {
        for (j = 0; j < ; j++)
        {
          [i,j]= (());
        }
      }
      [0] = ;
      ();
    }
    public void dfs(int p)
    {
      visited[p] = 1;
      int i, j;
      for (i = 0; i < n; i++)
      {
        if (map[p,i] == 1 && visited[i] == 0)
        {
          if (i == y)///If you search for the end point, output the path you just passed          {
            for (j = 0; j < m; j++)
            {
              ("{0}", road[j]);
            }
            ("{0}\r\n", y);
          }
          else///If this point is not the end point          {
            map[p,i] = 0;
            road[m] = i;///Save this point            m++;
            dfs(i);///Continue to search            map[p,i] = 1;
            visited[i] = 0;
            m--;
          }
        }
      }
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.