#include #include using namespace std; void find_tour(stack & tour, bool** g, unsigned int size, unsigned int u) { for (unsigned int v = 0; v < size; v++) { if (g[u][v]) { g[u][v] = false; find_tour(tour, g, size, v); } } tour.push(u); } unsigned int get_degree(bool** g, unsigned int size, unsigned int u) { unsigned int degree = 0; for (unsigned int v = 0; v < size; v++) { if (g[u][v]) { degree++; } } return degree; } int main () { unsigned int size = 5; bool** g = new bool*[size]; for (unsigned int i = 0; i < size; i++) { g[i] = new bool[size]; for (unsigned int j = 0; j < size; j++) { g[i][j] = true; } } unsigned int u = 0; unsigned int degree = get_degree(g, size, u); while (degree == 0 && u < size) { u++; degree = get_degree(g, size, u); } if (u == size) { cerr << "No tour found" << endl; } else { stack tour; find_tour(tour, g, size, u); cout << "Found tour (length: " << tour.size() - 1 << "):"; while (!tour.empty()) { cout << ' ' << tour.top(); tour.pop(); } cout << endl; } for (unsigned int i = 0; i < size; i++) { delete[] g[i]; } delete[] g; }