URAL 2029 Towers of Hanoi Strike back Hanoi, from the initial state to any given state required number of times

Source: Internet
Author: User
Tags pow time limit cmath

F- towers of Hanoi Strike back time Limit: 1000ms      Memory Limit: 65536kb      64bit IO Format: %i64d &%i64u Submit   status  practice  URAL 2029

Description the Tower of Hanoi puzzle is invented by French Mathematicianédouard Lucas in the second half of the 19th c Entury. Here are its formulation. There is three rods, denoted by the letters A, B, and C, and  n disks of different integer sizes from 1 to&nbsp ;  N. Initially the disks is stacked  in ascending order of size on rod a, the smallest at the top, thus making A con ical shape. Each move consists of taking the upper disk  from one of the rods and placing it in top of the stack at another rod, With the following condition Satisfied:no disk is placed on  top of a smaller disk. The objective of the puzzle is to move the entire stack to Rod B in the smallest possible number of moves. The auxiliary rod C can be used in the process. The state of the rods at each time can is described by a string of  n letters A, B, and c:the letter at Positio n  I denotes the rod where the disk of size   I is at that TIme. For example, the initial was given by the string containing letters a only, and the final state was described by The string consisting of letters b. The converse is also true:any such string uniquely describes a valid state of the rods, because the order of disks on a R OD is uniquely defined by their size. Imagine that is required to pass from the initial state, where all the disks is on rod a, to some prescribed St Ate. What's the  smallest number of moves in which this can be done?

Input The first line contains an integer n (1≤n≤50). The second line is given n uppercase 中文版 letters A, B, C, which describe the final state.

Output If It is impossible to obtain the final state from the initial state, output "1" (without quotation marks). Otherwise, output the minimum number of moves. It is guaranteed this, if there is a answer, it does not exceed 10 18.

Sample Input

input Output
1
A
0
4
BBBB
15
7
BCCBABC
95

Source

UESTC Summer Training #17 Div.2

URAL 2029


My Solution

Hanoi, get the O (n) algorithm from the initial state to the number of times the state needs to be given, and draw the conclusion ☺☺

For example, to get BCCBABC.

For the original AAAAAAA

For the first time res = ' A ', then for the given state from large to small start sweep, is currently C so the 7th a becomes c, ans + = 2^ (7-1), then res = ' B ', that is, the remaining moved to B,

Then the second one needs to be on B, and already on B, so do not care, continue to visit the next

Then a, this time the size of the disk on B, so move to a, ans + = 2^ (5-1), and then res = ' C ', the rest of the remaining all moved to the third pillar ' C '.

It's going to be fine.

Compare the column on which the current disk is needed and the current one on that pillar, if it happens to be directly on the next plate, if not then move the disk past ans + 2^i (0 <= i < n), the remaining disks are all moved to the third pillar

Put aaaaaaa. From right to left, all to state, you get the answer.


Also note the point

Use 1<<i to get 2^i times, if I very large, such as 50, this time the integer overflow, with (LL) (1<<50) also can not reach the effect, or to get the number of data loss of the integer size,

Then I used the Cmath pow, ans + = (LL) POW (2, i), and I could get a 64-bit integer.


Complexity O (N)


#include <iostream> #include <cstdio> #include <cmath> using namespace std;
typedef long Long LL;

const int MAXN = 1e2 + 8;

Char VAL[MAXN];
    int main () {#ifdef LOCAL freopen ("A.txt", "R", stdin);
    Freopen ("B.txt", "w", stdout);
    int T = 4;
    while (t--) {#endif//LOCAL LL n, ans = 0;
    char res = ' A ';
    scanf ("%i64d", &n);

    scanf ("%s", Val);
            for (ll i = n-1; I >= 0; i--) {if (res! = Val[i]) {ans + = (ll) Pow (2, i);
            cout<<ans<<endl;
                if (res = = ' A ') {if (val[i] = = ' B ') res = ' C ';
            else res = ' B ';
                } else if (res = = ' B ') {if (val[i] = = ' A ') res = ' C ';
            else res = ' A ';
                } else if (res = = ' C ') {if (val[i] = = ' A ') res = ' B ';
            else res = ' A ';
    }} if (Ans < 0 | | ans > 1e18) break; } if (Ans < 0 | | ans> 1e18) printf ("-1");

    printf ("%i64d", ans);
    #ifdef LOCAL printf ("\ n");
} #endif//LOCAL return 0; }

Thank you!

------from Prolights

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.