本文實(shí)例為大家分享了C#深度優(yōu)先搜索算法的具體代碼,供大家參考,具體內(nèi)容如下
//論文要用到其改進(jìn)算法,在此先demo測(cè)試一下using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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 DFS.Program();      int i, j;      pro.n = int.Parse(Console.ReadLine());      pro.x= int.Parse(Console.ReadLine());      pro.y= int.Parse(Console.ReadLine());          for (i = 0; i < pro.n; i++)      {        for (j = 0; j < pro.n; j++)        {          pro.map[i,j]= int.Parse(Console.ReadLine());        }      }      pro.road[0] = pro.x;      pro.dfs(pro.x);    }    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)///如果深搜到了終點(diǎn),就輸出剛才經(jīng)過的路徑           {            for (j = 0; j < m; j++)            {              Console.WriteLine("{0}", road[j]);            }            Console.WriteLine("{0}/r/n", y);          }          else///如果該點(diǎn)不是終點(diǎn)           {            map[p,i] = 0;            road[m] = i;///將該點(diǎn)存起來             m++;            dfs(i);///接著深搜             map[p,i] = 1;            visited[i] = 0;            m--;          }        }      }    }  }}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選