POJ 3572 Hanoi Towers (YY + 推公式)


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);
    }
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值