#include #include
using namespace std;
// move the top block from 'from' to 'to' void moveBlock(vector& from, vector& to) { to.push_back(from.back()); from.pop_back(); } // Function to print transition state so that user can see what he/she is doing void print_state(vector& T1, vector& T2, vector& T3) { cout << "Transition state:" << endl; cout << "T1: "; for (char block : T1) { cout << block << " "; } cout << endl; cout << "T2: "; for (char block : T2) { cout << block << " "; } cout << endl; cout << "T3: "; for (char block : T3) { cout << block << " "; } cout << endl; }
int main() { // initialize T1 with blocks A, B, C, D vector T1 = { 'A', 'B', 'C', 'D' }; vector T2, T3;
cout << "Initial state:" << endl; cout << "T1: "; for (char block : T1) { cout << block << " "; } cout << endl; cout << "T2: "; for (char block : T2) { cout << block << " "; } cout << endl; cout << "T3: "; for (char block : T3) { cout << block << " "; } cout << endl; vector Tf = { 'A','B','C','D' }; while (T3 != Tf) { int ss, dd; cout << "Enter Source : "; cin >> ss; cout << "Enter Destination : "; cin >> dd; if (ss == dd) { print_state(T1, T2, T3); continue; } vector sl = ss == 1 ? T1 : (ss == 2 ? T2 : T3); vector dl = dd == 1 ? T1 : (dd == 2 ? T2 : T3); // move block from source to destination if (sl.size() == 0 || (dl.size() && (sl.back() < dl.back()))) { cout << sl.size() << dl.size() << endl; cout << " INVALID MOVE " << endl; } else { if (ss == 1) { if (dd == 2) moveBlock(T1, T2); else moveBlock(T1, T3); } else if (ss == 2) { if (dd == 1) moveBlock(T2, T1); else moveBlock(T2, T3); } else { if (dd == 1) moveBlock(T3, T1); else moveBlock(T3, T2); } print_state(T1, T2, T3); } } cout << "Final state:" << endl; cout << "T1: "; for (char block : T1) { cout << block << " "; } cout << endl; cout << "T2: "; for (char block : T2) { cout << block << " "; } cout << endl; cout << "T3: "; for (char block : T3) { cout << block << " "; } cout << endl; return 0; }
output?