HDU 4260 (The End of the World-hanoi tower moves from the middle state)

Source: Internet
Author: User

The End of the worldTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 646 Accepted Submission (s): 308


Problem Descriptionlegend says that there was a group of monks who was 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 is stacked on one of the pegs, and ordered from largest (on the bottom) to smallest (on th e top). The object is to move this stack of disks to another peg, subject-to-rules:1) you can only move one disk at a time, a nd 2) cannot move a disk onto a peg if that peg already have a smaller disk on it.
The monks believe that's when they finish and the world would end. Suppose know how far they ' ve gotten. Assuming that the monks is pursuing the most efficient solution, what much time does the world has left?
Inputthere'll is several test cases in the input. Each of the test case would consist of a string of length 1 to a. This string would contain only 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 Smalles t disk, and so on, until the last character, which tells the position of the largest disk. The character would be A, BOr C, indicating which peg the disk is currently on. Assume that the monks ' overall goal are to move the disks from peg Ato Peg B, and that the input represents a legitimate position in the optimal solution. The input would end with a line with a single capital X.
Outputfor each test case, print a single number on its own line indicating the number of moves remaining until the given T Owers of Hanoi problem is solved. Output no extra spaces, and do not separate answers with blank lines. All possible inputs yield answers which would fit in a signed 64-bit integer.
Sample Input
Aaabbbx

Sample Output
70

Sourcethe University of Chicago Invitational programming Contest 2012
recommendliuyiding | 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
The Hanoi Tower gives an intermediate state. The minimum number of steps to finally state.

Recursion.

Each calc (n,goal) represents the number of steps to move the first n small plates to goal.

Can be recursively decomposed into

{

1. If the plate n is on the goal, skip

2. If the plate n is not on the goal,

1) Move the front n-1 plate to the Free Goal2 calc (n-1,goal2)

2) Move plate N to Goal 1 steps

3) Move n-1 plate from Goal2 to Goal 2^ (N-1)-1 steps

}


#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) {R Eturn (A*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (A-b+llabs (a)/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;}






HDU 4260 (The End of the World-hanoi tower moves from the middle state)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.