zoukankan      html  css  js  c++  java
  • HDU 2077 汉诺塔IV (递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2077

    还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到小盘的上面。xhd在想如果我们允许最大的盘子放到最上面会怎么样呢?(只允许最大的放在最上面)当然最后需要的结果是盘子从小到大排在最右边。 

    Input输入数据的第一行是一个数据T,表示有T组数据。 
    每组数据有一个正整数n(1 <= n <= 20),表示有n个盘子。 
    Output对于每组输入数据,最少需要的摆放次数。 
    Sample Input

    2
    1
    10

    Sample Output

    2
    19684

    题解:由汉诺塔3知从左往右的递推关系式为F[n]=F[n-1]*3+2,而此题允许最大盘在上记其步骤数为f[n],

    则f[n]=F[n-1]+2。原理很简单,将上面n-1个盘看为整体先将其移到B上再将 第n个盘移到B上再将其移到C上,最后再将n-1个移到C上

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <vector>
     6 #include <cstdlib>
     7 #include <iomanip>
     8 #include <cmath>
     9 #include <ctime>
    10 #include <map>
    11 #include <set>
    12 #include <queue>
    13 using namespace std;
    14 #define lowbit(x) (x&(-x))
    15 #define max(x,y) (x>y?x:y)
    16 #define min(x,y) (x<y?x:y)
    17 #define MAX 100000000000000000
    18 #define MOD 1000000007
    19 #define pi acos(-1.0)
    20 #define ei exp(1)
    21 #define PI 3.141592653589793238462
    22 #define INF 0x3f3f3f3f3f
    23 #define mem(a) (memset(a,0,sizeof(a)))
    24 typedef long long ll;
    25 ll gcd(ll a,ll b){
    26     return b?gcd(b,a%b):a;
    27 }
    28 bool cmp(int x,int y)
    29 {
    30     return x>y;
    31 }
    32 const int N=10005;
    33 const int mod=1e9+7;
    34 int main()
    35 {
    36     std::ios::sync_with_stdio(false);
    37     ll F[21],f[21],t,n;
    38     F[1]=2,f[1]=2;
    39     for(int i=2;i<=20;i++)
    40         F[i]=3*F[i-1]+2;
    41     for(int i=2;i<=20;i++)
    42         f[i]=F[i-1]+2;
    43     cin>>t;
    44     while(t--){
    45         cin>>n;
    46         cout <<f[n]<< endl;
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    添加或删除项并动态记录项的值
    练习题。对DOM中document的深刻理解巩固
    document--文档中的操作,操作属性、操作样式、操作元素
    10.13DOM中document--文档1找到元素的方法,还有元素内容属性
    函数的定义,语法,二维数组,几个练习题
    10.11讲的内容总结
    js基础巩固练习
    10.9做的一个静态页面(巩固前面的内容)
    9.29学习的js基础
    9.28做的作业(企业名称静态网页)
  • 原文地址:https://www.cnblogs.com/wydxry/p/7297789.html
Copyright © 2011-2022 走看看