 What's up guys? Mike the Coder here. Today we are going to talk about Tower of Hanoi. Pretty well known common puzzle in math and in computer science and the next problem of C.S.E.S. So what is Tower of Hanoi? Basically it is a math puzzle that can consist of three towers. So there's three towers. One of the towers has these rings. Each of these rings there's the small, the medium, and then the large. It's kind of like one of those child toys, right? When you see at Tories RS you have one of these rings. It has like small, medium, and large. Our job is to move all the discs to another tower, but there are some rules you have to follow. So we only could move one disc at a time, right? I can't just take all two, three, and then just put them onto the tower. It's cheating, right? Only one disc at a time. The second rule is that only the top disc can be removed. So I only could remove the one on the top most disc. So in our first picture you see right here, it's on the top disc, the smallest disc that you can move. If it was like the middle disc, then that's only one that you can move. So only the top disc can be removed, okay? The third rule is that you cannot have a large disc over a small disc. So what that means is that like in our picture here, we cannot have the green on top of the blue, right? Because the blue is the smallest, right? We cannot have the green because the green is a large disc and it cannot sit over the small disc, right? This green disc has to be always on the bottom and the brown disc has to be in the middle. So yeah, that's essentially what it has to do. Okay, so how do you solve this problem? So to solve this problem, you have to move the discs around individually and then place them back together and then move it back to your destination. Okay, let's start with one disc. So if we just have one disc, you could easily just move the source to the destination because like that's all you have to do. But if you have two discs, right, you have to move the smaller disc to the auxiliary peg, which is like the third tower that's on our destination. So we're just going to move the smallest disc to the third tower, then move the larger discs to the bottom, and then finally move the smaller disc from the third tower back to our destination. Okay, so it looks like this drawing right here. So now if you want to do the nth disc, the recursive algorithm is like this. So you're going to move n minus one discs from the source to the auxiliary. So that means that we're going to move all the discs that are not the largest to the third peg of the auxiliary, we're going to move the nth disc from the source to destination. So we're going to move the largest to our destination. And then we're just going to move the n minus one discs back to from auxiliary to destination. So yeah, so here is like the procedure here, right? So if the disc is as equal to one, our size of our discs is one, all we have to do is just move the destination, the disc from the source to destination. Okay, otherwise, we're just going to decrement the number of discs that we're moving every time. And what we're going to do is we're going to move the source to the auxiliary, then move one disc, one disc from the source to destination. And then now afterwards, we're just going to move the auxiliary to the destination. And that's our last call. The third variable, we just put the third variable at where it is. So yeah, that's essentially the code. And I'll show you guys the code now. The guys, the code looks like this. Yeah, so read an n, which is number of discs. I have a vector pair x represents like the source and destination because they want you to print the source and destination. That just called solve with my pairs, my pair of answers of x. And then I pass in the number of discs. And then here are just for from to an auxiliary. So I have one, three, and then two, representing from to an auxiliary. So these are the different pegs. And yeah, and here we have solved, if the size of n is equal to one, we're just going to push back onto to our source to destination, into our answer array. And then we return. Otherwise, we're going to recursively call n minus one, passing in our answer array, and just passing the source to the auxiliary. So this is passing in n minus one discs from source to auxiliary, then we're going to push back, which is going to be we're going to add the front to two, which is like source to front source to destination, adding one disc from source to destination, and we're adding this into our answer array, because I want us to print out the answers. And then here we recursively call on the n minus one discs, and we pass from auxiliary to the source. Yeah, yeah, auxiliary to source destiny, auxiliary destination, auxiliary destination, okay, so that's basically just piling on the rest of the rest of the discs. And that's basically the gist of the code. And then in the end, we just loop through our print out the sides of our answer array. And then we just print out for each of the answer, each of the values in our answer array, we just print it out, print out the first, the from the source and destination of each value. And that's it, return zero. Hope you guys enjoyed this video, like, come subscribe out, you guys are the piece