Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Lab 14
Tower of Hanoi
It is a puzzle where the player have to move the stack of dicswith different sizes from one rod onto another.
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?
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;
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/
Vector in STL(Standard Template Library)
• To use the Vector class, first need to include the C++ Vector library to your code.
#include <vector>
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;
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”);
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.
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
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();
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.
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.
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.
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.
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}
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.
Non-recursive Hanoi Tower(This works for odd n)
Sample Outputs of Hanoi Tower:
In addition, your program should take 2^n-1 moves for any n.
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
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