Statistics

Problem Statement for "HanoiGoodAndBad"

Problem Statement

The Towers of Hanoi puzzle consists of 3 pegs and a number of disks of distinct radii. The 3 pegs are the source, spare, and target. Initially, all disks are on the source peg, in ascending order by radius from top to bottom. The goal is to move all disks to the target peg, subject to the following rules:

  • Only one disk may be moved at a time.
  • No disk may be placed on top of a smaller disk.
  • A move consists of taking the highest disk from one peg, and placing it onto another peg above any disks already on that peg.

Dave and Earl are each solving a Towers of Hanoi puzzle with N disks. Dave makes as few moves as possible, solving the puzzle in only 2^N-1 moves. Earl, on the other hand, encounters every possible configuration of disks exactly once during the course of solving it, thereby requiring 3^N-1 moves to solve it. Pseudocode for their respective solutions are:

solve_Dave(source, target, spare, N)
{
	if(N>0)
	{
		solve_Dave(source, spare, target, N-1)
		move a disk from source to target
		solve_Dave(spare, target, source, N-1)
	}
}

solve_Earl(source, target, spare, N)
{
	if(N>0)
	{
		solve_Earl(source, target, spare, N-1)
		move a disk from source to spare
		solve_Earl(target, source, spare, N-1)
		move a disk from spare to target
		solve_Earl(source, target, spare, N-1)
	}
}

Given N, and the number of moves Dave has made toward the solution, return the number of moves Earl must make in order to reach the same configuration of disks as Dave.

Definition

Class:
HanoiGoodAndBad
Method:
moves
Parameters:
int, int
Returns:
int
Method signature:
int moves(int N, int Dave)
(be sure your method is public)

Constraints

  • N will be between 1 and 19, inclusive.
  • Dave will be bewteen 0 and 2^N-1, inclusive.

Examples

  1. 3

    1

    Returns: 2

    Dave begins by moving a disk from the source peg to the target peg. Earl begins by moving a disk from the source peg to the spare peg, then to the target peg.

  2. 4

    15

    Returns: 80

    It takes Dave 15 moves to completely solve the puzzle with 4 disks, and Earl 80 moves.

  3. 9

    265

    Returns: 16418

  4. 1

    0

    Returns: 0

  5. 19

    524287

    Returns: 1162261466

  6. 1

    1

    Returns: 2

  7. 19

    469957

    Returns: 1141770746

  8. 19

    112769

    Returns: 117099635

  9. 19

    267522

    Returns: 968892396

  10. 19

    398718

    Returns: 1034488176

  11. 19

    471981

    Returns: 1142891524

  12. 2

    1

    Returns: 1

  13. 2

    2

    Returns: 7

  14. 2

    3

    Returns: 8

  15. 3

    3

    Returns: 4

  16. 3

    5

    Returns: 23

  17. 3

    0

    Returns: 0

  18. 4

    5

    Returns: 11

  19. 4

    7

    Returns: 13

  20. 4

    2

    Returns: 7

  21. 5

    4

    Returns: 22

  22. 5

    25

    Returns: 218

  23. 5

    16

    Returns: 202

  24. 6

    13

    Returns: 73

  25. 6

    57

    Returns: 716

  26. 6

    60

    Returns: 720

  27. 7

    12

    Returns: 36

  28. 7

    55

    Returns: 350

  29. 7

    38

    Returns: 255

  30. 8

    106

    Returns: 1041

  31. 8

    55

    Returns: 661

  32. 8

    179

    Returns: 5795

  33. 9

    430

    Returns: 17818

  34. 9

    357

    Returns: 17380

  35. 9

    1

    Returns: 2

  36. 10

    342

    Returns: 8436

  37. 10

    138

    Returns: 5484

  38. 10

    313

    Returns: 6913

  39. 11

    1227

    Returns: 150556

  40. 11

    1663

    Returns: 159650

  41. 11

    1661

    Returns: 159647

  42. 12

    4066

    Returns: 531321

  43. 12

    511

    Returns: 9841

  44. 12

    2455

    Returns: 451669

  45. 13

    4871

    Returns: 1354859

  46. 13

    6275

    Returns: 1419371

  47. 13

    6204

    Returns: 1417536

  48. 14

    9278

    Returns: 4016059

  49. 14

    4515

    Returns: 549188

  50. 14

    129

    Returns: 5468

  51. 15

    17606

    Returns: 11992791

  52. 15

    1064

    Returns: 147771

  53. 15

    29688

    Returns: 14112684

  54. 16

    641

    Returns: 50303

  55. 16

    38386

    Returns: 36216681

  56. 16

    11357

    Returns: 4222487

  57. 17

    49370

    Returns: 19135011

  58. 17

    128491

    Returns: 128913758

  59. 17

    122606

    Returns: 127539075

  60. 18

    1

    Returns: 1

  61. 18

    38688

    Returns: 36223524

  62. 18

    171075

    Returns: 331590032

  63. 18

    262142

    Returns: 387420487

  64. 19

    123456

    Returns: 127566252

  65. 19

    504281

    Returns: 1150057751

  66. 19

    424288

    Returns: 1047310438

  67. 19

    593

    Returns: 20615

  68. 3

    2

    Returns: 3

  69. 19

    100000

    Returns: 114951514

  70. 19

    10000

    Returns: 1679818

  71. 5

    26

    Returns: 219

  72. 19

    342222

    Returns: 994771932

  73. 19

    14526

    Returns: 2308740

  74. 10

    200

    Returns: 5899

  75. 19

    341673

    Returns: 994410460

  76. 19

    262145

    Returns: 968551223

  77. 19

    500000

    Returns: 1149513687


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: