POJ 3572 Hanoi Towers (YY + 推公式)
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 648 | Accepted: 327 | Special Judge |
Description
The “Hanoi Towers” puzzle consists of three pegs (that we will name A, B, and C) with n disks of different diameters stacked onto the pegs. Initially all disks are stacked onto peg A with the smallest disk at the top and the largest one at the bottom, so that they form a conical shape on peg A.
A valid move in the puzzle is moving one disk from the top of one (source) peg to the top of the other (destination) peg, with a constraint that a disk can be placed only onto an empty destination peg or onto a disk of a larger diameter. We denote a move with two capital letters — the first letter denotes the source disk, and the second letter denotes the destination disk. For example, AB is a move from disk A to disk B.
The puzzle is considered solved when all the disks are stacked onto either peg B (with pegs A and C empty) or onto peg C (with pegs A and B empty). We will solve this puzzle with the following algorithm.
All six potential moves in the game (AB, AC, BA, BC, CA, and CB) are arranged into a list. The order of moves in this list defines our strategy. We always make the first valid move from this list with an additional constraint that we never move the same disk twice in a row.
It can be proven that this algorithm always solves the puzzle. Your problem is to find the number of moves it takes for this algorithm to solve the puzzle using a given strategy.
Input
The input file contains two lines. The first line consists of a single integer number n (1 ≤ n ≤ 30) — the number of disks in the puzzle. The second line contains descriptions of six moves separated by spaces — the strategy that is used to solve the puzzle.
Output
Write to the output file the number of moves it takes to solve the puzzle. This number will not exceed 1018.
Sample Input
#1 | 3 AB BC CA BA CB AC |
---|---|
#2 | 2 AB BA CA BC CB AC |
Sample Output
#1 | 7 |
---|---|
#2 | 5 |
Source
題目連結:http://poj.org/problem?id=3572
題目大意:n個盤子的漢諾塔遊戲,移動策略已經給出,同一個盤子不能連續被移動兩次,每次都按策略中最先可行的指令進行操作,問按照給定策略完成遊戲的操作次數,完成是移動到B或C都可以
題目分析:記錄一下每個策略的移動指令,去掉那些將一個盤子連續移動超過一次的指令,我是直接通過3個盤子和2個盤子的情況yy的,和普通漢諾塔問題類似,考慮最小的盤子的移動方案,因為全都移到B和移到C本質相同,所以只考慮移到C的情況,可以得到三種情況
1.A -> C;C -> B;B -> A;A -> C這就是普通3層漢諾塔的最優解 2^n - 1 (2的時候3步,3的時候需要7步)
2.A -> C;C -> B;B -> C;C -> B;B -> C 推出來是3^(n-1) (2的時候3步,可以看出2的時候小盤子只移動兩次即可和方案1相同,3的時候需要9步)
3.A -> C;C -> A;A -> C;... 推出來是2 * 3^(n-1) - 1 (2的時候5步,3的時候需要17步)
分情況討論即可
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
int n, mp[55];
char s[10];
int main()
{
while(scanf("%d", &n) != EOF)
{
memset(mp, 0, sizeof(mp));
for(int i = 0; i < 6; i++)
{
scanf("%s", s);
int a = s[0] - 'A' + 1, b = s[1] - 'A' + 1;
if(!mp[a])
mp[a] = b;
}
if(mp[2] != 1 && mp[3] != 1)
printf("%lld\n", (ll)(pow(3ll, n - 1)));
else if(mp[mp[1]] == 1)
printf("%lld\n", 2ll * (ll)pow(3ll, n - 1) - 1ll);
else
printf("%lld\n", (ll)(pow(2ll, n)) - 1ll);
}
}
相關文章
- Towers of Hanoi題解
- yy
- CF85E Guard Towers
- 雙色Hanoi塔問題
- 二項式定理公式推導公式
- 三角函式公式推導函式公式
- 四元數旋轉公式推導公式
- LOAM原始碼分析附公式推導原始碼公式
- FlashAttention逐代解析與公式推導公式
- 四元數的旋轉公式推導公式
- Diffusion系列 - DDIM 公式推導 + 程式碼 -(三)公式
- 高斯公式對高斯定理的推導公式
- poj 2031
- poj 3461
- 直播江湖:陌陌向左,YY向右
- 核化線性降維中部分公式的推導公式
- 線性迴歸模型公式推導完整簡潔版模型公式
- jzoj 6797. 【2014廣州市選day2】hanoi
- poj 3278 BFS
- POJ 2975 Nim
- poj3417
- POJ 1089 Intervals
- POJ 3414 Pots
- 利用MATLAB逆推王者榮耀物抗—免傷率公式Matlab公式
- YY財報圖解:2018年Q4 YY淨利潤7.404億元 同比下滑7.6%圖解
- win10 yy麥克風怎麼設定 win10yy麥克風如何設定Win10
- 小知識系列(3):Hanoi塔(漢諾塔,河內塔)
- 歡聚集團財報:BIGO進、YY退Go
- 科技創新需“計之深遠”YY
- 統一場理論公式推導和筆記——part5公式筆記
- 統一場理論公式推導和筆記——part6公式筆記
- 擴充套件歐幾里得演算法公式快速推導套件演算法公式
- 總結:生成函式(斐波那契通項公式推導)函式公式
- Network(POJ-1144)
- POJ 2553 The Bottom of a Graph
- POJ 1861 Network (Kruskal)
- Apple Catching POJ - 2385APP
- POJ 1442 Black Box
- POJ 2799 IP Networks