Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Recursive function to solve Hanoi Tower algorithm in JavaScript

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to use recursive function to solve the Tower of Hanoi algorithm in JavaScript", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use recursive function to solve Hanoi algorithm in JavaScript" can help you solve your doubts.

The Tower of Hanoi is a famous puzzle game. There are three columns and a set of hollow disks with different diameters on the tower. At the beginning, all the discs on the column are stacked from small to large. The goal is to move a pile of disks to the target column by moving one disc to another column at a time, and the large discs are not allowed to be placed on the smaller ones.

If you read this paragraph carefully, if there are 10 or more disks, the steps are absolutely shocking, but the goal is to move a pile of disks to the target pillar. If you regard the above nine disks as one set, and the 10th disc as another set, first move nine discs to another column, then regard the top eight disks as one set, and the ninth disc as another set. The idea of analogy, decomposition and movement, and recursive function is reflected.

Complete the code, a very simple way to write, I do not know if there is a simpler way to write?

Var hanoi = function {disc, begin, end, helper) {if (disc > 0) {hanoi (disc-1, begin, helper, end); [xss_clean] ln ('moving disk' + disc + 'from' + begin +'to'+ helper); hanoi (disc-1, end, begin, helper);}}; hanoi (3, 'Pillar 1', 'Pillar 2', 'Pillar 3')

Output when the number of disks is 3:

Move 1 from pillar one to pillar three

Move 2 from pillar one to pillar two

Move 1 from pillar three to pillar two

Move 3 from pillar one to pillar three

Move 1 from pillar two to pillar one

Move 2 from pillar 2 to pillar 3

Move 1 from pillar one to pillar three

The arguments passed to the hanoi function include the current moving disk number and the three columns it will use. When it invokes senior, it processes the disk on top of the disk currently being processed. Eventually, it will be called with a disk number that does not exist. In this case, it does nothing. Because this function ignores illegal values, you don't have to worry about causing a dead loop.

After reading this, the article "how to use recursive functions to solve the Tower of Hanoi algorithm in JavaScript" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report