zoukankan      html  css  js  c++  java
  • 96. 奇怪的汉诺塔

     d[i]表示在三根木棍的情况下,i个盘子要走d[i]步(注意,d[i] 当前i个盘子,三根木棍均可走)

    f[i]表示在四根木棍的情况下,i个盘子要走的最短步数f[i]步(注意,f[i] 当前i个盘子,四根木棍均可走)

    f[j] * 2 或 d[i - 1] * 2 表示拿下j(或i - 1)个盘子放到一个临时棍子上,最后还得放到最终的那个棍子上,所以要乘以2

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
    	int d[15], f[15];
    	
    	d[1] = 1;
    	for(int i = 2; i <= 12; ++ i)
    	{
    		d[i] = 1 + d[i - 1] * 2;
    	}
    	
    	memset(f, 0x3f, sizeof(f));
    	f[1] = 1;
    	for(int i = 2; i <= 12; ++ i)
    	{
    		for(int j = 1; j < i; ++ j)
    		{
    			f[i] = min(f[i], f[j] * 2 + d[i - j]);
    		}
    	}
    	
    	for(int i = 1; i <= 12; ++ i)
    	{
    		cout << f[i] << endl;
    	}
    	
    	return 0;
    }
    

      

  • 相关阅读:
    open jdk
    llvm 编译
    llvm Array Bounds Check Elimination
    tmux 共享窗口大小
    llvm pass
    llvm code call graph
    llvm -O 经历过那些pass
    tcmalloc asan
    web ide
    eclipse配置
  • 原文地址:https://www.cnblogs.com/mjn1/p/11779114.html
Copyright © 2011-2022 走看看