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. 第24次Scrum会议(11/12)【欢迎来怼】

    一.小组信息 队名:欢迎来怼 小组成员 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,阚博文 小组照片 二.开会信息 时间:2017/11/12 17:05~17:32,总计27min. 地 ...

  2. idea打断点是灰色的

    点击这个图标,debug的断点就是灰色的,debug功能被禁用

  3. Daily Scrum2 11.4

    昨天的任务大家都已经完成,daily scrum记录的是当日已经完成的任务. 今日任务列表: 杨伊:完成团队作业之软件测评的功能部分 徐钧鸿:CodingCook的model和helper部分 张艺: ...

  4. 第一节 Linux系统简介

    一.Linux定义 Linux 是一个操作系统,就像你多少已经了解的 Windows(xp,7,8)和 Max OS. 操作系统在整个计算机系统中的角色: Linux 是系统调用和内核那两层,直观的来 ...

  5. C++:类中两个易被忽略的默认函数

    C++的自定义类中有六个默认的函数,即如果用户没有显式定义这些函数时,C++编译器会类中生成这些函数的默认形式.除了大家所熟知的构造函数.拷贝构造函数.赋值函数和析构函数外,C++为自定义类 还提供了 ...

  6. web07-jdbcBookStore

    新建web项目,名字 新建servlet,名字CreateDBServlet 内容为: ---- 配置web.xml 数据库的URL.driveclass.user.passWord都写在web.xm ...

  7. Task 4.5 求二维数组中的最大连通子数组之和

    任务:输入一个二维整形数组,数组里有正数也有负数. 求所有子数组的和的最大值.要求时间复杂度为O(n). 1.设计思想:因为用之前的解决子数组最大和的问题的思路一直没能解决这个问题,后来看到同学使用将 ...

  8. Matlab图像匹配问题

    已知一个任意形状,查找在大图像中最接近的形状位置. 输入:一个小图形状和一张大图 输出:最接近的形状在大图中的位置 假设: (1)已知形状与目标形状有一定的形变. (2)形状与大图像均为二值图像,图中 ...

  9. 微信小程序之Flex布局

    微信小程序页面布局方式采用的是Flex布局.Flex布局,是W3c在2009年提出的一种新的方案,可以简便,完整,响应式的实现各种页面布局.Flex布局提供了元素在容器中的对齐,方向以及顺序,甚至他们 ...

  10. angularJS1笔记-(12)-自定义指令(compile/link)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...