POJ 3572 Hanoi Towers (YY + 推公式)

_TCgogogo_發表於2015-10-08


Hanoi Towers
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

#13 AB BC CA BA CB AC
#22 AB BA CA BC CB AC

Sample Output

#17
#25

Source

Northeastern Europe 2007

題目連結: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);
    }
} 


相關文章