-1

我不知道河内塔。我想用递归写一个程序。

4

6 回答 6

7

另一个家庭作业。把你老师的 A 传给我 ;)

来源: http: //www.soc.napier.ac.uk/~andrew/hanoi/rechelp.html
奖励:一步一步的YouTube 视频


关于河内塔的一点点

对此的分析以及对(发明的)神话和四钉版本的讨论可以在rec.puzzles FAQ中找到induction/hanoi.s

河内塔问题有一个很好的递归解决方案。

制定递归解决方案

要解决这些问题,问问自己:“如果我解决了n-1案例,我可以解决n案例吗?”

如果这个问题的答案是肯定的,那么你会在n-1案例已经解决的荒谬假设下继续进行。奇怪的是,只要有一些可以被视为特殊情况的基本情况(通常当n为零或一时),它就可以工作。

如何将 n 个环从 A 极移到 C 极?

如果您知道如何将n-1 个环从一个杆移动到另一个杆,那么只需将n-1 个环移动到备用杆 - 现在源杆上只剩下一个环,只需将其移动到目的地,然后将其余的堆放他们从备用杆到目的地杆。

例如,当n为 4...

河内 - 第 1 步
首先将三个移到备用杆上(担心以后如何做)。

河内 - 第 2 步
现在将一个环从源极移动到目标极。

河内 - 第 3 步
现在将三个环从备用杆移动到目标杆
(同样,我们可以考虑稍后如何执行此操作)。

河内 - 第 4 步
我们已经完成了!

更简洁...

使用 B 作为备用将n 个环从 A 移动到 C:

  • 如果n为 1
    • 去做就对了,
  • 否则...
    • 使用 C 作为备用将n-1 个环从 A 移动到 B
    • 将一环从 A 移到 C
    • 使用 A 作为备用将n-1 个环从 B 移动到 C

与大多数递归解决方案一样,我们必须特别处理一些基本情况——这里的基本情况发生在我们只有一个环可以移动的地方。

如何在 C 中做到这一点

/* Tower of Hanoi - the answer */
/* How to move four rings from pin 1 to pin 3 using pin 2 as spare */
#include <stdio.h>

void move(n, A, C, B)
  /* number to move, source pole, destination pole and
     spare pole respectively */
  int n, A, B, C; {
    if (n == 1) {
      printf("Move from %d to %d.\n", A, C);
    } else {
      move(n - 1, A, B, C);
      move(1, A, C, B);
      move(n - 1, B, C, A);
    }
  }

  main() {
    move(4, 1, 3, 2);
  }
于 2009-07-18T11:06:23.043 回答
2

这是 Lisp 中的一个紧凑实现:http: //www.kernelthread.com/projects/hanoi/html/gcl.html。它当然是递归的,但我没有验证它的正确性。

于 2009-07-18T11:08:08.517 回答
1

来自维基百科

河内塔或河内塔(也称为梵天塔)是一种数学游戏或谜题。它由三个杆和许多不同大小的圆盘组成,这些圆盘可以滑到任何杆上。拼图从将圆盘按大小顺序整齐地堆叠在一根杆上开始,最小的在顶部,从而形成圆锥形。

查看递归解决方案

于 2009-07-18T11:14:12.327 回答
1
#!/usr/bin/env python
discs = 3
T = [range(discs, 0, -1), [], []]

def show_towers():
    """Render a picture of the current state of the towers"""
    def render_disc(t, y):
        return ("-"*(t[y]*2-1) if y < len(t) else "|").center(discs*2)

    for y in range(discs):  
        print " ".join(render_disc(t, discs-y-1) for t in T)
    print "="*(discs*6+3)


def move(n, source, destination):
    """Recursively move n discs from source to destination"""
    while n > 0:
        temp = 3 - source - destination 
        move(n-1, source, temp)
        T[destination].append(T[source].pop())
        show_towers() 
        n, source = n-1, temp    # Simulate tail recursion


show_towers()
move(discs, 0, 2)

光盘输出 = 3

  -      |      |   
 ---     |      |   
-----    |      |   
=====================
  |      |      |   
 ---     |      |   
