您的位置:首页 > 其它

Hanoi Towers - POJ 3572 dp

2014-12-11 22:26 295 查看
Hanoi Towers

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 398Accepted: 198Special 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


题意:对于一个汉诺塔,每次按照给定的顺序,找第一个可以移动的方式去移动。要求不能大的下面放小的,且不能连续两次移动同一个盘子。

思路:这个。。。我是部分模拟找规律的。每次我们只用关系AB、AC BA、BC CA、BC 这三对的顺序即可。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll a[40],b[40],c[40];
int num[10];
char s[10];
int main()
{
    int n,i,j,k;
    a[1]=b[1]=c[1]=1;
    for(i=2;i<=30;i++)
    {
        a[i]=a[i-1]*2+1;
        c[i]=c[i-1]*3+2;
        b[i]=b[i-1]+c[i-1]+1;
    }
    scanf("%d",&n);
    for(i=1;i<=6;i++)
    {
        scanf("%s",s);
        k=(s[0]-'A')*4+s[1]-'A';
        num[k]=i;
    }
    if(num[1]<num[2])
    {
        if(num[4]<num[6])
          printf("%I64d\n",c
);
        else if(num[9]<num[8])
          printf("%I64d\n",b
);
        else
          printf("%I64d\n",a
);
    }
    else
    {
        if(num[8]<num[9])
          printf("%I64d\n",c
);
        else if(num[6]<num[4])
          printf("%I64d\n",b
);
        else
          printf("%I64d\n",a
);
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: