Tower of Hanoi

def towerOfHanoi(N , source, destination, auxiliary):
	if N==1:
		print("Move disk 1 from source",source,"to destination",destination)
		return
	towerOfHanoi(N-1, source, auxiliary, destination)
	print("Move disk",N,"from source",source,"to destination",destination)
	towerOfHanoi(N-1, auxiliary, destination, source)
		
# Driver code
N = 3
towerOfHanoi(N,'A','B','C')
# A, C, B are the name of rods
Source: favtutor.com

tower of hanoi

// C++ recursive function to
// solve tower of hanoi puzzle
#include <bits/stdc++.h>
using namespace std;
 
void towerOfHanoi(int n, char from_rod, char to_rod,
                  char aux_rod)
{
    if (n == 0) {
        return;
    }
    towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
    cout << "Move disk " << n << " from rod " << from_rod
         << " to rod " << to_rod << endl;
    towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
 
// Driver code
int main()
{
    int N = 3;
 
    // A, B and C are names of rods
    towerOfHanoi(N, 'A', 'C', 'B');
    return 0;
}
 
// This is code is contributed by rathbhupendra

tower of hanoi

/// find total number of steps 
int towerOfHanoi(int n) {
  /// pow(2,n)-1
  if (n == 0) return 0;
  
  return towerOfHanoi(n - 1) + 1 + towerOfHanoi(n - 1);
}

tower of hanoi

# Recursive Python function to solve tower of hanoi
 
def TowerOfHanoi(n , from_rod, to_rod, aux_rod):
    if n == 0:
        return
    TowerOfHanoi(n-1, from_rod, aux_rod, to_rod)
    print("Move disk",n,"from rod",from_rod,"to rod",to_rod)
    TowerOfHanoi(n-1, aux_rod, to_rod, from_rod)
         
# Driver code
n = 4
TowerOfHanoi(n, 'A', 'C', 'B')
# A, C, B are the name of rods
 
# Contributed By Harshit Agrawal

tower of hanoi

#include <iostream>
#include <math.h>
struct Node{    
    int diam;
    Node* next;}; 
class Stack
{
private:
    Node* top;
public:
    Stack();
    bool push(int);
    int pop();
    int peek();
    int count();
    void print();
    bool isEmpty();
}; 
Stack::Stack(){
    top = NULL;
} 
bool Stack::push(int diam){
    bool status = false;    
    Node* temp = new Node();
    temp->diam = diam;
    temp->next = top;
    top = temp;
    if(top->diam == diam){
        status = true;
    }
    return status;
}
bool Stack::isEmpty(){
    bool status = false;
    if(top == NULL){
        status = true;
    }
    return status;
} 
int Stack::pop(){
    int value = INT_MIN;
    if(top){
        Node* temp = top;
        value = temp->diam;
        top = temp->next;
        delete(temp);
    }
    return value;
} 
int Stack::count(){
    int count = 0;
    Node* temp = top;
    while(temp){
        count++;
        temp = temp->next;
    }
    return count;
} 
int Stack::peek(){
    int value = INT_MIN;
    if(top){
        value = top->diam;
    }
    return value;
} 
void Stack::print(){
    Node* temp = top;
    while(temp){
        std::cout << temp->diam << ' ';
        temp = temp->next;
    }
} 
void moveDisk(Stack *source, Stack *destination){
    int tower1Top = source->pop();
    int tower2Top = destination->pop();
        if(tower1Top == INT_MIN){
        source->push(tower2Top);
    }
    else if(tower2Top ==INT_MIN){
        destination->push(tower1Top);
    }
    else if(tower1Top > tower2Top){
        source->push(tower1Top);
        source->push(tower2Top);
    }
    else{
        destination->push(tower2Top);
        destination->push(tower1Top);
    }
}
int main() {
    Stack towerOne;
    Stack towerTwo;
    Stack towerThree;
    int towerSize = 10;
    if(towerOne.isEmpty() && towerTwo.isEmpty() && towerThree.isEmpty()){
        std::cout << "Three empty stacks created" << std::endl;
    }
    std::cout << '\n';
    std::cout << "Putting " << towerSize << " plates of subsequently smaller diameter on towerOne" << std::endl;
    for(int i=towerSize; i>0; i--){
        towerOne.push(i);
    }
    std::cout << '\n';
    
    std::cout << "Printing Tower One: " << std::endl;
    towerOne.print();
    std::cout << '\n';
    std::cout << '\n';
    std::cout << "Testing pop() on Tower One (removing top value):" << std::endl;
    std::cout << towerOne.pop() << std::endl;
    std::cout << "Tower Size is now " << towerSize-1 << std::endl;
    std::cout << '\n';
    std::cout << "Printing updated list:" << std::endl;
    towerOne.print();
    std::cout << '\n';
    std::cout << '\n';
    std::cout << "Testing peek()" << std::endl;
    std::cout << towerOne.peek() << std::endl;
    std::cout << '\n';
    std::cout << "Testing count()" << std::endl;
    std::cout << towerOne.count() << std::endl;
    std::cout << '\n';
    
    int total_moves = pow(2, towerOne.count()) - 1;
    std::cout << "Running Towers of Hanoi algorithm" << std::endl;
    if(towerOne.count()%2 != 0){
        for(int i=1; i<=total_moves; i++){
            if(i%3==1){
                moveDisk(&towerOne, &towerThree);
            }
            else if(i%3==2){
                moveDisk(&towerOne, &towerTwo);
            }
            else if(i%3==0){
                moveDisk(&towerTwo, &towerThree);
            }
        }
    }
    else if(towerOne.count()%2==0){
        for(int i=1; i<=total_moves; i++){
            if(i%3==1){
                moveDisk(&towerOne, &towerTwo);
            }
            else if(i%3==2){
                moveDisk(&towerOne, &towerThree);
            }
            else if(i%3==0){
                moveDisk(&towerTwo, &towerThree);
            }
        }
    }
    std::cout << "Checking Tower Three count is equal to " << towerSize-1 << std::endl;
    std::cout << "Tower Three Count: " << towerThree.count() << std::endl;
    std::cout << "\n";
    std::cout << "Checking Tower Three elements are in ascending order " << std::endl;
    towerThree.print();
    std::cout << "\n";
 }

C++相关代码片段

how to kill

hello world cpp

cpp hello world

when kotlin

how to write hello world c++

fenwick tree

main function

python loop

is palindrom

subsequence

insertion sort

react cookie

data structure

Stack Modified

increment integer

496. Next Greater Element I.cpp

c++ freecodecamp course 10 hours youtube

simple interest rate

deliberation meaning

fingerprint

c++ system() from variable

operator using

unambiguous

Stream Overloading

quick_sort

hash table

graph colouring

the question for me

fname from FString

how togreper

is plaindrome

logisch oder

robot framework for loop example

spyder enviroment

pallindrome string

ue4 c++ string from fvector

interactive problem

two sum solution

interpolation search

Required Length

new expression

Targon lol

xor linked list

stack implementation

typescript

loop through array

loop array

how to point to next array using pointer c++

file exist

floating point exception

array di struct

castin in C++

varint index

how to make a instagram report bot python

windows servis from console app

add integers

listas enlazadas/ linked lists

niet werkend

bubble sort

triangle angle sum

divisor summation

rotateArray

destiny child

Populating Next Right Pointers in Each Node

cosnt cast

bucket sort

double plus overload

Z-function

binary search

permutation

linked list

Implicit conversion casting

square root

public method

tower of hanoi

selection sort

dangling pointer

hello world programming

statements

volumeof a sphere