Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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(); }

#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?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

Explain the dual challenges of adverse selection and moral hazard.

Answered: 1 week ago