#include #include #include // a slightly prettier definition of town than we had: struct town { string name; vector roads; town(string n) { name=n; } void add_road_to(town * B) { roads.push_back(B); } int number_of_roads() { return roads.size(); } town * destination_of_road(int i) { return roads[i]; } }; // this method only works when the map is really a tree: roads // are only one-way, and there is never more than one path // between any two places. bool find_path(town * from, town * dest, vector & path) { path.push_back(from); if (from==dest) return true; for (int i=0; inumber_of_roads(); i+=1) { bool found = find_path(from->destination_of_road(i), dest, path); if (found) return true; } path.pop_back(); return false; } // maps can be built by hand if a programmer is too lazy to // read the structure properly from a file. void main() { town * A = new town("A"); town * B = new town("B"); town * C = new town("C"); town * D = new town("D"); town * E = new town("E"); town * F = new town("F"); town * G = new town("G"); town * H = new town("H"); town * I = new town("I"); town * J = new town("J"); town * K = new town("K"); A->add_road_to(H); A->add_road_to(D); A->add_road_to(J); D->add_road_to(F); J->add_road_to(E); E->add_road_to(K); E->add_road_to(C); K->add_road_to(G); K->add_road_to(B); B->add_road_to(I); vector path_taken; bool ok = find_path(A, B, path_taken); if (!ok) cout << "No path exists\n"; for (int i=0; iname << "\n"; }