poj 3601Tower of Hanoi

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3601Tower of Hanoi相关的知识,希望对你有一定的参考价值。

Tower of Hanoi
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 1895   Accepted: 646

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

 
从别人的博客学习的

解题报告:
假设有n种盘子,x[i]为第i种盘子的数目, 0 <= i <= n - 1.
我们先计算出相同的盘子不考虑顺寻的情况,记作a[i],表示有i种盘子所需的步骤数目(不考虑第i种顺序)。
容易知道,a[0] = x[0],只有一种时,直接把这种的所有盘子移动到目标轴上。
a[i] = 2 * a[i - 1] x[i].
说明:如果从A到C轴,借助B轴,i种盘子,要先把i - 1种移动到 B, 需要a[i - 1],然后把第i种移动到C,需要x[i],然后再把i - 1种从B移动到C,需要a[i - 1]
所以得到a[i] = 2 * a[i - 1] x[i].

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

 

题意:Hanoi塔问题,只不过有一些盘子的大小是一样的,所有的盘子转移到另一个塔,相同大小的盘子要保证原本的顺序不变,问你最少的步骤数目,结果对m取余。

输入n,m,n表示有几种盘子大小,m表示结果将要取余的数。

第二行输入n个数,表示盘子从小到大每种盘子大小的个数。如{1,3,2}表示大小为1的盘子有1个,大小为2的盘子有3个,大小为3的盘子有2个。

 

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int i,j,n,m,a[105],b[105],x[105];
 7     while(~scanf("%d%d",&n,&m))
 8     {
 9         for(i=0; i<n; i++)
10             scanf("%d",&x[i]);
11         a[0]=x[0];
12         for(i=1; i<n; i++)
13             a[i]=(a[i-1]*2+x[i])%m;
14         b[0]=2*x[0]-1;
15         for(i=1; i<n; i++)
16         {
17             if(x[i]==1)
18                 b[i]=a[i];
19             else
20                 b[i]=(2*a[i-1]+2*x[i]+b[i-1])%m;
21         }
22         printf("%d\n",b[n-1]);
23     }
24     return 0;
25 }

 

以上是关于poj 3601Tower of Hanoi的主要内容,如果未能解决你的问题,请参考以下文章

poj 1958 Strange Towers of Hanoi

One usage of recurison: the tower of Hanoi

UVA The Tower of Babylon

uva 437 The Tower of Babylon

UVa 437 The Tower of Babylon(动态规划)

Uva437 The Tower of Babylon