首页 > ACM题库 > HDU-杭电 > hdu 2511 汉诺塔 X-递归和分治-[解题报告]C++
2014
02-09

hdu 2511 汉诺塔 X-递归和分治-[解题报告]C++

汉诺塔 X

问题描述 :

1,2,…,n表示n个盘子.数字大盘子就大.n个盘子放在第1根柱子上.大盘不能放在小盘上.在第1根柱子上的盘子是a[1],a[2],…,a[n]. a[1]=n,a[2]=n-1,…,a[n]=1.即a[1]是最下面的盘子.把n个盘子移动到第3根柱子.每次只能移动1个盘子,且大盘不能放在小盘上.问第m次移动的是哪一个盘子,从哪根柱子移到哪根柱子.例如:n=3,m=2. 回答是 :2 1 2,即移动的是2号盘,从第1根柱子移动到第2根柱子 。

输入:

第1行是整数T,表示有T组数据,下面有T行,每行2个整数n (1 ≤ n ≤ 63) ,m≤ 2^n-1

输出:

第1行是整数T,表示有T组数据,下面有T行,每行2个整数n (1 ≤ n ≤ 63) ,m≤ 2^n-1

样例输入:

4
3 2
4 5
39 183251937942
63 3074457345618258570

样例输出:

2 1 2
1 3 1
2 2 3
2 2 3

2011-12-31 20:09:16

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2511

题意:中文。

mark:递归。

代码:

# include <stdio.h>


void gao(long long n, long long time, int s, int e)
{
    int m = 6-s-e ;
    long long mid = (1LL<<(n-1)) ;
    if (time == mid)
    {
        printf ("%I64d %d %d\n", n, s, e) ;
        return ;
    }
    if (time < mid)
        gao(n-1, time, s, m) ;
    else
        gao(n-1, time-mid, m, e) ;
}


int main ()
{
    int T ;
    long long n, time ;
    scanf ("%d", &T) ;
    while (T--)
    {
        scanf ("%I64d%I64d", &n, &time) ;
        gao(n, time, 1, 3) ;
    }
    return 0 ;
}

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/01/07/2315442.html


  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  2. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。