zoj 2338 The Towers of Hanoi Revisited

1.47寸ST7789V3彩屏硬件设计与驱动调试指南 TFT LCD是一种基于薄膜晶体管阵列控制像素明暗的主动式显示技术,其核心依赖于专用驱动IC(如ST7789V3)完成时序生成、GRAM管理与伽马校正。该类小尺寸彩色显示屏广泛应用于嵌入式HMI、IoT终端与可穿戴设备,具备高PPI、低功耗和SPI接口易集成等工程优势。理解RGB565数据格式、SPI Mode 0通信时序及关键寄存器(如0x36方向控制、0x3A像素格式、0x2A/0x2B地址窗口)是实现稳定显示的基础;而正确处理电源设计(严禁5V直供)、复位时序与背光PWM调光,则直接决定系统可靠性与能 阅读详情

  还是太naive了,在知道经典汉诺塔的情况下,居然很久没想出来。其实多根棍子和三根是一个原理。n个盘子,m根棍子的最优解,一定是把一部分盘子(假设为x)先移到某个中间棍子上,也就是先解决子问题(x,m),然后把剩下的n-x个盘子通过剩下的m-1根棍子用最优方案转移,即子问题(n-x,m-1),最后把中间棍子上的那x个盘子移过去,即子问题(x,m)。这样可以用dp来求解。

#include <iostream>    
#include <stdio.h>    
#include <cmath>    
#include <algorithm>    
#include <string>    
#include <memory.h>    
#include <vector>    
#include <queue>    
#include <stack>  
using namespace std;

#define ll unsigned long long 


ll dp[77][77];
int num[77][77];

ll fun(int n,int m){
    if(dp[n][m]){
        return dp[n][m];
    }
    if(n==0){
        return 0;
    }else if(n==1){
        return 1;
    }else if(m==2){
        return n;
    }else if(m==3){
        num[n][m] = n-1;
        dp[n][m] = fun(n-1,3)*2+1;
        return dp[n][m];
    }
    ll res = (~(0ULL))>>1;;

    for(int i=1;i<n;i++){
        ll tmp = fun(i,m)*2+fun(n-i,m-1);
        if(tmp<=res){
            res = tmp;
            num[n][m] = i;
        }
    }
    dp[n][m] = res;
    return res;
}

stack<int> pegs[77];

void move(int from,int to){
    if(pegs[to].size()){
        printf("move %d from %d to %d atop %d\n",pegs[from].top(),from,to,pegs[to].top());
        pegs[to].push( pegs[from].top() );
        pegs[from].pop();
    }else{
        printf("move %d from %d to %d\n",pegs[from].top(),from,to);
        pegs[to].push( pegs[from].top() );
        pegs[from].pop();
    }
}

int M;

void solve(int n,int m,int from,int to){
    if(n==1){
        move(from,to);
        return;
    }
    if(m==2){
        while(n--){
            move(from,to);
        }
        return;
    }

    stack<int> where;
    stack<int> how;

    int curn = n;
    int curm = m;
    for(int i=1;i<=M;i++){
        if(i==from || i==to)continue;
        if(pegs[i].size() && pegs[from].top()>pegs[i].top())continue; 
        int tmp = num[curn][curm];
        if(tmp==0){
            break;
        }
        if(curn==1)break;
        solve(tmp,curm,from,i);
        where.push(i);
        how.push(tmp);
        curn-=tmp;
        curm--;

    }
    curm++;
    move(from,to);

    while(where.size()){
        solve(how.top(),curm,where.top(),to);
        how.pop();
        where.pop();
        curm++;
    }
}

