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三个柱子上,只需要把所有的都移到相应位置。从最上面开始判断,直到到开始的那个就可以了。

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

Towers of Hanoi Strike Back (URAL 2029)的更多相关文章

  1. Strange Towers of Hanoi POJ - 1958(递推)

    题意:就是让你求出4个塔的汉诺塔的最小移动步数,(1 <= n <= 12) 那么我们知道3个塔的汉诺塔问题的解为:d[n] = 2*d[n-1] + 1 ,可以解释为把n-1个圆盘移动到 ...

  2. ural 2029 Towers of Hanoi Strike Back (数学找规律)

    ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...

  3. HDU100题简要题解(2020~2029)

    HDU2020 绝对值排序 题目链接 Problem Description 输入n(n<=100)个整数,按照绝对值从大到小排序后输出.题目保证对于每一个测试实例,所有的数的绝对值都不相等. ...

  4. Hanoi双塔问题(递推)

    Hanoi双塔问题 时间限制: 1 Sec  内存限制: 128 MB提交: 10  解决: 4[提交][状态][讨论版][命题人:外部导入] 题目描述 给定A,B,C三根足够长的细柱,在A柱上放有2 ...

  5. Dean and Schedule (URAL 2026)

    Problem A new academic year approaches, and the dean must make a schedule of classes for first-year ...

  6. Scarily interesting! (URAL - 2021)

    Problem This year at Monsters University it is decided to arrange Scare Games. At the Games all camp ...

  7. POJ1958 Strange Towers of Hanoi [递推]

    题目传送门 Strange Towers of Hanoi Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3117   Ac ...

  8. zoj 2338 The Towers of Hanoi Revisited

    The Towers of Hanoi Revisited Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge You all mus ...

  9. The Towers of Hanoi Revisited---(多柱汉诺塔)

    Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...

随机推荐

  1. Django的URLconf

    URL 概要 我们要在Django项目中为应用程序设计URL,我们可以创建一个名为URLconf(通常为urls.py)的Python模块.这个模块是纯Python代码,是一个简单的正则表达式到Pyt ...

  2. 论文阅读:《Bag of Tricks for Efficient Text Classification》

    论文阅读:<Bag of Tricks for Efficient Text Classification> 2018-04-25 11:22:29 卓寿杰_SoulJoy 阅读数 954 ...

  3. element-ui组件dialog遇到form

    Vue.js似乎成了一种潮流. UI框架element-ui也跟着成了一种潮流,不过得承认,至少我个人还是非常认可的,element-ui做的是真不错. 用到element-ui,那么在dialog中 ...

  4. linux下mysql数据导入到redis

    自Redis 2.6以上版本起,Redis支持快速大批量导入数据,即Pipe传输.通过将要导入的命令转换为Resp格式,然后通过MySQL的concat()来整理出最终导入的命令集合,以达到快速导入的 ...

  5. git merge 命令的使用

    我们把dev分支的工作成果合并到master分支上: $ git merge dev Updating d46f35e..b17d20e Fast-forward readme.txt | 1 + 1 ...

  6. FLEX AIR 读写安卓本地文件

    1.  目标: 将字节流图片保存在安卓本地路径,如 "/data/mypppd/"下, file = File.documentsDirectory.resolvePath(&qu ...

  7. C# enum枚举知识总结

    C#中除了简单的变量类型外,还提供了三种较为复杂的变量类型,包括枚举.结构和数组.本文主要讲述枚举相关的知识. 枚举类型(也称为枚举),提供了一种有效的方式,来定义可能分配给变量的一组已命名的整数常量 ...

  8. vue组件之间的传值

    vue中组件之间的传值有好几种情况 1.父向子传值 父组件向子组件传值使用props,直接上实例 city.vue是父组件,list.vue是子组件city.vue里定义cities和hotCitie ...

  9. linux环境下安装python3的方法(转)

    Linux 安装python3.7.0   我这里使用的时centos7-mini,centos系统本身默认安装有python2.x,版本x根据不同版本系统有所不同,可通过 python --V 或 ...

  10. 【jekins】安装jdk遇到的坑

    首先我电脑版本为win10 64 在安装JDK时先安装了jdk包,安装路径为D:\Java\jdk1.8.0_171,装完jdk后,自动安装jre,我将其安装在D:\Java\jre1.8.0_171 ...