-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccc01s3.py
81 lines (67 loc) · 1.41 KB
/
ccc01s3.py
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
#Input
graph1 = {'A': [],
'B': [],
'C': [],
'D': [],
'E': [],
'F': [],
'G': [],
'H': [],
'I': [],
'J': [],
'K': [],
'M': [],
'O': [],
'P': [],
'Q': [],
'R': [],
'S': [],
'T': [],
'U': [],
'V': [],
'W': [],
'X': [],
'Y': [],
'Z': []
}
reverse = []
isDone = False
while isDone == False:
temp = input()
temp1 = temp[::-1]
if temp == "**":
isDone = True
else:
reverse.append(temp)
for key in graph1:
if temp[0] == key:
graph1[key] = graph1[key] + list(temp[1])
if temp1[0] == key:
graph1[key] = graph1[key] + list(temp1[1])
def find_paths(current, end, visited):
k = []
if current == end:
return [visited]
for x in graph1[current]:
if f'{current}{x}' not in visited and f'{x}{current}' not in visited:
k += find_paths(x, end, visited+[f'{current}{x}'])
return k
cum = {}
for x in reverse:
cum[x] = 0
cum[x[::-1]] = 0
paths = find_paths('A', 'B', [])
for x in paths:
for y in x:
cum[y] += 1
c = len(paths)
answer = []
for x in cum:
if cum[x] == c:
answer.append(x)
for x in answer:
if x in reverse:
print(x)
else:
print(x[::-1])
print(f'There are {len(answer)} disconnecting roads.')