前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Towers of Hanoi Strike Back (URAL 2029)

Towers of Hanoi Strike Back (URAL 2029)

作者头像
Lokinli
发布2023-03-09 16:19:29
3080
发布2023-03-09 16:19:29
举报
文章被收录于专栏:以终为始以终为始

Problem

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?

Input

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.

Output

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.

Example

input

output

1 A

0

4 BBBB

15

7 BCCBABC

95

题解:读懂题意就蛮好做的了,就是汉诺塔的一个变形,让字母移到对应的A、B、C三个柱子上,只需要把所有的都移到相应位置。从最上面开始判断,直到到开始的那个就可以了。

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem
  • Input
  • Output
  • Example
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档