This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve12.cc
98 lines (86 loc) · 2.23 KB
/
solve12.cc
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
#include <cctype>
#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#include <set>
std::vector<std::string> caves;
std::vector<std::set<size_t> > connections;
size_t find_cave(std::string name)
{
for(size_t i = 0; i < caves.size(); ++i) {
if (caves[i]==name) return i;
}
return caves.size();
}
size_t add_cave(std::string name)
{
size_t i = find_cave(name);
if (i < caves.size()) {
return i;
}
else {
caves.push_back(name);
connections.emplace_back();
return caves.size()-1;
}
}
void rd()
{
std::string s;
for(;;) {
std::cin >> s;
if (std::cin.eof()) break;
size_t dash = s.find_first_of('-');
size_t i = add_cave(s.substr(0, dash));
size_t j = add_cave(s.substr(dash+1, s.npos));
connections[i].insert(j);
connections[j].insert(i);
std::cout << caves[i] << '-' << caves[j] << std::endl;
}
}
unsigned npath(size_t start, size_t finish, std::set<size_t> &visited, bool task2_allowed)
{
unsigned n = 0;
bool task2_revisit = visited.find(start) != visited.end();
// For silly premature-optimization reasons, visited is not stacked...
if (std::islower(caves[start][0]) && !task2_revisit)
visited.insert(start);
for (std::set<size_t>::const_iterator ix = connections[start].begin();
ix != connections[start].end(); ++ix) {
size_t via = *ix;
if (caves[via]=="start") continue;
if (via == finish) {
++n;
continue;
}
if (std::islower(caves[via][0]) && visited.find(via) != visited.end()) {
if (!task2_allowed) continue;
n += npath(via, finish, visited, false);
}
else {
n += npath(via, finish, visited, task2_allowed);
}
}
// ...so we must restore it on exit
if (std::islower(caves[start][0]) && !task2_revisit)
visited.erase(start);
return n;
}
void task1()
{
size_t start = find_cave("start");
size_t finish = find_cave("end");
assert(start < caves.size() && finish < caves.size());
std::set<size_t> visited;
unsigned n = npath(start, finish, visited, false);
std::cout << "Task 1: " << n << std::endl;
assert(visited.empty());
n = npath(start, finish, visited, true);
std::cout << "Task 2: " << n << std::endl;
}
int main() {
rd();
task1();
return 0;
}