zoukankan      html  css  js  c++  java
  • poj 3601 Tower of Hanoi

    Tower of Hanoi
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 1853   Accepted: 635

    Description

    The Tower of Hanoi is a puzzle consisting of three pegs and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another peg, obeying the following rules:

    • Only one disk may be moved at a time.
    • Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
    • No disk may be placed on top of a smaller disk.

    For n disks, it is a well-known result that the optimal solution takes 2n − 1 moves.

    To complicate the puzzle a little, we allow multiple disks to be of the same size. Moreover, equisized disks are mutually distinguishable. Their ordering at the beginning should be preserved at the end, though it may be disturbed during the process of solving the puzzle.

    Given the number of disks of each size, compute the number of moves that the optimal solution takes.

    Input

    The input contains multiple test cases. Each test case consists of two lines. The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 106). The second lines contains n integers a1a2, …, an (1 ≤ a1a2, …, an ≤ 105). For each 1 ≤ i ≤ n, there are ai disks of size i. The input ends where EOF is met.

    Output

    For each test case, print the answer modulo m on a separate line.

    Sample Input

    1 1000
    2
    5 1000
    1 1 1 1 1
    5 1000
    2 2 2 2 2
    5 1000
    1 2 1 2 1

    Sample Output

    3
    31
    123
    41

    Source

    分析:

    学习网址:http://blog.csdn.net/acm18810549519/article/details/10155281 

    动态规划

    题意:汉诺塔问题,不过其中加了一个条件,就是盘子可以有相同的

    可以发现,当移动第i 种盘子时&& num[i]>=2得到的是逆序的,但是题目要求不能改变顺序,所以必须处理

    1.先考虑不按顺序的情况为 dp1[i]=dp1[i-1]*2+num[i]  ,num[i]为相同盘子的个数

    公式得来:如果从A到C轴,借助B轴,i种盘子,要先把i - 1种移动到 B, 需要dp1[i - 1],然后把第i种移动到C,需要num[i],然后再把i - 1种从B移动到C,需要dp1[i - 1]
    所以得到dp1[i]=dp1[i-1]*2+num[i] .

    2.考虑按顺序的情况为 dp2[i]=2*dp1[i-1]+2*num[i]+dp2[i-1]

    公式得来:把num[1] - 1个移动到辅助轴,这时这num[1] - 1个盘子的顺序颠倒了,然后把第num[1]中最后一个移动到目标轴,然后把辅助轴上的移动回来,再次颠倒,恢复顺序,得到dp2[1] = 2 * (num[1] - 1) +1。
    对于dp2[i],
    如果x[i] == 1,那么第i种就不需要考虑顺序(只有一种),所以dp2[i] = dp1[i]
    否则,第i种要调动2次,保证顺序不变。
    还是从A到C轴,借助B轴,i种盘子
    把i - 1种不考虑顺序移到C,需要dp1[i - 1].
    把第i种从A移动到B,需要num[i](颠倒顺序),
    把i - 1种不考虑顺序移到A(腾出C),需要dp1[i - 1].
    把第i种从B移动到C,需要num[i](顺序恢复)
    把i - 1种考虑顺序移到C,需要dp2[i - 1].
    所以有dp2[i] = 2 * dp1[i - 1] +2 * num[i] +dp2[i - 1]
    最后答案是dp2[n].

     1 //起始条件:dp1[1]=num[1];dp2[1]=2*num[1]-1
     2 //num[i]==1:dp2[i]=2*dp1[i-1]+1
     3 //num[i]>=2:dp2[i]=dp1[i]+num[i]+dp1[i]+num[i]+dp2[i]=2*dp1[i-1]+2*num[i]+dp2[i-1]
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<iostream>
     8 using namespace std;
     9 int num[105],dp1[105],dp2[105];
    10 int main(){
    11     int n,m;
    12     while(cin>>n>>m){
    13         int i=1;
    14         for(;i<=n;i++){
    15             cin>>num[i];
    16         }
    17         dp1[1]=num[1]%m;
    18         dp2[1]=(2*(num[1]-1)+1)%m;
    19         for(i=2;i<=n;i++){
    20             dp1[i]=(2*dp1[i-1]+num[i])%m;
    21             if(num[i]==1){
    22                 dp2[i]=dp1[i];
    23             }
    24             else{
    25                 dp2[i]=(2*dp1[i-1]+2*num[i]+dp2[i-1])%m;
    26             }
    27         }
    28         cout<<dp2[n]<<endl;
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    linux查看CPU性能及工作状态的指令mpstat,vmstat,iostat,sar,top
    Linux vmstat命令实战详解
    dstat 性能监测工具
    sysstat 工具
    Linux命令详解----iostat
    Linux CPU实时监控mpstat命令详解
    Linux Top 命令解析 比较详细
    Linux统计/监控工具SAR详细介绍
    ubuntu 添加用户到已存在的组
    Ubuntu 14.04 使用速度极快的Genymotion 取代蜗牛速度的原生AVD模拟器
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4299863.html
Copyright © 2011-2022 走看看