-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-208.cpp
102 lines (94 loc) · 1.77 KB
/
UVA-208.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <string.h>
#define MAX 21
int terminal;
int map[MAX][MAX];
int tempLine[MAX];
int totalLine[MAX][10000];
int totalNum = 0;
int findStatus[MAX];
void copyLine(int *arr1, int *arr2)
{
for (int i = 0; arr1[i]; ++i)
{
arr2[i] = arr1[i];
}
}
// 初始化
void init()
{
memset(map, 0, sizeof(map));
memset(tempLine, 0, sizeof(tempLine));
memset(totalLine, 0, sizeof(totalLine));
memset(findStatus, 0, sizeof(findStatus));
totalNum = 0;
}
void dfs(int i, int num)
{
tempLine[num] = i;
findStatus[i] = 1;
++num;
if (i == terminal)
{
copyLine(tempLine, totalLine[totalNum]);
++totalNum;
findStatus[i] = 0;
return;
}
int x, y;
for (x = 1; x < MAX; ++x)
{
if (map[i][x] && !findStatus[x])
{
dfs(x, num);
tempLine[num] = 0;
}
}
findStatus[i] = 0;
}
void findDfs(int i)
{
findStatus[i] = 1;
for (int x = 1; x < MAX; ++x)
{
if (map[i][x] && !findStatus[x])
findDfs(x);
}
}
int main()
{
int x, y, i = 0;
while (scanf("%d", &terminal) == 1)
{
// 初始化
init();
++i;
while (scanf("%d %d", &x, &y) == 2 && x != 0)
{
map[x][y] = 1;
map[y][x] = 1;
}
printf("CASE %d:\n", i);
// 先求联通图
findDfs(1);
if (!findStatus[terminal])
{
printf("There are 0 routes from the firestation to streetcorner %d.\n", terminal);
continue;
}
memset(findStatus, 0, sizeof(findStatus));
dfs(1, 0);
for (x = 0; x < totalNum; ++x)
{
for (y = 0; totalLine[x][y]; ++y)
{
if (y != 0)
putchar(' ');
printf("%d", totalLine[x][y]);
}
putchar('\n');
}
printf("There are %d routes from the firestation to streetcorner %d.\n", totalNum, terminal);
}
return 0;
}