-----    |      -   
=====================
  |      |      |   
  |      |      |   
-----   ---     -   
=====================
  |      |      |   
  |      -      |   
-----   ---     |   
=====================
  |      |      |   
  |      -      |   
  |     ---   ----- 
=====================
  |      |      |   
  |      |      |   
  -     ---   ----- 
=====================
  |      |      |   
  |      |     ---  
  -      |    ----- 
=====================
  |      |      -   
  |      |     ---  
  |      |    ----- 
=====================
于 2012-10-06T09:07:46.420 回答
0

计算机程序的结构和解释视频讲座包含解决这个问题的有用技巧以及丰富的知识。

于 2009-07-18T11:09:56.360 回答
0

有关递归算法的描述,请参见wikipedia towers of hanoi文章。

它是这样的:

#include <iostream>   // ostream
#include <algorithm>  // for_each
#include <deque> // I can iterate over towers & print state,<stack> works as well
#include <boost/array.hpp>   // just a wrapper for array
#include <boost/lambda/lambda.hpp>  // easy one line for_each iterating

using namespace std;

typedef std::deque< int > tower_t;  // stack works as well, deque for printing
typedef boost::array< tower_t ,3 > towers_t;  // 3 towers

enum peg { A = 0, B = 1, C = 2 };

印刷:

ostream & show(ostream & os, const tower_t & t)
{
    os << "[";
    for_each (t.begin(), t.end(), os << boost::lambda::_1 );
    return os << "]";
}

ostream & show(ostream & os, const towers_t & t)
{
    show(os, t[0]); show(os, t[1]); show(os, t[2]);
    return os;
}

解决:

void move(peg from, peg to, towers_t & t)
{
    // show move and state before move
    cout << "mv: " << t[from].back() << " " << from << " --> " << to << "\t\t";
    show(cout, t); cout << " --> ";

    // the actual move: move top peg `from` stick `to` stick (and `pop` old top)
    t[to].push_back(t[from].back());
    t[from].pop_back();

    // show state after move
    show(cout, t); cout << endl;
}

// move n discs from A to B via C
void move(int n, peg from, peg to, peg via, towers_t & t)
{
    if (n == 1) { move(from, to, t); return; }

    move(n-1, from, via, to, t);
    move(from, to, t);
    move(n-1, via, to, from, t);

    return;
}

用法,用4个钉子解决塔:

int main()
{
    towers_t ttt;
    tower_t & first_tower(ttt[0]);
    first_tower.push_back(4);
    first_tower.push_back(3);
    first_tower.push_back(2);
    first_tower.push_back(1);

    move(first_tower.size(), A, C, B, ttt); // move n from A to C via B
}

解决了 3 个塔,第一个塔上有 4 个钉子,最大的钉子有最高的数字,最小的一个是 1。

输出 ( mv: PegX FromTower ---> ToTower) 后跟移动前后的状态,每个塔从左到右显示从下到上的钉子 - 顶部在右侧:

mv: 1 0 --> 1       [4321][][] --> [432][1][]
mv: 2 0 --> 2       [432][1][] --> [43][1][2]
mv: 1 1 --> 2       [43][1][2] --> [43][][21]
mv: 3 0 --> 1       [43][][21] --> [4][3][21]
mv: 1 2 --> 0       [4][3][21] --> [41][3][2]
mv: 2 2 --> 1       [41][3][2] --> [41][32][]
mv: 1 0 --> 1       [41][32][] --> [4][321][]
mv: 4 0 --> 2       [4][321][] --> [][321][4]
mv: 1 1 --> 2       [][321][4] --> [][32][41]
mv: 2 1 --> 0       [][32][41] --> [2][3][41]
mv: 1 2 --> 0       [2][3][41] --> [21][3][4]
mv: 3 1 --> 2       [21][3][4] --> [21][][43]
mv: 1 0 --> 1       [21][][43] --> [2][1][43]
mv: 2 0 --> 2       [2][1][43] --> [][1][432]
mv: 1 1 --> 2       [][1][432] --> [][][4321]
于 2012-01-31T06:06:56.213 回答