HDU 4260(The End of The World-Hanoi塔从中间状态移动)

The End of The World

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 646    Accepted Submission(s): 308

Problem Description

Legend says that there is a group of monks who are solving a large Towers of Hanoi puzzle. The Towers of Hanoi is a well-known puzzle, consisting of three pegs, with a stack of disks, each a different size. At the start, all of the
disks are stacked on one of the pegs, and ordered from largest (on the bottom) to smallest (on the top). The object is to move this stack of disks to another peg, subject to two rules: 1) you can only move one disk at a time, and 2) you cannot move a disk
onto a peg if that peg already has a smaller disk on it.

The monks believe that when they finish, the world will end. Suppose you know how far they’ve gotten. Assuming that the monks are pursuing the most efficient solution, how much time does the world have left?

Input

There will be several test cases in the input. Each test case will consist of a string of length 1 to 63, on a single line. This string will contain only (capital)
As, Bs and Cs. The length of the string indicates the number of disks, and each character indicates the position of one disk. The first character tells the position of the smallest disk, the second character
tells the position of the second smallest disk, and so on, until the last character, which tells the position of the largest disk. The character will be
A, B or C, indicating which peg the disk is currently on. You may assume that the monks’ overall goal is to move the disks from peg
A to peg B, and that the input represents a legitimate position in the optimal solution. The input will end with a line with a single capital
X.

Output

For each test case, print a single number on its own line indicating the number of moves remaining until the given Towers of Hanoi problem is solved. Output no extra spaces, and do not separate answers with blank lines. All possible
inputs yield answers which will fit in a signed 64-bit integer.

Sample Input

AAA
BBB
X

Sample Output

7
0

Source

The University of Chicago Invitational Programming Contest 2012

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  pid=4257" target="_blank">4257 

pid=4261" target="_blank">4261 4262 

pid=5390" target="_blank">5390 

pid=5389" target="_blank">5389

Hanoi塔给一个中间状态。求到终于状态最小步数。

递归。

每次calc(n,goal) 表示把前n小的盘子全移动到goal的步数。

可递归分解为

{

1.若盘子n本来就在goal上,跳过

2.若盘子n不在goal上,

1)把前n-1个盘子移动到空余的goal2上    calc(n-1,goal2)

2)移动盘子n到goal    1步

3)把n-1个盘子从goal2移到goal上   2^(n-1)-1步

}

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (63+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

char s[MAXN];
int n;

ll calc(int n,int goal) {
	ForD(i,n)
	{
		if (s[i]!=goal)
		{
			int goal2;
			if (s[i] + goal == 'A' + 'B' ) goal2='C';
			if (s[i] + goal == 'A' + 'C' ) goal2='B';
			if (s[i] + goal == 'C' + 'B' ) goal2='A';

			return calc(i-1,goal2) + (1LL<<i-1);
		}
	}
	return 0;

}

int main()
{
//	freopen("hdu4260.in","r",stdin);

	while(scanf("%s",s+1)==1)
	{
		if (s[1]=='X') break;
		n=strlen(s+1);
		cout<<calc(n,'B')<<endl;
	} 

	return 0;
}
时间: 2025-01-07 11:14:13

HDU 4260(The End of The World-Hanoi塔从中间状态移动)的相关文章

栈和递归之Hanoi塔

hanoi塔 代码 <pre name="code" class="cpp">#include<stdio.h> void move(char x,int n,char y) { static int k=1; printf("Step %d : %d from %c >>->> to %c \n",k++,n,x,y); } void hanoi(int n,char A,char B,char

用递归实现 hanoi塔

// hanoi.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" void move(int n,char moveStart,char moveEnd){ printf("move %d from %c to %c\n",n,moveStart,moveEnd); } void hanoi(int n,char moveStart,char moveTemp,char moveEnd){ if (n == 1) { move

n阶Hanoi塔问题

假设有三个命名为x.y.z的塔座,在塔座x上插有n个直径大小各不相同.依小到大编号为1.2...n的圆盘,要求将x塔座上的n个圆盘移至z上,并仍按同样的顺序叠排,圆盘移动时应遵守下列规则: (1)每次只能移动一个圆盘: (2)圆盘可插在x.y和z中任何一个塔座上: (3)任何时刻都不能将一个较大的圆盘压在较小的圆盘上面. 如下图所示为3阶Hanoi塔问题的初始状态. 完整代码: 1 #include <stdio.h> 2 3 void Hanoi(int n, char x, char y,

Demo:Hanoi塔问题到底是如何运行的?

Hanoi问题以递归移动柱上碟子的方法解决问题,但各柱上碟片的变化到底是如何变化的? 下面的程序给出了演示效果,便于程序员理解. #include <stdio.h> #define N 6 int Vals[3][N+1]; int movecnt = 0; void out() { for(int i = 0; i < 3; i++) { for(int j = 1; j <= N; j++) if (j <= Vals[i][0]) printf("%d &q

Hanoi塔问题

传说在古代印度的贝拿勒斯圣庙里,安放了一块黄铜板,板上插了三根宝石柱(不妨设A.B.C柱),在A宝石柱上,自上而下按由小到大的顺序串有64个金盘.要求将A柱子上的64个金盘按照下面的规则移到C柱上. 规则: ①一次只能移一个盘子: ②盘子只能在三个柱子上存放: ③任何时候大盘不能放在小盘上面. 任务:输入正整数n(A柱上的盘子数),输出移动到C柱上的移动过程. 分析 :设A上有n个盘子. 1. n=1时,则将圆盘从A直接移动到C. 2. n=2时,则: ①将A上的1个圆盘移到B上: ②再将A上的

POJ1958 Strange Towers of Hanoi --- 递推【n盘m塔Hanoi塔问题】

POJ1958 Strange Towers of Hanoi Sol: n盘4塔问题可以分为3步: 1.以4塔模式移走i个盘. 2.以3塔模式将剩余n-i个盘移至第4塔. 3.以4塔模式将第一步中的i个盘移至第4塔. 我们用\(d[i]\)表示在3塔模式下移i个盘的最小步数,\(f[i]\)表示在4塔模式下移i个盘的最小步数. 递推式:\(f[i]=\min_{1\leq j < i}(2*f[j]+d[i-j])\) EX 本题可以拓展至n盘m塔问题. \(f[i][j]\)表示在i塔模式下

Hanoi塔

2017年07月29日 由<数据结构>(c语言版)[严蔚敏  吴伟民 编著]page54- page58 启发得到:根据递归原理.当n=1时,只需移动1次.当n=2时,需要移动3次.当n=3时.可以利用上题结论.经过我在公交车上的思考,可以得到递推公式.本次增加一层所需的移动量,是之前(增加一层之前)的所需的移动量的2倍,还没完,再加上1.这个使用语言表达起来还真有点繁琐.公式表达如下:(目前不知道怎么利用latex等编辑器的方式输入公式)最后可以得到当为n时的所需的移动次数的公式,结果看起来

Hanoi塔问题(递归)

#include<iostream> #include<cstdio> using namespace std; int sum; int f(int n,char a,char b,char c){ if(n == 1){printf("1 from a to b\n");sum++;return sum;} else { f(n-1,a,c,b); printf("%d from %c to %c\n",n,a,c);sum++; f(n

HDU 1176 免费馅饼(简单DP,数塔变形)

Problem Description: 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内.馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接.但由于小径两侧都不能站人,所以他只能在小径上接.由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼.现在给