Underground Labyrinth Exploration (30 points) (DFS)

Tunnel warfare was a method of fighting against Japanese invaders by tunnels on the North China Plain during the Anti-Japanese War. The tunnel network is the underground fortifications of houses connecting houses, streets connecting streets, and villages connecting villages, as shown in the figure below.

Insert picture description here

While reviewing the arduous war lives of our predecessors, we sincerely admire their ingenuity. In the era of peaceful development, for most people, exploring underground passages may be just an entertainment or puzzle game. This experimental case is based on exploring the maze of underground passages.

Suppose there is a labyrinth of underground passages. Its passages are straight, and there is a light and a switch at all intersections of the passages (including the ends of the passages). How do you light up all the lights in the maze from a certain starting point and return to the starting point?

Insert picture description here

Input format:
Input the first line to give three positive integers, which respectively represent the number of nodes in the underground maze N (1<N≤1000, which means all intersections and end points of the passage), the number of edges M (≤3000, which means the number of passages) and Explore the starting node number S (nodes are numbered from 1 to N). The subsequent M rows correspond to M edges (channels), and each row gives a pair of positive integers, which are the numbers of the two nodes directly connected to the edge.

Output format:
If the lights of all nodes can be lighted up, then output a sequence containing all nodes starting from S and ending with S. The adjacent nodes in the sequence must have edges (channels); otherwise, the lights of all nodes cannot be lighted. , But still output the sequence of nodes that light up some lights, and finally output 0, which means that the maze is not a connected graph.

Since the node sequence of depth-first traversal is not unique, in order to make the output have a unique result, we agree to visit (light on) in the order of priority of the small number of nodes. After lighting up all the lights that can be lighted, return to the starting point by returning to the original path

Input example 1:

6 8 1
1 2
2 3
3 4
4 5
5 6
6 4
3 6
1 5
1 2 3 4 5 6 5 4 3 2 1

Input example 1:

6 6 6
1 2
1 3
2 3
5 4
6 5
6 4
6 4 5 4 6 0

Question idea:
1. The basic algorithm of DFS is the prerequisite for this question.
2. This question is to improve the DFS algorithm, I hope we can "return to the park road" after completing the DFS traversal.
3. The comments are very detailed, you can comment or private message at any time if you don’t understand, I will reply as soon as possible

#include<iostream>
using namespace std;

int MGraph[1005][1005] = {
    
     0 };
int Visited[1005] = {
    
     0 };
int n, m, s, a, b, count1 = 0;
bool flag = false;		 
void DFS(int index);	

int main()
{
    
    
	cin >> n >> m >> s;

	while (m--)
	{
    
    
		cin >> a >> b;
		MGraph[a][b] = MGraph[b][a] = 1;
	}

	Visited[s] = 1;		//起点访问好了,设为1
	count1 = 1;			//已经访问一个点
	DFS(s);
	if (!flag)cout << " " << 0;			//如果不是连通图就输出“ 0”;

	return 0;
}
void DFS(int index)
{
    
    
	cout << index << " ";	//打印当前访问的点
	if (count1 == n)		//当访问的点数到达n时,也就是说这是一个连通图了,
	{
    
    
		flag = true;		//既然是连通图,那么我们就吧标记做好。true还是false看个人习惯
		return;				//递归出口
	}
	
	for (int i = 1; i <= n; i++)
	{
    
    
		if (MGraph[index][i] && !Visited[i])	//如果index-i 这条边存在,并且这个节点没有被访问过
		{
    
    
			Visited[i] = 1;						//访问这个顶点
			count1++;							//访问个数增加
			DFS(i);								//从这个点继续DFS
			//根据递归是通过堆栈实现的,下面的算法就是“往回走”所经历的语句
			if (index == s)						//如果当前访问的点和一开始的起点相同,代表已经回到原点了,就不要输出额外空格了
				cout << index;
			else
				cout << index << " ";			//如果还没回到原点,那么久继续输出空格
		}
	}
	return;
}

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/114098451