This article is about PHP Hanoi Tower problem Recursive algorithm implementation and iterative algorithm implementation, has a certain reference value, now share to everyone, there is a need for friends can refer to
Implementation code
Program code Address: Https://github.com/ParrySMS/Exp/tree/master/ProLang/hannota
Recursive methodhannoRec.php
<?php/** * Created by Phpstorm. * User:l * date:2018-4-15 * time:2:07 *//** Recursive implementation * @param $id//Plate number * @param $first//Start column * @param $middle//intermediary pillar * @ param $end//end pillar */function hanrec ($id, $first, $middle, $end, $counter) { if ($id = = 1) { move (1, $first, $end, $cou nter); } else { Hanrec ($id-1, $first, $end, $middle, $counter); Move ($id, $first, $end, $counter); $counter + +; Hanrec ($id-1, $middle, $first, $end, $counter); }} function Move ($id, $from, $to, $counter) { global $counter; $counter + +; echo "Step: $counter, Level $id from $from move to $to, <br/>";}
Iterative method Hannoiter
<?php/** * Created by Phpstorm. * User:l * DATE:2018-4-17 * time:2:38 */class params{//Define an object to hold the parameter state public $id; Public $num; Public $first; Public $middle; Public $end; Public $counter; /** * Params constructor. * @param $num * @param $first * @param $middle * @param $end * @param $counter * * Public Function _ _construct ($id, $num, $first, $middle, $end, $counter) {$this->id = $id; $this->num = $num; $this->first = $first; $this->middle = $middle; $this->end = $end; $this->counter = $counter; }}function Haniter ($id, $num, $first, $middle, $end, $counter) {$stack =init ($id, $num, $first, $middle, $end, $counter); while ($stack) {//out of stack $action = Array_pop ($stack); Var_dump ($action); if ($action->num ==1) {Move ($action->id, $action->first, $action->end, $action->counter); }else{//into the stack$next _stack = init ($action->id, $action->num, $action->first, $action->middle, $action->end, $action- >counter); $stack =array_merge ($stack, $next _stack); }}}/** into the stack operation * @param $id//need to move the plate * @param $num//Move the plate number of total plates to be moved * @param $first * @param $middle * @param $end * @p Aram $counter * @return Array */function init ($id, $num, $first, $middle, $end, $counter) {unset ($stack); Note The inbound outbound Order $stack = array (); First callback $stack [] =new Params ($id-1, $num-1, $middle, $first, $end, $counter); The second callback $stack [] =new Params ($id, 1, $first, $middle, $end, $counter); The third callback $stack [] =new Params ($id-1, $num-1, $first, $end, $middle, $counter); return $stack;} /** if in the test case, because two files have this move function, function name, comment out one can function move ($id, $from, $to, $counter) {global $counter; $counter + +; echo "Step: $counter, Level $id from $from move to $to, <br/>";} **/
Execution Time Test Script test.php
<?php/** * Created by Phpstorm. * User:l * DATE:2018-4-17 * time:2:18 */require "hannorec.php" Require "hannoiter.php";d efine (' Times ', +);d efine (' NUM "," function rowtable () {unset ($row); $row = Array (); for ($i = 0; $i < times; $i + +) {$row = Getsortrow ($row); } foreach ($row as $r) {print <<< TR <tr> <td> $r->iter</td> <td> ; $r->rec</td> </tr>TR; }}function Getsortrow (array $row) {$num = num; $counter = 0; $stime = Microtime (true); Haniter ($num, $num, ' A ', ' B ', ' C ', $counter); $etime = Microtime (true); $iterTime = * ($etime-$stime);//echo "<br/>"; $counter = 0; $num = num; $stime = Microtime (true); Hanrec ($num, ' A ', ' B ', ' C ', $counter); $etime = Microtime (true); $recTime = + * ($etime-$stime); $row [] = (object) ["ITER"] and $iterTime, "Rec" and $recTime]; return $row;}? ><table border= "1" > <tr> <th> iterative iter/ms</th> <th> recursive rec/ms</th> </tr> <?php rowtable ();? ></t Able>
Reference
Iterative approach: https://wenku.baidu.com/view/dce79165b0717fd5360cdcdb.html