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. excel中如何将时间戳转换为日期格式

    https://www.cnblogs.com/xueluozhangxin/p/5868225.html =TEXT((B2/1000+8*3600)/86400+70*365+19,"y ...

  2. 【探路者】Postmortem会议(“事后诸葛亮”会议)

    [探路者]Postmortem会议(“事后诸葛亮”会议) 整理:米赫 设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的贪吃蛇游戏主要将完成一个 ...

  3. vs2013+python+ cocos2d-x-3.3rc0环境搭建

    1.vs2013安装一路next,安装即可,时间1~2个小时 2.解压cocos2d-x-3.3rc0   build文件夹里会有名为  cocos2d-win32.vc2012的sln文件  打开  ...

  4. Java Head First & 多态

    package com.cwcec.tag; class Fruit { } class Apple extends Fruit{} class Animal { public Fruit eat(F ...

  5. grep文本处理工具

    grep是一款文本过滤工具,基于正则表达式进行模式匹配sed:stream editor 流编辑器awk:linux上实现为gawk,文本报告生成器(格式化文本)文本搜索工具,根据用户指定的模式,对目 ...

  6. Beta阶段团队项目开发篇章4

    例会时间 2016.12.11 例会照片 个人工作 上阶段任务验收 删除robot功能较为简单,基本实现,但是一直Question版块任务量过大,考虑到最近要进行编译课设第二次测试,为该任务再分配一段 ...

  7. 安装virtualbox

    1.安装virtualbox https://www.virtualbox.org/ 2.安装centos6.6 3.配置网络 右键-->网络-->网卡2-->host-only c ...

  8. soap 简单的例子

    首先确保你的soap模块开启 客户端代码 <?php try { $client = new SoapClient(null, array('location' =>"http: ...

  9. VNC Server (Ubuntu 16.04.3 GNOME)

    1. 安装VNC服务 sudo apt-get install vnc4server -y 2. 启动VNC服务 vncserver :1 3. 此时客户端连上后你会发现灰屏,原因出在~/.vnc/x ...

  10. java 实用类

                                                java  实用类 1.File类为管理文件和目录提供了方法,其对象表示一个文件或者目录.它提供了若干方法对文件 ...