惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

TinyEdi

Coroutine GDB with Python Bazel Notes Unix related things Shared Library I dreamt for so long The Building Blocks of Transformers A note for cmake A Strory of Mixin Tablegen Language Tutorial Lit and FileCheck 比较运算符, Min, Max, Sort 和 Order IEEE 754 的 inf 比较问题 KD树与SKD树
汉诺塔问题-记录
Edimetia3D · 2020-05-18 · via TinyEdi
记录

汉诺塔问题-记录

偶尔看到了汉诺塔这个问题,作为益智题从正向分析了一下,很难解. 编程解的思路倒是很清晰.简单记录一下

#include <iostream>
#include <vector>


struct Stock
{
    std::vector<int> plates;
    int id = 0;
};

Stock g_stocks[3];

void plotOneStock(const Stock& stk)
{
    printf("* ");
    for (int i = 0; i < stk.plates.size(); ++i)
    {
        printf("%d ", stk.plates[i]);
    }
}

void plotStocks()
{
    printf("=====================\n");
    plotOneStock(g_stocks[0]);
    printf("\n");
    plotOneStock(g_stocks[1]);
    printf("\n");
    plotOneStock(g_stocks[2]);
    printf("\n");
}

struct OneMove
{
    OneMove(Stock& src_stk, Stock& dest_stk)
    {
        if (dest_stk.plates.size() > 0 && src_stk.plates.back() > dest_stk.plates.back())
        {
            throw "shit";
        }
        plotStocks();
        src = src_stk.id;
        dest = dest_stk.id;
        weight = src_stk.plates.back();
        src_stk.plates.pop_back();
        dest_stk.plates.push_back(weight);
    }

    int src = -1;
    int dest = -1;
    int weight = -1;
};


void moveAtoC(Stock& stkA, Stock& stkB, Stock& stkC, int num_to_move, std::vector<OneMove>& moves)
{
    if (num_to_move == 1)
    {
        OneMove move(stkA, stkC);
        moves.push_back(move);
        return;
    }
    moveAtoC(stkA, stkC, stkB, num_to_move - 1, moves);
    OneMove move{stkA, stkC};
    moves.push_back(move);
    moveAtoC(stkB, stkA, stkC, num_to_move - 1, moves);
}

int main()
{
    const int N = 10;

    g_stocks[0].id = 0;
    g_stocks[1].id = 1;
    g_stocks[2].id = 2;
    std::vector<OneMove> moves;
    for (int i = 0; i < N; ++i)
    {
        g_stocks[0].plates.push_back(N - i);
    }
    moveAtoC(g_stocks[0], g_stocks[1], g_stocks[2], N, moves);
    std::cout << "Hello World!\n";
}