Hackerss.com

hackerss
hackerss

Posted on

Php Solve the towers of hanoi problem


function hanoi($n, $from, $to, $aux) {
  if ($n == 1) {
    echo "Move disk 1 from rod $from to rod $to\n";
    return;
  }
  hanoi($n-1, $from, $aux, $to);
  echo "Move disk $n from rod $from to rod $to\n";
  hanoi($n-1, $aux, $to, $from);
}

//example
hanoi(3, 'A', 'C', 'B');


Enter fullscreen mode Exit fullscreen mode

Top comments (0)