Towers of Hanoi Strike Back (URAL 2029)
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 |
0 |
4 |
15 |
7 |
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)的更多相关文章
- Strange Towers of Hanoi POJ - 1958(递推)
题意:就是让你求出4个塔的汉诺塔的最小移动步数,(1 <= n <= 12) 那么我们知道3个塔的汉诺塔问题的解为:d[n] = 2*d[n-1] + 1 ,可以解释为把n-1个圆盘移动到 ...
- ural 2029 Towers of Hanoi Strike Back (数学找规律)
ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...
- HDU100题简要题解(2020~2029)
HDU2020 绝对值排序 题目链接 Problem Description 输入n(n<=100)个整数,按照绝对值从大到小排序后输出.题目保证对于每一个测试实例,所有的数的绝对值都不相等. ...
- Hanoi双塔问题(递推)
Hanoi双塔问题 时间限制: 1 Sec 内存限制: 128 MB提交: 10 解决: 4[提交][状态][讨论版][命题人:外部导入] 题目描述 给定A,B,C三根足够长的细柱,在A柱上放有2 ...
- Dean and Schedule (URAL 2026)
Problem A new academic year approaches, and the dean must make a schedule of classes for first-year ...
- Scarily interesting! (URAL - 2021)
Problem This year at Monsters University it is decided to arrange Scare Games. At the Games all camp ...
- POJ1958 Strange Towers of Hanoi [递推]
题目传送门 Strange Towers of Hanoi Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3117 Ac ...
- 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 ...
- The Towers of Hanoi Revisited---(多柱汉诺塔)
Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...
随机推荐
- Python学习笔记:格式化输出
%d digit%s string%f float程序运用:name = input("please input your name:")age = int(input(" ...
- 请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0ax 2 +bx+c=0 的两个解。
#!/usr/bin/python # 导入math包 import math def quadratic(a, b, c): if not isinstance(a, (int, float))an ...
- 植物大战僵尸:查找植物叠加种植CALL
实验目标:我们都知道植物大战僵尸游戏中植物是不可以叠加种植的,也就是一个格子只能种植一个植物,今天我们将实现一个格子里种植无限多的植物. 我们首先需要找到植物的种植CALL,然后在逐步测试观察功能之间 ...
- Oracle 表分区介绍与使用
什么是表分区 分区表是将大表的数据分成称为分区的许多小的子集,类型有FAT32,NTFST32,NTFS.另外,分区表的种类划分主要有:range,list,和hash分区.划分依据主要是根据其表内部 ...
- pat L2_004
一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的键值: 其左右子树都是二叉搜索树. 所谓二叉搜索 ...
- springcloud eureka注册中心分布式配置
最近在学习springcloud,做下笔记以及记下遇到的坑. 1.建立maven工程,结构很简单,一个启动类和一个配置文件,结构如下图所示 2.启动类代码如下,需要添加注册中心注解:EnableEur ...
- Android应用市场App发布
来自知乎 Android应用市场App发布说到官方渠道,不得不说一些主要的大市场了,如:360.小米.应用宝.91.安卓.百度.豌豆荚.安智.现在我来一一说它们的一些简单特点. 1,360 (1)当天 ...
- DVA-subscriptions
import { routerRedux } from 'dva/router' export default { namespace: 'notice', state: { notices:[], ...
- 如何使用cgdb(一)——窗口切换
cgdb是一个轻量级的基于控制台的多窗口gdb调试界面.除了标准的gdb控制台之外,cgdb还提供了一个分屏视图,可以在执行的时候显示具备语法高亮的源代码.键盘控制是仿照vim设计的,所以vim用户使 ...
- spring boot 的一些高级用法
1 spring boot 项目的创建 参考 https://www.cnblogs.com/PerZhu/p/10708809.html 2 首先我们先把Maven里面的配置完成 <depen ...