// prob.cpp - C++ code for solving problem 5 #include #include // Uncomment the following lines if using APCS classes #include "apvector.h" #include "apmatrix.h" #include "apstack.h" // #include "apqueue.h" // #include "apstring.cpp" bool Ham(int num, apmatrix &adj, apstack &seats, apvector &marked) { int i, j = seats.top(); if (num == seats.length() && adj[j][0]) { return true; } for (i=1; i> num; apmatrix adj(num,num,true); // who's allowed to be adjacent for (i=0; i> x; if (x == -1) break; cin >> y; adj[x-1][y-1] = false; // can't sit next to each other adj[y-1][x-1] = false; } // find seating apstack seats; // seats for diplomats apvector marked(num,false); // which nodes visited seats.push(0); if (Ham(num, adj, seats, marked)) { while (!seats.isEmpty()) { seats.pop(x); cout << x+1 << " "; } cout << "\n"; } else { cout << "0" << "\n"; } return 0; }