From 3e937b3fa345a5f6eb14fcc5edfee076143a6bbc Mon Sep 17 00:00:00 2001 From: Gnieark Date: Wed, 22 Nov 2017 19:05:23 +0100 Subject: [PATCH] Works --- README.md | 4 +- class.towers.php | 28 +++-- resolver.php | 45 ++++---- result.php | 295 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 336 insertions(+), 36 deletions(-) create mode 100644 result.php diff --git a/README.md b/README.md index df9efd4..ec8d890 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # Hanoi Tower recursive resolution -Work in progress +Resolve this problem by + php resolver.php > result.php + diff --git a/class.towers.php b/class.towers.php index 65c9e08..9ebed35 100644 --- a/class.towers.php +++ b/class.towers.php @@ -44,7 +44,7 @@ class Tower{ return $movesAvailables; } - private function get_tower_array(){ + public function get_tower_array(){ return array( $this->tower0, $this->tower1, @@ -76,6 +76,14 @@ class Tower{ } return false; } + public function is_won(){ + $arr = $this->get_tower_array(); + if ((empty($arr[0]) && empty($arr[1])) + ||(empty($arr[0]) && empty($arr[2]))){ + return true; + } + return false; + } } class Move{ @@ -178,7 +186,7 @@ class Steps{ *Memorise the differents towers configurations */ private $steps; - public function __construct($steps){ + public function __construct(){ $this->steps = array(); } @@ -186,7 +194,7 @@ class Steps{ $exp = 0; $total = 0; foreach($column as $elem){ - $total += $elem * pow( $base , $exp ); + $total =$total + ($elem + 1) * pow( $base + 1 , $exp ); $exp++; } return $total; @@ -194,21 +202,19 @@ class Steps{ private function convert_tower_to_int(Tower $tower){ $arrTower = $tower->get_tower_array(); - return - $this::convert_column_to_int($arrTower[0]) + - $this::convert_column_to_int($arrTower[1]) * pow(10,$tower->discsCount) + - $this::convert_column_to_int($arrTower[2]) * pow(10,2 * $tower->discsCount); + $this::convert_column_to_int($arrTower[0],$tower->discsCount) + + $this::convert_column_to_int($arrTower[1],$tower->discsCount) * pow(10,$tower->discsCount) + + $this::convert_column_to_int($arrTower[2],$tower->discsCount) * pow(10,2 * $tower->discsCount); } - + public function add_step(Tower $tower){ - $towerValue = $this::convert_tower_to_int($tower); - if(in_array($towerValue,$this->steps)){ - return false; + return false; }else{ $this->steps[] = $towerValue; + return true; } } } diff --git a/resolver.php b/resolver.php index 6f2388a..ca54286 100644 --- a/resolver.php +++ b/resolver.php @@ -6,36 +6,33 @@ include("class.towers.php"); $tower = new Tower($discCount); -$steps = new Steps(); -/* -echo "1".$tower."\n"; -$move = Move::make(0,2); -$tower = $tower->add_move($move); -echo "2".$tower."\n"; -print_r($tower->list_moves_availables()); -*/ +$steps = new Steps(array()); +$steps->add_step($tower); -resolveHanoi($tower); -function resolveHanoi(Tower $tower, Steps $steps){ +resolveHanoi($tower,$steps); - $steps->add_step($tower); - $availablesMoves = $tower->list_moves_availables(); +function resolveHanoi(Tower $tower, Steps $steps){ + $result = false; - //take only the moves who will generate a new unknowed Tower - $uniquesAvailableMoves = array(); - foreach($availablesMoves as $move){ - $newTower = $tower-> add_move($move); - //to do check if unique ************************************************ + if($tower->is_won()){ + echo "\nSolution founded. Reverse this steps:\n".$tower; + return true; } + $availablesMoves = $tower->list_moves_availables(); + //take only the moves who will generate a new unknowed Tower - - - //don't take moves that will generate an ever used tower configuration - $useful_moves = array(); foreach($availablesMoves as $move){ - + $newTower = $tower->add_move($move); + $newSteps = $steps; + + if($newSteps->add_step($newTower)){ + $r = resolveHanoi($newTower,$newSteps); + if($r){ + $result = true; + echo "\n".$tower; + } + } } - - + return $result; } diff --git a/result.php b/result.php new file mode 100644 index 0000000..3e118eb --- /dev/null +++ b/result.php @@ -0,0 +1,295 @@ + +Solution founded. Reverse this steps: +[[],[],[5,4,3,2,1,0]] +[[0],[],[5,4,3,2,1]] +[[],[0],[5,4,3,2,1]] +[[1],[0],[5,4,3,2]] +[[1],[],[5,4,3,2,0]] +[[],[1],[5,4,3,2,0]] +[[0],[1],[5,4,3,2]] +[[],[1,0],[5,4,3,2]] +[[2],[1,0],[5,4,3]] +[[2,0],[1],[5,4,3]] +[[2,0],[],[5,4,3,1]] +[[2],[0],[5,4,3,1]] +[[2],[],[5,4,3,1,0]] +[[],[2],[5,4,3,1,0]] +[[0],[2],[5,4,3,1]] +[[],[2,0],[5,4,3,1]] +[[1],[2,0],[5,4,3]] +[[1],[2],[5,4,3,0]] +[[],[2,1],[5,4,3,0]] +[[0],[2,1],[5,4,3]] +[[],[2,1,0],[5,4,3]] +[[3],[2,1,0],[5,4]] +[[3,0],[2,1],[5,4]] +[[3,0],[2],[5,4,1]] +[[3],[2,0],[5,4,1]] +[[3],[2],[5,4,1,0]] +[[3,2],[],[5,4,1,0]] +[[3,2,0],[],[5,4,1]] +[[3,2],[0],[5,4,1]] +[[3,2,1],[0],[5,4]] +[[3,2,1,0],[],[5,4]] +[[3,2,1,0],[4],[5]] +[[3,2,1],[4,0],[5]] +[[3,2,1],[4],[5,0]] +[[3,2],[4,1],[5,0]] +[[3,2],[4,1,0],[5]] +[[3,2,0],[4,1],[5]] +[[3,2,0],[4],[5,1]] +[[3,2],[4,0],[5,1]] +[[3,2],[4],[5,1,0]] +[[3],[4,2],[5,1,0]] +[[3,0],[4,2],[5,1]] +Solution founded. Reverse this steps: +[[],[5,4,3,2,1,0],[]] +[[0],[5,4,3,2,1],[]] +[[0],[5,4,3,2],[1]] +[[],[5,4,3,2,0],[1]] +[[],[5,4,3,2],[1,0]] +[[2],[5,4,3],[1,0]] +[[2,0],[5,4,3],[1]] +[[2],[5,4,3,0],[1]] +[[2,1],[5,4,3,0],[]] +[[2,1,0],[5,4,3],[]] +[[2,1,0],[5,4],[3]] +[[2,1],[5,4,0],[3]] +[[2,1],[5,4],[3,0]] +[[2],[5,4,1],[3,0]] +[[2],[5,4,1,0],[3]] +[[2,0],[5,4,1],[3]] +[[2,0],[5,4],[3,1]] +[[2],[5,4,0],[3,1]] +[[2],[5,4],[3,1,0]] +[[],[5,4,2],[3,1,0]] +[[0],[5,4,2],[3,1]] +[[],[5,4,2,0],[3,1]] +[[1],[5,4,2,0],[3]] +[[1,0],[5,4,2],[3]] +[[1,0],[5,4],[3,2]] +[[1],[5,4,0],[3,2]] +[[1],[5,4],[3,2,0]] +[[],[5,4,1],[3,2,0]] +[[],[5,4,1,0],[3,2]] +[[0],[5,4,1],[3,2]] +[[0],[5,4],[3,2,1]] +[[],[5,4,0],[3,2,1]] +[[],[5,4],[3,2,1,0]] +[[4],[5],[3,2,1,0]] +[[4,0],[5],[3,2,1]] +[[4],[5,0],[3,2,1]] +[[4,1],[5,0],[3,2]] +[[4,1],[5],[3,2,0]] +[[4],[5,1],[3,2,0]] +[[4,0],[5,1],[3,2]] +[[4],[5,1,0],[3,2]] +[[4,2],[5,1,0],[3]] +[[4,2,0],[5,1],[3]] +[[4,2,0],[5],[3,1]] +[[4,2],[5,0],[3,1]] +[[4,2],[5],[3,1,0]] +[[4],[5,2],[3,1,0]] +[[4,0],[5,2],[3,1]] +[[4],[5,2,0],[3,1]] +[[4,1],[5,2,0],[3]] +[[4,1],[5,2],[3,0]] +[[4],[5,2,1],[3,0]] +[[4,0],[5,2,1],[3]] +[[4],[5,2,1,0],[3]] +[[4,3],[5,2,1,0],[]] +[[4,3,0],[5,2,1],[]] +[[4,3,0],[5,2],[1]] +[[4,3],[5,2,0],[1]] +[[4,3],[5,2],[1,0]] +[[4,3,2],[5],[1,0]] +[[4,3,2,0],[5],[1]] +[[4,3,2],[5,0],[1]] +[[4,3,2,1],[5,0],[]] +[[4,3,2,1,0],[5],[]] +[[4,3,2,1,0],[],[5]] +[[4,3,2,1],[0],[5]] +[[4,3,2,1],[],[5,0]] +[[4,3,2],[1],[5,0]] +[[4,3,2],[1,0],[5]] +[[4,3,2,0],[1],[5]] +[[4,3,2,0],[],[5,1]] +[[4,3,2],[0],[5,1]] +[[4,3,2],[],[5,1,0]] +[[4,3],[2],[5,1,0]] +[[4,3,0],[2],[5,1]] +[[4,3],[2,0],[5,1]] +[[4,3,1],[2,0],[5]] +[[4,3,1,0],[2],[5]] +[[4,3,1,0],[],[5,2]] +[[4,3,1],[0],[5,2]] +[[4,3,1],[],[5,2,0]] +[[4,3],[1],[5,2,0]] +[[4,3],[1,0],[5,2]] +[[4,3,0],[1],[5,2]] +[[4,3,0],[],[5,2,1]] +[[4,3],[0],[5,2,1]] +[[4,3],[],[5,2,1,0]] +[[4],[3],[5,2,1,0]] +[[4,0],[3],[5,2,1]] +[[4],[3,0],[5,2,1]] +[[4,1],[3,0],[5,2]] +[[4,1,0],[3],[5,2]] +[[4,1,0],[3,2],[5]] +[[4,1],[3,2,0],[5]] +[[4,1],[3,2],[5,0]] +[[4],[3,2,1],[5,0]] +[[4],[3,2,1,0],[5]] +[[4,0],[3,2,1],[5]] +[[4,0],[3,2],[5,1]] +[[4],[3,2,0],[5,1]] +[[4],[3,2],[5,1,0]] +[[4,2],[3],[5,1,0]] +[[4,2,0],[3],[5,1]] +[[4,2],[3,0],[5,1]] +[[4,2,1],[3,0],[5]] +[[4,2,1,0],[3],[5]] +[[4,2,1,0],[],[5,3]] +[[4,2,1],[0],[5,3]] +[[4,2,1],[],[5,3,0]] +[[4,2],[1],[5,3,0]] +[[4,2],[1,0],[5,3]] +[[4,2,0],[1],[5,3]] +[[4,2,0],[],[5,3,1]] +[[4,2],[0],[5,3,1]] +[[4,2],[],[5,3,1,0]] +[[4],[2],[5,3,1,0]] +[[4,0],[2],[5,3,1]] +[[4],[2,0],[5,3,1]] +[[4,1],[2,0],[5,3]] +[[4,1,0],[2],[5,3]] +[[4,1,0],[],[5,3,2]] +[[4,1],[0],[5,3,2]] +[[4,1],[],[5,3,2,0]] +[[4],[1],[5,3,2,0]] +[[4],[1,0],[5,3,2]] +[[4,0],[1],[5,3,2]] +[[4,0],[],[5,3,2,1]] +[[4],[0],[5,3,2,1]] +[[4],[],[5,3,2,1,0]] +[[],[4],[5,3,2,1,0]] +[[0],[4],[5,3,2,1]] +[[],[4,0],[5,3,2,1]] +[[1],[4,0],[5,3,2]] +[[1],[4],[5,3,2,0]] +[[],[4,1],[5,3,2,0]] +[[0],[4,1],[5,3,2]] +[[],[4,1,0],[5,3,2]] +[[2],[4,1,0],[5,3]] +[[2,0],[4,1],[5,3]] +[[2,0],[4],[5,3,1]] +[[2],[4,0],[5,3,1]] +[[2],[4],[5,3,1,0]] +[[],[4,2],[5,3,1,0]] +[[0],[4,2],[5,3,1]] +[[],[4,2,0],[5,3,1]] +[[1],[4,2,0],[5,3]] +[[1],[4,2],[5,3,0]] +[[],[4,2,1],[5,3,0]] +[[0],[4,2,1],[5,3]] +[[],[4,2,1,0],[5,3]] +[[3],[4,2,1,0],[5]] +[[3,0],[4,2,1],[5]] +[[3,0],[4,2],[5,1]] +[[3],[4,2,0],[5,1]] +[[3,1],[4,2,0],[5]] +[[3,1,0],[4,2],[5]] +[[3,1,0],[4],[5,2]] +[[3,1],[4,0],[5,2]] +[[3,1],[4],[5,2,0]] +[[3],[4,1],[5,2,0]] +[[3],[4,1,0],[5,2]] +[[3,0],[4,1],[5,2]] +[[3,0],[4],[5,2,1]] +[[3],[4,0],[5,2,1]] +[[3],[4],[5,2,1,0]] +[[],[4,3],[5,2,1,0]] +[[0],[4,3],[5,2,1]] +[[],[4,3,0],[5,2,1]] +[[1],[4,3,0],[5,2]] +[[1],[4,3],[5,2,0]] +[[],[4,3,1],[5,2,0]] +[[0],[4,3,1],[5,2]] +[[],[4,3,1,0],[5,2]] +[[2],[4,3,1,0],[5]] +[[2,0],[4,3,1],[5]] +[[2,0],[4,3],[5,1]] +[[2],[4,3,0],[5,1]] +[[2],[4,3],[5,1,0]] +[[],[4,3,2],[5,1,0]] +[[0],[4,3,2],[5,1]] +[[],[4,3,2,0],[5,1]] +[[1],[4,3,2,0],[5]] +[[1],[4,3,2],[5,0]] +[[],[4,3,2,1],[5,0]] +[[0],[4,3,2,1],[5]] +[[],[4,3,2,1,0],[5]] +[[5],[4,3,2,1,0],[]] +[[5,0],[4,3,2,1],[]] +[[5,0],[4,3,2],[1]] +[[5],[4,3,2,0],[1]] +[[5],[4,3,2],[1,0]] +[[5,2],[4,3],[1,0]] +[[5,2,0],[4,3],[1]] +[[5,2],[4,3,0],[1]] +[[5,2,1],[4,3,0],[]] +[[5,2,1,0],[4,3],[]] +[[5,2,1,0],[4],[3]] +[[5,2,1],[4,0],[3]] +[[5,2,1],[4],[3,0]] +[[5,2],[4,1],[3,0]] +[[5,2],[4,1,0],[3]] +[[5,2,0],[4,1],[3]] +[[5,2,0],[4],[3,1]] +[[5,2],[4,0],[3,1]] +[[5,2],[4],[3,1,0]] +[[5],[4,2],[3,1,0]] +[[5,0],[4,2],[3,1]] +[[5],[4,2,0],[3,1]] +[[5,1],[4,2,0],[3]] +[[5,1,0],[4,2],[3]] +[[5,1,0],[4],[3,2]] +[[5,1],[4,0],[3,2]] +[[5,1],[4],[3,2,0]] +[[5],[4,1],[3,2,0]] +[[5],[4,1,0],[3,2]] +[[5,0],[4,1],[3,2]] +[[5,0],[4],[3,2,1]] +[[5],[4,0],[3,2,1]] +[[5],[4],[3,2,1,0]] +[[5,4],[],[3,2,1,0]] +[[5,4,0],[],[3,2,1]] +[[5,4],[0],[3,2,1]] +[[5,4,1],[0],[3,2]] +[[5,4,1],[],[3,2,0]] +[[5,4],[1],[3,2,0]] +[[5,4,0],[1],[3,2]] +[[5,4],[1,0],[3,2]] +[[5,4,2],[1,0],[3]] +[[5,4,2,0],[1],[3]] +[[5,4,2,0],[],[3,1]] +[[5,4,2],[0],[3,1]] +[[5,4,2],[],[3,1,0]] +[[5,4],[2],[3,1,0]] +[[5,4,0],[2],[3,1]] +[[5,4],[2,0],[3,1]] +[[5,4,1],[2,0],[3]] +[[5,4,1],[2],[3,0]] +[[5,4],[2,1],[3,0]] +[[5,4,0],[2,1],[3]] +[[5,4],[2,1,0],[3]] +[[5,4,3],[2,1,0],[]] +[[5,4,3,0],[2,1],[]] +[[5,4,3,0],[2],[1]] +[[5,4,3],[2,0],[1]] +[[5,4,3],[2],[1,0]] +[[5,4,3,2],[],[1,0]] +[[5,4,3,2,0],[],[1]] +[[5,4,3,2],[0],[1]] +[[5,4,3,2,1],[0],[]] +[[5,4,3,2,1,0],[],[]] \ No newline at end of file