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

推荐订阅源

S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
P
Palo Alto Networks Blog
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
T
Tenable Blog
人人都是产品经理
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
Scott Helme
SecWiki News
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
Cloudbric
Cloudbric
C
Check Point Blog
Engineering at Meta
Engineering at Meta
TaoSecurity Blog
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
腾讯CDC
量子位
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs

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";
}