zoukankan      html  css  js  c++  java
  • AcWing96 奇怪的汉诺塔 (递推)

    题目链接:https://www.acwing.com/problem/content/98/

    (d[n])表示(n)(3)塔问题的最小步数,(f[n])表示(n)(4)塔问题的最小步数
    (f[n] = min_{1 leq i< n}{2*f[i]+d[n-i]})
    其中(f[1] = 1)
    含义是先将(i)个盘子在四塔模式下移到(B)柱,再将(n-i)个盘子在三塔模式下移到(D)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cmath>
    #include<stack>
    #include<queue>
    using namespace std;
    typedef long long ll;
    
    const int maxn = 20;
    const int inf = 100000007;
    
    int n;
    int d[maxn],f[maxn];
    int sta[maxn],tail = 0; 
    
    ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
    
    int main(){
    	n = 12;
    	d[0] = 0;
    	d[1] = 1;
    	for(int i=1;i<=n;++i) d[i] = d[i-1] * 2 + 1;
    	
    	f[1] = 1;
    	for(int i=2;i<=n;++i){
    		f[i] = inf;
    		for(int j=1;j<i;++j){
    			f[i] = min(f[i],2 * f[j] + d[i-j]);
    		}
    	}
    	
    	for(int i=1;i<=n;++i) printf("%d
    ",f[i]);
    	
    	return 0;
    }
    
  • 相关阅读:
    进程空间与系统空间(一)
    内核之最
    Linux 内核3.10.5 专场
    device_create与device_register
    重写与重载的含义
    抽象类和接口的区别
    Spring知识点
    mybatis学习
    知识点
    Mybatis面试题
  • 原文地址:https://www.cnblogs.com/tuchen/p/13909229.html
Copyright © 2011-2022 走看看