Works
This commit is contained in:
parent
1e08e2a86b
commit
3e937b3fa3
|
@ -1,3 +1,5 @@
|
|||
# Hanoi Tower recursive resolution
|
||||
|
||||
Work in progress
|
||||
Resolve this problem by
|
||||
php resolver.php > result.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
47
resolver.php
47
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);
|
||||
|
||||
resolveHanoi($tower,$steps);
|
||||
|
||||
function resolveHanoi(Tower $tower, Steps $steps){
|
||||
|
||||
$steps->add_step($tower);
|
||||
$result = false;
|
||||
|
||||
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
|
||||
$uniquesAvailableMoves = array();
|
||||
|
||||
foreach($availablesMoves as $move){
|
||||
$newTower = $tower-> add_move($move);
|
||||
//to do check if unique ************************************************
|
||||
$newTower = $tower->add_move($move);
|
||||
$newSteps = $steps;
|
||||
|
||||
if($newSteps->add_step($newTower)){
|
||||
$r = resolveHanoi($newTower,$newSteps);
|
||||
if($r){
|
||||
$result = true;
|
||||
echo "\n".$tower;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//don't take moves that will generate an ever used tower configuration
|
||||
$useful_moves = array();
|
||||
foreach($availablesMoves as $move){
|
||||
|
||||
}
|
||||
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
|
295
result.php
Normal file
295
result.php
Normal file
|
@ -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],[],[]]
|
Loading…
Reference in New Issue
Block a user