# 递归思想-汉诺塔问题

```python
step=1

def move(number_i,m_from,m_to):
    global step
    '''
    把第number_i号盘子，从m_from移动到m_to位置
    '''
    print('step:%d,move %d: from %s to %s'%(step,number_i,m_from,m_to))
    step+=1

def hanoi(n,A,B,C):
    '''
    把n个盘子，从A处移动到C处，借助B位置
    '''

    if n==1:#如果A处只剩下一个盘子，那就把它放到C
        move(1,A,C)
    else:
        #否则就把n-1个盘子放到B；再把n号盘子放到C；再把n-1个盘子放到C;
        hanoi(n-1,A,C,B)
        move(n,A,C)
        hanoi(n-1,B,A,C)
nums=4
hanoi(nums,'A','B','C')
```

```
step:1,move 1: from A to B
step:2,move 2: from A to C
step:3,move 1: from B to C
step:4,move 3: from A to B
step:5,move 1: from C to A
step:6,move 2: from C to B
step:7,move 1: from A to B
step:8,move 4: from A to C
step:9,move 1: from B to C
step:10,move 2: from B to A
step:11,move 1: from C to A
step:12,move 3: from B to C
step:13,move 1: from A to B
step:14,move 2: from A to C
step:15,move 1: from B to C
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/si-xiang-lei/di-gui/han-nuo-ta-wen-ti.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