int main(){
    for(int i=1;i<=65;i++){
        for(int j=4;j<=65;j++){
            fun(i,j);
        }
    }

    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        M = m;

        for(int i=n;i>=1;i--){
            pegs[1].push(i);
        }

        cout<<fun(n,m)<<endl;       
        solve(n,m,1,m);

        while(pegs[m].size()){
            pegs[m].pop();
        }
    }
    return 0;
} 
川崎机器人 AS语言基础运动指令表     功能简介 动作方式 参考坐标系 位置特性 X,Y,Z 角度特性 O,A,T 指令格式(距离单位毫米,时间单位秒,角度单位度 示例1 示例2 运 动 指 令 JMOVE 以关节移动到目标点 关节移动方式 基础坐标系 不受控 伪随机动作 JMOVE 目标点 JMOVE #yd JMOVE put1 LMOVE 以直线移动到目标点 直线移动方式 阅读详情

相关推荐

allegro 约束管理Constraint Manager(一)

介绍Cadence allegro 约束管理器CM的使用方法。

weixin_38975819的博客 8721

ZOJ Problem Set - 2338 The Towers of Hanoi Revisited DFS+预处理

ZOJ Problem Set - 2338 The Towers of Hanoi Revisited题目描述:  题目链接:Problem Set - 2338 The Towers of Hanoi Revisited题目大意:  给定N(1<= N <=64)个盘子和M(4<= M <= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程。解题思路:  对于

Running Boy的AC Dream 734

别再只用GWR了!用Python的mgtwr包搞定时空地理加权回归(GTWR)实战

本文详细介绍了如何使用Python的mgtwr包进行时空地理加权回归(GTWR)实战,突破传统GWR模型的局限。通过核心差异解析、实战全流程演示和进阶技巧,帮助读者掌握GTWR在房价波动、污染物扩散等时空数据分析中的应用,提升模型预测精度。

weixin_30899023的博客 344

ZOJ-2338 The Towers of Hanoi Revisited 输出汉诺塔的最优解移动过程

题意:给定N(1&lt;= N &lt;=64)个盘子和M(4&lt;= M &lt;= 65)根柱子,问把N个盘子从1号柱子移动到M号柱子所需要的最少步数,并且输出移动过程。 分析:设f[i][j]表示将i个盘通过j个柱子从第一根柱子移动到最后一根柱子所需要的最少次数,f[i][j]的得到的最优移动方案是先将最小的path[i][j]个盘移动到某一根柱子上,然后通过一个dfs输出移动次序。 ...

weixin_34313182的博客 190

Cesium 实战 28 - 三维视频融合(视频投影)

Cesium 实战 27 - 三维视频融合(视频投影)。

非科班Java出身GISer的博客 3369

GEE 案例分析:利用Landsat数据进行FAI、NDVI及CHLA分析并统计平均值(巢湖为例)

浮游藻类指数(FAI)、归一化植被指数(NDVI)及叶绿素a(CHLA)是水体环境监测的重要指标。本文利用Google Earth Engine(GEE)平台,基于Landsat 5和Landsat 8数据,进行水体藻类及植被监测。

此星光明博客 1336

ZOJ Problem Set - 1049

【【数学题】】 【题目】 I Think I Need a Houseboat Time Limit: 1 Second      Memory Limit: 32768 KB Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the

言子檡的优快云博客 2491

在Ubuntu上安装Node.js编程

在Ubuntu操作系统上安装Node.js是非常简单的,本文将为您提供详细的安装步骤。这是一个简单的Node.js HTTP服务器示例,它将在本地主机的端口3000上监听请求,并返回"Hello, World!npm是Node.js的包管理器,您可以使用它来安装和管理Node.js模块。您已成功在Ubuntu上安装了Node.js,并创建并运行了一个简单的Node.js应用程序。现在,您已经成功安装了Node.js和npm,让我们创建一个简单的Node.js应用程序并运行它。如有任何疑问,请随时提问。

CodeNexus的博客 3524

POJ 1985 Strange Towers of Hanoi

题目:输出盘子为1~12的四柱汉诺塔问题的答案分析:设f(n)为四柱汉诺塔n盘子的答案;设d(n)为三柱汉诺塔n盘子的答案d(n)=d(n-1)*2+1f(n)=min{f(i)*2+d(n-i)}(0&lt;=i&lt;n)d的推导就不说了,关于f,可以发现所有的移动方法(A-&gt;D)都可以归结为以下三步①i个盘子通过四柱移动(A-&gt;B or C)步数为f(i)②剩下n-i个盘子通过三...

勉強しているユキさんです 242

18. 从 SAP Fiori Elements List Report 中的图片显示,谈谈背后的 XML Template 设计机制

至此,Fiori Elements List Report 这个模版里,图片显示的原理,笔者已经从开发人员层面需要定义的注解,和 Fiori Elements 框架层面如何解析这些注解,这两方面做了深入介绍。从本文开始,我们从 Fiori Elements 预制的标准 XML Template 模版的设计,来介绍我们在最终 Fiori Elements 页面看到的产品图标,是如何渲染出来的。但是,List Report 里产品的图标显示,到底是上面这些 XML 文件里的哪一个里定义的呢?

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 607

【C语言】初识C语言(上篇)

初识C语言第一部分

StackFrame 4019

zoj 2338

HanioTower的加强版 记f[n][m]为n个disc,m个peg的Hanoi问题,则有dp公式f[n][m]=min{f[n-k][m-1]+2*f[k][m]}。即把上面的k个disc利用m个peg转移某个中间peg,再把下面的n-k个disc利用m-1个peg转移到目标peg,最后把上面的k个disc利用m个peg移到目标peg。dp过程记下使得f[n][m]最小的g[n][m]

1292765944的专栏 1480

用playwright封装一个处理web网页的爬虫,并隐藏自动化特征,自动处理反爬

本文介绍了一个使用Playwright封装的Python爬虫函数,具备反爬对抗能力。该脚本通过移除自动化标志、注入真实用户代理、禁用Chrome自动化属性等措施,有效绕过电商、新闻等中等反爬网站的检测。核心功能包括:隐藏navigator.webdriver标志、模拟人类浏览行为、设置合理的设备参数,并支持代理和延迟加载。使用时需注意避免高频访问并定期更新User-Agent。该方案适用于需要JavaScript渲染的动态网页,性能优于非无头模式。

数据知道的博客 3394

ZOJ

ZOJ题型分类   --------------------------------------------------------------------------------   初学者题: 1001 1037 1048 1049 1051 1067 1

&&&思考中前行~~ 1677

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

Acdream 1219 The Towers of Hanoi Revisited(递归汉诺塔问题)

传送门The Towers of Hanoi Revisited Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description You all must know t

代码改变世界 3534

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

UVa 1629 - Cake slicing

题意:n*m大小的蛋糕,被分成n*m个区域,其中k个区域上面有樱桃。你需要把蛋糕切成k份,每份都有樱桃,切的时候只能水平或竖直沿着网格线切,切到底。问最短切的长度是多少。 思路:DP。DP[u][d][l][r]。u,d,l,r表示整块蛋糕的上下左右。

A Cube Master 1948

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

UVa242 Stamps and Envelope Size

题意:信封上最多贴S张邮票。有N个you

A Cube Master 1729

UVa1630 Folding/poj 2176 Folding/zoj 1554 Folding

题意:给一个由大写字母组成的chuan

A Cube Master 1330

UVa12093 Protecting Zonk

题意:给定一个有n个节点的wu'gen'shu

A Cube Master 1270

CF 793D Presents in Bankopolis

D. Presents in Bankopolis一个有向图,节点“有序”,找一条长度为k的最短路径,额外要求是当前边不能“跃过”已经经过的节点。 起点的选择范围是[1,n]然后每经过一个点,下一个能到达的点的范围就会缩小,注意到总可以用一个连续的范围表示之,记为[l,r]。那么用dp解决,状态dp[l][r][k]表示可达范围是[l,r],已经走了k步。 貌似dp写成记忆化搜索更符合我的思维,

A Cube Master 1154

hdu 5119 Happy Matt Friends

题意:n个数,可以取其中一部分来异或,问全部异或结果不小于m有多少种取法。         思路:dp(背包)。dp(i,j)表示取到第i个数,异或结果为j,有多少种方法。初态是有一种取法取得0,然后对每个数,有两种选择,取或不取,加上上一步的路径数就可以了。最后将dp(n,j)中j>=m的部分求和就行。需要注意的是,会爆int。 #include #include #include

A Cube Master 1146

UVa1213 Sum of Different Primes

问一个数能表示为不同的 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #defin

A Cube Master 1098

CF 797F Mice and Holes(单调队列优化dp)

F. Mice and Holes 先给老鼠和洞排序,然后dp解之dp[i][j]表示前i个洞进了j个老鼠的最小cost,很容易想到O(n∗n∗m)O(n*n*m)的做法。然后试了下类似多重背包的方法去优化,结果T了。。 看了下官方题解,给的方案是用单调队列优化。因为我们要从dp[i-1][j-1],dp[i-1][j-2],dp[i-1][j-3]…转移到dp[i][j],每个不同的状态转移过

A Cube Master 1097

UVa12563 Jin Ge Jin Qu hao

知道它是0-1背包,还是跪了很多次。。xian

A Cube Master 970

TopCoder SRM 696

给了20条边,最多会涉及到40个点。我们可以先贪心把那些肯定不会影响结果的点先染色(比如度为0的点和度为1的点,如果一条边连接的两个点度都是1,那么先只染一个,不然会有花费)。贪心过后,最多剩下20个点,这样就可以状压dp了。#include <bits/stdc++.h> using namespace std;#define ll long longint deg[55]; bool vis[5

A Cube Master 925

UVa1252 Twenty Questions

传说中的状态压缩DP。。把某一类状态压缩成

A Cube Master 842
上一篇: Google Code Jam 2016 Round 1A
下一篇: TopCoder SRM 688
squee_spoon
博客等级 码龄14年 61粉丝 455原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值