//paper to use its improved algorithm, here first demo test
UsingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacedfs{classProgram { Public int[,] map =New int[ -, -]; Public int[] Road =New int[ -]; Public intn, x, y; Public intm =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 voidMain (string[] args) {Program Pro=NewDFS. Program (); intI, 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 voidDfsintp) {Visited[p]=1; intI, J; for(i =0; I < n; i++) { if(Map[p,i] = =1&& Visited[i] = =0) { if(i = = y)///If you reach the end, you can output the path you just passed. { for(j =0; J < M; J + +) {Console.WriteLine ("{0}", Road[j]); } Console.WriteLine ("{0}\r\n", y); } Else///If the point is not the end{Map[p,i]=0; ROAD[M]= i;///Save the point .m++; DFS (i);///then deep searchMap[p,i] =1; Visited[i]=0; M--; } } } } }}
C # Depth-First search algorithm demo