21
Lab 14

Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Lab 14

Page 2: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Tower of Hanoi

It is a puzzle where the player have to move the stack of dicswith different sizes from one rod onto another.

Page 3: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Array vs Vector

• When we need to keep track of similar data, we usually create an array.

• We have to specify the size of the array.

• What if we have no idea how large the dataset is indeed?

• We can create an array with a size which we think is big enough to hold all the data. What if the array is not big enough?

What if we actually do not need that big?

Page 4: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Dynamic array

• There are two types of dynamic array structure in C++.

• array pointerExample:

int *q = new int[k];Note: remember to delete q at the end of function.

• vectorExample:

vector<string> student_names;vector<double> prices;

Page 5: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Intro to vector

Vector is a little bit more powerful than arrays and may handle storage of data with unknown sizes.

Vector is a data structure that grows as you add data to it. Internally, vectors use a dynamically allocated array to store elements. This list grows as data adds to it and shrinks as data are removed from it.

Compared with deques and lists, vectors are very efficient accessing its elements just like arrays.

http://www.cplusplus.com/reference/vector/vector/

Page 6: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Vector in STL(Standard Template Library)

• To use the Vector class, first need to include the C++ Vector library to your code.

#include <vector>

Page 7: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

How to use it

vector<data_type> variable_name;

In the < >, you put in the data type which can be any data type, even the data type defined by users.

Example: vector<string> student_names;

Page 8: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Functions of vector

Push back:

To add data onto the vector, vector class has the member function push_back;

Syntax: vector_variable_name.push_back(data);

Example: student_names.push_back(“Michael”);

Page 9: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Functions of vector

To remove data from a vector, we use pop_back() to remove the last data(if exists) in the vector.

Syntax: vector_variable_name.pop_back();

Example: student_names.pop_back();//remove the last one in vector.

Page 10: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Functions of vector

We may want to know the current size of the vector.

Syntax: vector_variable_name.size();

Example: student_names.size();//length of the vector

Page 11: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Functions of vector

• We also can access the vector just like an array using the [ ] operator.

Syntax: vector_variable_name[index]

• Or use member function at

Syntax: vector_variable_name.at[index]

• Quick access to the first and last elements:

Syntax: vector_variable_name.front()

vector_variable_name.back();

Page 12: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Hanoi Tower

You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

1. We can only move one ring at a time, and

2. We may never put a larger numbered ring on top of a smaller numbered one.

There are always 3 towers. Your program will prompt the user for the number of rings.

Page 13: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Hanoi Tower

To move 5 disc from start rod to destination tower:

- To place the largest disc to the bottom of destination tower, you have to move the top 4 to the extra tower.

- After placing the largest disc, then place the top 4 from the extra tower onto the destination tower.

- To move the top 4 to its destination, you have to move the top 3 to the extra tower, place the 4th one to its destination and move the top 3 onto it and so on.

Page 14: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

How many steps do you need?

• We moved the largest disc once from source tower to destination, moved the second largest twice from source to the extra tower and from the extra to the destination.

• For each time, you move (i+1)-th disc, you have to move twice for i-thdisc.

Example: 5 disc

1(disc 5) + 2(disc 4) + 4(disc 3) + 8(disc 2) + 16(disc 1)

Total: 2^5 – 1 = 31.

Page 15: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Solving recursively

• Base case:

When there is only 1 disc to move, simply move the disc to the destination.

• Recursive step:

move from current tower to extra tower.

then move from extra tower to the destination tower.

Page 16: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Recursive Hanoi Tower Code

int main()

{

int n,a;

cout<<"enter a number"<<endl;

cin>>n;

a=0;///count steps

tower(n,'A','B','C',a);

cout<<a<<endl;

return 0;

}

void tower(int n, char x,char y, char z,int &a){if(n==1){

cout<<"move the top Piece of "<<x<<" to the"<<y<<endl;a=a+1;return;

}//what comes here, you need to figure out}

Page 17: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Non-recursive Hanoi Tower Algorithm

• Definition: A ring is "available" if it is on the top of one of the towers.

• Definition: The "candidate" is the smallest available ring that has not been moved on the

• most recent move. The first candidate is ring 1.

• The Algorithm: Find the candidate.

Move the candidate (right or left, depending if the number of rings is odd or even) to the closest tower on which it can be placed. Move "around the circle" if necessary.

If not done, go back to step 1.

Page 18: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Non-recursive Hanoi Tower(This works for odd n)

Page 19: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Sample Outputs of Hanoi Tower:

In addition, your program should take 2^n-1 moves for any n.

Page 20: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

Redo Using Recursion

Using the recursive eight queens (see handout section on web site) as a model, redo

- the cross problem using recursion

- the stable marriage problem

Page 21: Lab 14kbian/CS211/Lab14/#Lab 14.pdfHanoi Tower You are given three towers a, b, and c. Start with n rings on tower a and we need to transfer them to tower b subject to the following:

move function

void move(int *q, int i){

if(i==8){ //reaching column 8 means we found one solution

print(q); //therefore, print it.

return; //terminate the function call.

}

for(int j=0;j<8;j++){ //for each row

q[i] = j; //place the queen

if(ok(q,i)) //check if the row position works

move(q,i+1); //recursively try the following columns

}

} //recursive 8 queens

Base case

Recursive step