Hanoi Tower problem PHP Solution

Source: Internet
Author: User
Tags php class

Process-oriented solutions

<?php function Hanio ($n, $x, $y, $z) {//Put n plates, as required from X to Z,y is the mediation//recursive bounce condition if ($n ==1) {Move ($n, $x, $z);} else{//these three are core Hanio ($n-1, $x, $z, $y), Move ($n, $x, $z), Hanio ($n-1, $y, $x, $z);}} function Move ($n, $x, $y) {$format = ' move%d from%s to%s ';p rintf ($format, $n, $x, $y); echo "<br/>";} Hanio (2, ' A ', ' B ', ' C ');? >

Process-oriented writing

<?php class hanio{private $n;//scale private $start;//Start column private $mediator;//intermediary pillar private $goal;//target pillar private $format = ' Move%d from%s to%s ';p ublic function __construct ($n, $start, $mediator, $goal) {$this->n = $n; $this->start = $start; $ This->mediator = $mediator; $this->goal = $goal;} Single disk move private function Move ($n, $start, $goal) {printf ($this->format, $n, $start, $goal); echo "<br/>";} Public Function GetResult () {$this->handle ($this->n, $this->start, $this->mediator, $this->goal);} Private function handle ($n, $x, $y, $z) {//recursive jump-out condition if ($n ==1) {$this->move ($n, $x, $z);} else{//these three are core $this->handle ($n-1, $x, $z, $y), $this->move ($n, $x, $z), $this->handle ($n-1, $y, $x, $z);}} Class Client{public static function main () {$hanio = new Hanio (4, ' A ', ' B ', ' C '); $hanio->getresult ();}} Client::main ();? >

  

Hanoi Tower problem PHP Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.