ural 2029 Towers of Hanoi Strike Back

链接:http://acm.timus.ru/problem.aspx?space=1&num=2029

题意:汉诺塔问题,给定一串只有(A, B, C)的字符串(A代表在第一根柱子,B代表在第二根柱子,C代表在第三根柱子),从前往后代表盘子的大小,第 i 个字母代表di i 个盘子在某个柱子上。问移动给定字母状态的盘子最少需要多少步。

思路:首先,从后往前看(最大的盘子),如果不在当前柱子上,那么移动到目标柱子需要 2^(n-1) 步,其余的盘子都移动到剩下的柱子上;若目标柱子与当前柱子相同,则不需要移动。当移动到目标柱子,该盘子不需要再考虑,依次类推,就可求出移动步数。

代码:

 #include <climits>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cstdarg>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <memory>
#include <locale>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
#include <string>
#include <complex>
#include <valarray> using namespace std; typedef long long ll;
const int N = ;
ll bit[N];
char s[N]; inline void init() {
bit[] = 1LL;
for(int i = ; i < N; ++i) bit[i] = (bit[i-] * 2LL);
return ;
} int n;
int pos, nxt; void solve(){
pos = nxt = ;
ll ans = 0LL;
for(int i = n-; i > -; --i) {
int k = s[i] - 'A' + ; // A : 1 ; B : 2 ; C : 3;
if(k == nxt) continue;
nxt = - nxt - k, pos = k; // nxt 代表第i个盘子除当前位置柱子和目标柱子,剩下的那根
//printf("nxt = %d pos = %d\n", nxt, pos);
ans += bit[i];
}
printf("%I64d\n", ans);
return ;
} int main()
{
init(); while(~scanf("%d", &n)) {
scanf("%s", s);
solve();
}
return ;
}

ural 2029 Towers of Hanoi Strike Back (数学找规律)的更多相关文章

  1. # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor

    E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...

  2. Towers of Hanoi Strike Back (URAL 2029)

    Problem The Tower of Hanoi puzzle was invented by French mathematician Édouard Lucas in the second h ...

  3. 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight

    题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...

  4. HDU 5914 Triangle 数学找规律

    Triangle 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Description Mr. Frog has n sticks, who ...

  5. Ural 2037. Richness of binary words 打表找规律 构造

    2037. Richness of binary words 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2037 Descripti ...

  6. HDU 1273 漫步森林(数学 找规律)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1273 漫步森林 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. SGU 105 数学找规律

    观察一下序列,每3个数一组,第一个数余1,不能,加第二个数后整除(第二个数本身余2),第三数恰整除.一行代码的事.011011011.... #include<iostream> usin ...

  8. 数学 找规律 Jzzhu and Sequences

    A - Jzzhu and Sequences   Jzzhu has invented a kind of sequences, they meet the following property: ...

  9. UVA 12683 Odd and Even Zeroes(数学—找规律)

    Time Limit: 1000 MS In mathematics, the factorial of a positive integer number n is written as n! an ...

随机推荐

  1. Python文本文件的输入输出操作学习

    Python具有基本的文本文件读写功能.Python的标准库提供有更丰富的读写功能. 文本文件的读写主要通过open()所构建的文件对象来实现. 创建文件对象 我们打开一个文件,并使用一个对象来表示该 ...

  2. fetch err : "Body not allowed for GET or HEAD requests"

    在使用 fetch 的时候 报了  "Body not allowed for GET or HEAD requests" 这个错. 代码如下: 一番google , 找到答案了. ...

  3. Scrum立会报告+燃尽图(Final阶段第四次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2481 项目地址:https://coding.net/u/wuyy694 ...

  4. Scrum立会报告+燃尽图(十二月六日总第三十七次):程序功能逻辑优化

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  5. (第九周)Beta-1阶段成员贡献分

    项目名:食物链教学工具 组名:奋斗吧兄弟 组长:黄兴 组员:李俞寰.杜桥.栾骄阳.王东涵 个人贡献分=基础分+表现分 基础分=5*5*0.5/5=2.5 成员得分如下: 成员 基础分 表现分 个人贡献 ...

  6. ### Error building SqlSession.

    org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession.### The error may e ...

  7. Sprint9

    进展:完善设置事件提醒界面,增加调用手机铃声部分,以及是否选择振动,以及可以添加事件进行保存.

  8. mysql密码忘记解决方案

    方法:在忘记root密码的时候,可以这样 以windows为例: 1. 关闭正在运行的MySQL服务. 2. 打开DOS窗口,转到mysql\bin目录. 3. 输入mysqld --skip-gra ...

  9. CodeM Qualifying Match Q1

    问题描述: 具体地说,就是在第二段音频中找到一个长度和第一段音频相等且是连续的子序列,使得它们的 difference 最小.两段等长音频的 difference 定义为: difference = ...

  10. Fastqc使用说明

    用FastQC检查二代测序原始数据的质量 2013-01-28 21:28:10|  分类: Bioinformatics |  标签:bioinformatics  deep-seq   |举报 | ...