前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++经典算法题-双色、三色河内塔

C++经典算法题-双色、三色河内塔

作者头像
cwl_java
发布2022-11-30 08:35:58
5410
发布2022-11-30 08:35:58
举报
文章被收录于专栏:cwl_Java

12.Algorithm Gossip: 双色、三色河内塔

说明

双色河内塔与三色河内塔是由之前所介绍过的河内塔规则衍生而来,双色河内塔的目的是将下图左上的圆环位置经移动成为右下的圆环位置:

在这里插入图片描述
在这里插入图片描述

而三色河内塔则是将下图左上的圆环经移动成为右上的圆环:

在这里插入图片描述
在这里插入图片描述

解法

无论是双色河内塔或是三色河内塔,其解法观念与之前介绍过的河内塔是类似的,同样也是使用递回来解,不过这次递回解法的目的不同,我们先来看只有两个盘的情况,这很简单, 只要将第一柱的黄色移动至第二柱,而接下来第一柱的蓝色移动至第三柱。再来是四个盘的情况,首先必须用递回完成下图左上至右下的移动:

在这里插入图片描述
在这里插入图片描述

接下来最底层的就不用管它们了,因为它们已经就定位,只要再处理第一柱的上面两个盘子就可以了。那么六个盘的情况呢?一样!首先必须用递回完成下图左上至右下的移动:

在这里插入图片描述
在这里插入图片描述

接下来最底层的就不用管它们了,因为它们已经就定位,只要再处理第一柱上面的四个盘子就可以了,这又与之前只有四盘的情况相同,接下来您就知道该如何进行解题了,无论是八个盘、 十个盘以上等,都是用这个观念来解题。

那么三色河内塔呢?一样,直接来看九个盘的情况,首先必须完成下图的移动结果:

在这里插入图片描述
在这里插入图片描述

接下来最底两层的就不用管它们了,因为它们已经就定位,只要再处理第一柱上面的三个盘子就可以了。

在这里插入图片描述
在这里插入图片描述

双色河内塔 C 实作

代码语言:javascript
复制
#include<stdio.h>


    void hanoi(int disks, char source, char temp, char target) {
        if (disks == 1) {
            printf("move disk from %c to %c\n", source, target);
            printf("move disk from %c to %c\n", source, target);
        } else {
            hanoi(disks - 1, source, target, temp);
            hanoi(1, source, temp, target);
            hanoi(disks - 1, temp, source, target);
        }
    }

    void hanoi2colors(int disks) {
        char source = 'A';
        char temp = 'B';
        char target = 'C';
        int i;
        for (i = disks / 2; i > 1; i--) {
            hanoi(i - 1, source, temp, target);
            printf("move disk from %c to %c\n", source, temp);
            printf("move disk from %c to %c\n", source, temp);
            hanoi(i - 1, target, temp, source);
            printf("move disk from %c to %c\n", temp, target);
        }
        printf("move disk from %c to %c\n", source, temp);
        printf("move disk from %c to %c\n", source, target);
    }

    int main() {
        int n;
        printf("请输入盘数:");
        scanf("%d", & n);

        hanoi2colors(n);

        return 0;
    }

三色河内塔 C 实作

代码语言:javascript
复制
#include<stdio.h>


    void hanoi(int disks, char source, char temp, char target) {
        if (disks == 1) {
            printf("move disk from %c to %c\n", source, target);
            printf("move disk from %c to %c\n", source, target);
            printf("move disk from %c to %c\n", source, target);
        } else {
            hanoi(disks - 1, source, target, temp);
            hanoi(1, source, temp, target);
            hanoi(disks - 1, temp, source, target);
        }
    }

    void hanoi3colors(int disks) {
        char source = 'A';
        char temp = 'B';
        char target = 'C';
        int i;
        if (disks == 3) {
            printf("move disk from %c to %c\n", source, temp);
            printf("move disk from %c to %c\n", source, temp);
            printf("move disk from %c to %c\n", source, target);
            printf("move disk from %c to %c\n", temp, target);
            printf("move disk from %c to %c\n", temp, source);
            printf("move disk from %c to %c\n", target, temp);
            ;
        } else {
            hanoi(disks / 3 - 1, source, temp, target);
            printf("move disk from %c to %c\n", source, temp);
            printf("move disk from %c to %c\n", source, temp);
            printf("move disk from %c to %c\n", source, temp);

            hanoi(disks / 3 - 1, target, temp, source);
            printf("move disk from %c to %c\n", temp, target);
            printf("move disk from %c to %c\n", temp, target);
            printf("move disk from %c to %c\n", temp, target);


            hanoi(disks / 3 - 1, source, target, temp);
            printf("move disk from %c to %c\n", target, source);
            printf("move disk from %c to %c\n", target, source);

            hanoi(disks / 3 - 1, temp, source, target);
            printf("move disk from %c to %c\n", source, temp);

            for (i = disks / 3 - 1; i > 0; i--) {
                if (i > 1) {
                    hanoi(i - 1, target, source, temp);
                }
                printf("move disk from %c to %c\n", target, source);
                printf("move disk from %c to %c\n", target, source);
                if (i > 1) {
                    hanoi(i - 1, temp, source, target);
                }
                printf("move disk from %c to %c\n", source, temp);
            }
        }
    }

    int main() {
        int n;
        printf("请输入盘数:");
        scanf("%d", & n);

        hanoi3colors(n);
        return 0;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-01-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 12.Algorithm Gossip: 双色、三色河内塔
    • 说明
      • 解法
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档