首页 > 解决方案 > Semi tower of hanoi

问题描述

void semiHanoi(int n, char source, char destination, char temp) {
    if (n == 2) {//base case
        cout << "Move from " << source << " to " << destination << endl;
        cout << "Move from " << temp << " to " << destination << endl;
    }
    else {
        semiHanoi((n - 1), source, destination, temp);//to make moves
        semiHanoi(2, destination, temp, source);
        //semiHanoi(2 * (n - 1), destination, source, temp);

    }
}

I am tried a lot but could not figure out what and how the recursive calls should go! Please Help

This is the question: Semi-Hanoi is a problem very similar to Hanoi problem. The only difference is that your input in semi-Hanoi must be an even number of disks like 2n and they are set as shown in the following figure-a. The peg A has all the even numbered disks such as 2,4,6, … and the peg B has the rest (all the odd numbered disks such as 1,3,5,...). the rules of movement in semi-Hanoi is the same as Hanoi’s rules: • only one disk can be moved at a time • a bigger disk cannot be placed on top of a smaller one The goal is to move all the disks on the peg C in order as shown in figure-b here is the image how disks initially looks like

enter image description here

标签: c++c++14

解决方案


推荐阅读