The Tower of Hanoi puzzle was invented by French mathematician Édouard Lucas in the second half of the 19th century. Here is its formulation.
There are three rods, denoted by the letters A, B, and C, and n disks of different integer sizes from 1 to n. Initially the disks are stacked in ascending order of size on rod A, the smallest at the top, thus making a conical shape. Each move consists of taking the upper disk from one of the rods and placing it on top of the stack at another rod, with the following condition satisfied: no disk may be placed on top of a smaller disk. The objective of the puzzle is to move the entire stack to rod B in the smallest possible number of moves. The auxiliary rod C can be used in the process.
The state of the rods at each time can be described by a string of n letters A, B, and C: the letter at position i denotes the rod where the disk of size i is at that time. For example, the initial state is given by the string containing letters A only, and the final state is described by the string consisting of letters B. The converse is also true: any such string uniquely describes a valid state of the rods, because the order of disks on a rod is uniquely defined by their size.
Imagine that you are required to pass from the initial state, where all the disks are on rod A, to some prescribed state. What is the smallest number of moves in which this can be done?
The first line contains an integer n (1 ≤ n ≤ 50).
In the second line you are given n uppercase English letters A, B, C, which describe the final state.
If it is impossible to obtain the final state from the initial state, output “-1” (without quotation marks). Otherwise, output the minimum number of moves. It is guaranteed that, if there is an answer, it does not exceed 10 18.
input | output |
---|---|
1 A | 0 |
4 BBBB | 15 |
7 BCCBABC | 95 |
题解:读懂题意就蛮好做的了,就是汉诺塔的一个变形,让字母移到对应的A、B、C三个柱子上,只需要把所有的都移到相应位置。从最上面开始判断,直到到开始的那个就可以了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
ll a[55];
char s[100];
int main()
{
ll n, i;
a[0] = 0;
a[1] = 1;
for(i = 2; i <= 50; i ++) // 汉诺塔公式
{
a[i] = a[i - 1] * 2 + 1;
}
scanf("%lld",&n);
scanf("%s",s+1);
ll x = 1; // 来表示一开始在的位置
ll ans = 0;
for(i = n; i >= 1; i--) // 如果想要由位置1移到位置3,那么2为跳板,位置x更新为跳板
{
if(x==1&&s[i]=='A') continue;
else if(x==1&&s[i]=='B')
{
ans+=a[i-1]+1;
x=3;
}
else if(x==1&&s[i]=='C')
{
ans+=a[i-1]+1;
x=2;
}
else if(x==2&&s[i]=='A')
{
ans+=a[i-1]+1;
x=3;
}
else if(x==2&&s[i]=='B')continue;
else if(x==2&&s[i]=='C')
{
ans+=a[i-1]+1;
x=1;
}
else if(x==3&&s[i]=='A')
{
ans+=a[i-1]+1;
x=2;
}
else if(x==3&&s[i]=='B')
{
ans+=a[i-1]+1;
x=1;
}
else if(x==3&&s[i]=='C') continue;
}
printf("%lld\n",ans);
return 0;
}