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. Daily scrum 2015.10.19

    这周是我们团队项目开始的第一周.我们的团队项目是“北航社团平台”,一个致力于打造北航社团资讯整合.社团工作服务与社团商品销售的一站式网络平台. 一.会议内容 1. 总体分工,江昊同学担任项目PM,王若 ...

  2. 团队计划backlog---DayTwo

    任务索引卡(Two): 1.  季方:实现界面跳转,数据库相关数据的显示 的测试: 2.  司宇航:添加部分团队博客,并测试: 3.  王金萱.马佳慧:学习爬虫的相关内容,为将来统计博客部分做准备: ...

  3. 对网络助手的NABCD分析心得

    Sunny--Code团队::刘中睿,杜晓松,郑成 我们小组这次做的软件名字叫为校园网络助手.在大学学习的同学都知道学校里面有着内网与外网两种,并且有着流量限制,所以我们设计出来了这项软件,它主要有着 ...

  4. Mininet-wifi安装和简单使用

    Mininet-WIFI安装和简单使用 安装 git clone https://github.com/intrig-unicamp/mininet-wifi cd mininet-wifi sudo ...

  5. 四则运算之GUI

    四则运算之GUI Coding克隆地址:https://git.coding.net/lvgx/pair_programming.git   目录: 一.前言 二.计划时间——PSP 三.接口设计 四 ...

  6. 四则运算web版

    1)在文章开头给出Coding.Net项目地址.(1') https://git.coding.net/meiyoupiqidefan/jieduizuoye.git url测试地址:http://3 ...

  7. Java 线程结束 & 守护线程

    /* 停止线程: 1,stop方法. 2,run方法结束. 怎么控制线程的任务结束呢? 任务中都会有循环结构,只要控制住循环就可以结束任务. 控制循环通常就用定义标记来完成. 但是如果线程处于了冻结状 ...

  8. UTC时间与北京时间

    经常混淆于此,特地研究了一下,记录在此以备忘. 整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal ...

  9. Linux操作系统(三)

    文件系统: boot sector:记录引导文件 sb: super block 每个文件系统只有一个sb,其余black group均是它的备份 读写文件会进入到inode operation vf ...

  10. 补发9.28“天天向上”团队Scrum站立会议

    组长:王森 组员:张金生 张政 栾骄阳 时间:2016.09.28 地点:612寝 组员 已完成 未完成 王森 搭建初步原型 完善原型 张金生 更新UI设计 生成应用界面原型 张政 初步设计框架图 完 ...