#include #include #include #include #include #include #include using namespace std; string start = "start"; string finish = "end"; string lowercase = "qwertyuiopasdfghjklzxcvbnm"; int find_all_paths(unordered_map> graph, vector current_path); int main(int argc, char* argv[]) { unordered_map> graph; string line; while(getline(cin, line)) { string::size_type n = line.find("-",0); string node1 = line.substr(0,n); string node2 = line.substr(n+1); if (!graph.contains(node1)) { graph.emplace(node1, set()); } if (!graph.contains(node2)) { graph.emplace(node2, set()); } graph[node1].insert(node2); graph[node2].insert(node1); } vector beginning_path; beginning_path.push_back(start); cout << find_all_paths(graph, beginning_path) << endl; } int find_all_paths(unordered_map> graph, vector current_path) { int found_paths = 0; set connected_nodes = graph[current_path.back()]; for (set::iterator it = connected_nodes.begin(); it != connected_nodes.end(); ++it) { string current_node = *it; if (current_node.compare(finish) == 0) { ++found_paths; } else if (string::npos == current_node.find_first_of(lowercase) || current_path.end() == std::find(current_path.begin(), current_path.end(), current_node)) { //Big cave or small cave not in path vector next_path(current_path); next_path.push_back(current_node); found_paths += find_all_paths(graph, next_path); } } return found_paths; }