UVA - 208 Firetruck (双向DFS)

        The Center City fire department collaborates with the transportation department to maintain mapsof the city which reflects the current status of the city streets. On any given day, several streets areclosed for repairs or construction. Firefighters need to be able to select routes from the firestations tofires that do not use closed streets.Central City is divided into non-overlapping fire districts, each containing a single firestation. Whena fire is reported, a central dispatcher alerts the firestation of the district where the fire is located andgives a list of possible routes from the firestation to the fire. You must write a program that the centraldispatcher can use to generate routes from the district firestations to the fires.

Input

    The city has a separate map for each fire district. Streetcorners of each map are identified by positiveintegers less than 21, with the firestation always on corner #1. The input file contains several test casesrepresenting different fires in different districts.• The first line of a test case consists of a single integer which is the number of the streetcornerclosest to the fire.• The next several lines consist of pairs of positive integers separated by blanks which are theadjacent streetcorners of open streets. (For example, if the pair 4 7 is on a line in the file, thenthe street between streetcorners 4 and 7 is open. There are no other streetcorners between 4 and7 on that section of the street.)• The final line of each test case consists of a pair of 0’s.

Output

    For each test case, your output must identify the case by number (‘CASE 1:’, ‘CASE 2:’, etc). It mustlist each route on a separate line, with the streetcorners written in the order in which they appear onthe route. And it must give the total number routes from firestation to the fire. Include only routeswhich do not pass through any streetcorner more than once. (For obvious reasons, the firedepartment doesn’t want its trucks driving around in circles.)Output from separate cases must appear on separate lines


Sample Input

6

1 2

1 3

3 4

3 5

4 6

5 6

扫描二维码关注公众号,回复: 1890370 查看本文章

2 3

2 4

0 0

4

2 3

3 4

5 1

1 6

7 8

8 9

2 5

5 7

3 1

1 8

4 6

6 9

0 0

Sample Output

CASE 1:

1 2 3 4 6

1 2 3 5 6

1 2 4 3 5 6

1 2 4 6

1 3 2 4 6

1 3 4 6

1 3 5 6

There are 7 routes from the firestation to streetcorner 6.

CASE 2:

1 3 2 5 7 8 9 6 4

1 3 41 5 2 3 4

1 5 7 8 9 6 4

1 6 4

1 6 9 8 7 5 2 3 4

1 8 7 5 2 3 4

1 8 9 6 4

There are 8 routes from the firestation to streetcorner 4.



一、原题地址

传送门

二、大致题意

    给出很多条路,询问有多少种走到n的路径。

三、思路

    首先我们从n点开始做一次DFS,跑出所有与n点连通的点,并做上标记。然后再从1开始跑一次DFS就可以了。(输出的路径需要字典序,所以建边的时候需要一次sort)

四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;


int n,road_num;
bool flag[25],vis[25];
vector<int>e[25],ans;
void init()
{
	ans.clear();
	road_num = 0;
	for (int i = 0; i <= 20; i++)
		e[i].clear();
	int x, y;
	while (scanf("%d%d", &x, &y), x + y)
	{
		e[x].push_back(y);
		e[y].push_back(x);
	}
	for (int i = 0; i <= 20; i++)
		sort(e[i].begin(), e[i].end());
	memset(flag, false, sizeof(flag));
	memset(vis, false, sizeof(vis));
}
void find(int x)
{
	flag[x] = true;
	int size = e[x].size();
	for (int i = 0; i < size; i++)
	{
		int to = e[x][i];
		if (!flag[to])
		{
			find(to);
		}
	}
}
void print()
{
	road_num++;
	int size = ans.size();
	for (int i = 0; i < size; i++)
	{
		if (i == 0)printf("%d", ans[i]);
		else printf(" %d", ans[i]);
	}
	printf("\n");
}
void DFS(int u)
{
	if (u == n)
	{
		print();
		return;
	}
	int size = e[u].size();
	for (int i = 0; i < size; i++)
	{
		int to = e[u][i];
		if (!vis[to] && flag[to])
		{
			vis[to] = true;
			ans.push_back(to);
			DFS(to);
			vis[to] = false;
			ans.pop_back();
		}
	}
}
int main()
{
	int cnt = 1;
	while (scanf("%d", &n) != EOF)
	{
		init();
		find(n);
		printf("CASE %d:\n", cnt++);
		vis[1] = true;
		ans.push_back(1);
		DFS(1);
		printf("There are %d routes from the firestation to streetcorner %d.\n", road_num, n);
	}
	getchar();
	getchar();
}

    

猜你喜欢

转载自blog.csdn.net/amovement/article/details/80555765
今日推荐