Problem Statement
In the classic Towers of Hanoi problem, there are three pegs, labeled A, B and C, and n discs, of sizes 1 to n. Each disc has a hole in the center, so that it can be placed on a peg. Initially, all the discs are stacked on peg A in order of size, with the smallest disc on top. Only one disc may be moved at a time. In each move, you remove the top disc from one of the pegs, and place it on another peg. A disc may be placed on an empty peg or stacked on top of a larger disc, but it may not be placed on top of a smaller disc.
target is a string consisting of n characters, each of which is either A, B or C. The goal of the game is to make the minimum number of moves so that character i of target is the peg containing the disc of size i + 1. There is only one shortest sequence of moves. Return the state of the game after making moves of these moves, in the same format as target.
Definition
- Class:
- HanoiState
- Method:
- partwayState
- Parameters:
- String, int
- Returns:
- String
- Method signature:
- String partwayState(String target, int moves)
- (be sure your method is public)
Constraints
- target will contain between 1 and 30 characters, inclusive.
- Each character of target will be either 'A', 'B' or 'C'.
- moves will be between 0 and m, inclusive, where the shortest solution to the game takes m moves.
Examples
"CCC"
4
Returns: "BBC"
The full sequence of states is AAA, CAA, CBA, BBA, BBC, ABC, ACC, CCC. After 4 moves the state is BBC.
"AAC"
7
Returns: "AAC"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAA"
0
Returns: "AAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"ABCABCABCABCABCABCABCABCABCABC"
100000000
Returns: "CCCCCCCCBAAAABBBBACBAAAAACBAAA"
"A"
0
Returns: "A"
"B"
0
Returns: "A"
"B"
1
Returns: "B"
"C"
0
Returns: "A"
"C"
1
Returns: "C"
"CCCC"
0
Returns: "AAAA"
"CCCC"
1
Returns: "BAAA"
"CCCC"
2
Returns: "BCAA"
"CCCC"
3
Returns: "CCAA"
"CCCC"
4
Returns: "CCBA"
"CCCC"
5
Returns: "ACBA"
"CCCC"
6
Returns: "ABBA"
"CCCC"
7
Returns: "BBBA"
"CCCC"
8
Returns: "BBBC"
"CCCC"
9
Returns: "CBBC"
"CCCC"
10
Returns: "CABC"
"CCCC"
11
Returns: "AABC"
"CCCC"
12
Returns: "AACC"
"CCCC"
13
Returns: "BACC"
"CCCC"
14
Returns: "BCCC"
"CCCC"
15
Returns: "CCCC"
"CBAC"
0
Returns: "AAAA"
"CBAC"
1
Returns: "BAAA"
"CBAC"
2
Returns: "BCAA"
"CBAC"
3
Returns: "CCAA"
"CBAC"
4
Returns: "CCBA"
"CBAC"
5
Returns: "ACBA"
"CBAC"
6
Returns: "ABBA"
"CBAC"
7
Returns: "BBBA"
"CBAC"
8
Returns: "BBBC"
"CBAC"
9
Returns: "ABBC"
"CBAC"
10
Returns: "ACBC"
"CBAC"
11
Returns: "CCBC"
"CBAC"
12
Returns: "CCAC"
"CBAC"
13
Returns: "ACAC"
"CBAC"
14
Returns: "ABAC"
"CBAC"
15
Returns: "CBAC"
"CABBCCBACCBACCABAACCCCACABABAB"
0
Returns: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"BAACCBACBBBBACAACCAACCAABAACCA"
432853159
Returns: "BAACCBACBBBBACAACCAACCAABAACCA"
"ABCCBCACCBCABABBAABCCBAABACBAC"
536870911
Returns: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBA"
"ABCCBCACCBCABABBAABCCBAABACBAC"
536870912
Returns: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBC"
"ABCCBCACCBCABABBAABCCBAABACBAC"
536870913
Returns: "ABBBBBBBBBBBBBBBBBBBBBBBBBBBBC"
"ABCCBCACCBCABABBAABCCBAABACBAC"
268435456
Returns: "CCCCCCCCCCCCCCCCCCCCCCCCCCCCBA"
"ABCCBCACCBCABABBAABCCBAABACBAC"
268435455
Returns: "CCCCCCCCCCCCCCCCCCCCCCCCCCCCAA"
"CAAAABAACBCACCACCAACACCBABAAAB"
1000000000
Returns: "AAAAAAAAABCABBAABCABBAABBBCAAB"
"CAAAABAACBCACCACCAACACCBABAAAB"
100000000
Returns: "BBBBBBBBCAAAACCCCABCAAAAABCAAA"
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
1073741823
Returns: "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
"CCBAABBCBBABBCBBABABCBABAAABAB"
395488233
Returns: "BCCBACCCCCBACBACBACCABBAAAABCA"
"ABCABCABCABCABCABCABCABCABCCAB"
900909000
Returns: "BBBACCAAAAABBBCCBACCAACBBCABAB"
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
1000000000
Returns: "CCCCCCCCCABCAACCABCAACCAAABCCC"
"BCBC"
9
Returns: "ABBC"
"BACBAACC"
201
Returns: "BCCBAACC"