Created
December 13, 2017 19:59
-
-
Save AhmedMourad0/fad4395fbc461d6120bceb6fe5d2022f to your computer and use it in GitHub Desktop.
For college
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/.idea/TowerOfHanoi.iml" filepath="$PROJECT_DIR$/.idea/TowerOfHanoi.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module classpath="CMake" type="CPP_MODULE" version="4" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.8) | |
project(TowerOfHanoi) | |
set(CMAKE_CXX_STANDARD 17) | |
set(SOURCE_FILES main.cpp) | |
add_executable(TowerOfHanoi ${SOURCE_FILES}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> // cout, endl | |
void moveDisks(int, int, int, int); | |
using namespace std; | |
int main() { | |
// should take an eternity to finish | |
moveDisks(1000, 1, 3, 2); | |
return 0; | |
} | |
/** | |
* move the disks to the goal peg. | |
* @param n the number of disks. | |
* @param initialPeg the peg where disks already are. | |
* @param goalPeg the peg to move our disks to. | |
* @param intermediatePeg the peg to use as a holder for our disks. | |
*/ | |
void moveDisks(int n, int initialPeg, int goalPeg, int intermediatePeg) { | |
if (n > 0) { | |
// we move the top n - 1 disks to our intermediate peg | |
moveDisks(n - 1, initialPeg, intermediatePeg, goalPeg); | |
// we move the remaining disk to our goal peg | |
cout << initialPeg << " -> " << goalPeg << endl; | |
// we move our n - 1 disks existing at our intermediate peg to our | |
// goal peg using our initial peg as an intermediate peg | |
moveDisks(n - 1, intermediatePeg, goalPeg, initialPeg); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment