Statistics

Problem Statement for "Hilbert"

Problem Statement

[Note to plug-in users: This problem statement contains images and superscripts. If you do not see the images, or if 2k is displayed the same as 2k, then please view the problem in the applet.]

A Hilbert curve is a path that traces through all the points in a two-dimensional square grid in such a way that each step in the path moves between neighbors in the grid. Hilbert curves are sometimes used in image processing because they exhibit greater locality than other common methods of linearizing a two-dimensional image, such as row-by-row (even than the boustrophedonic variations of row-by-row that alternate between left-to-right and right-to-left).

A rank-k Hilbert curve is a path through a 2k-by-2k grid. A rank-1 Hilbert curve is a simple U-shape through a 2-by-2 grid, as shown below.



The blue point indicates the first point in the path and the red point indicates the last point in the path. For k > 1, a rank-k Hilbert curve is composed of four rank-(k-1) Hilbert curves, shown below as dotted lines.



Notice how the curve is organized in quadrants, first the upper left, then the lower left, then the lower right, and finally the upper right. (The cross in the background is not part of the curve; it merely highlights the quadrants.) Notice also how some of the rank-(k-1) curves are flipped and rotated compared to the orientation of the rank-k curve. The blue and red points indicate where each rank-(k-1) curve begins and ends, respectively. The overall rank-k curve begins at the first point of the upper left rank-(k-1) curve and ends at the last point of the upper right rank-(k-1) curve. Here is a complete picture of a rank-3 Hilbert curve.

You will be given a value k and coordinates x and y in a 2k-by-2k grid. Coordinates range from 1 to 2k inclusive, with the upper left corner at coordinates (1,1). Assuming the points are traversed in the order specified by a rank-k Hilbert curve, determine how many steps it takes to reach the point at coordinates (x,y). For example, it takes 0 steps to reach the upper left corner at coordinates (1,1) and 4k-1 steps to reach the upper right corner at coordinates (2k,1).

Definition

Class:
Hilbert
Method:
steps
Parameters:
int, int, int
Returns:
int
Method signature:
int steps(int k, int x, int y)
(be sure your method is public)

Constraints

  • k is between 1 and 15, inclusive.
  • x and y are between 1 and 2k, inclusive.

Examples

  1. 3

    2

    3

    Returns: 13

    The target is the colored point in the image below. It takes 13 steps to reach from the start point along the path highlighted in blue.

  2. 5

    1

    1

    Returns: 0

    The upper left corner.

  3. 15

    32768

    1

    Returns: 1073741823

    The upper right corner.

  4. 1

    2

    2

    Returns: 2

  5. 10

    546

    109

    Returns: 955129

  6. 15

    30000

    30000

    Returns: 706873514

  7. 1

    1

    1

    Returns: 0

  8. 2

    3

    1

    Returns: 14

  9. 2

    3

    2

    Returns: 13

  10. 3

    6

    7

    Returns: 39

  11. 3

    7

    2

    Returns: 61

  12. 4

    9

    2

    Returns: 233

  13. 4

    8

    3

    Returns: 25

  14. 5

    21

    21

    Returns: 544

  15. 5

    18

    23

    Returns: 573

  16. 7

    52

    87

    Returns: 7353

  17. 10

    17

    47

    Returns: 1878

  18. 6

    20

    12

    Returns: 480

  19. 8

    92

    186

    Returns: 29742

  20. 6

    12

    11

    Returns: 137

  21. 7

    5

    108

    Returns: 5343

  22. 7

    52

    72

    Returns: 7984

  23. 8

    162

    162

    Returns: 34818

  24. 9

    492

    143

    Returns: 202257

  25. 8

    78

    114

    Returns: 12114

  26. 6

    21

    2

    Returns: 313

  27. 10

    777

    476

    Returns: 850821

  28. 7

    82

    19

    Returns: 14509

  29. 10

    574

    264

    Returns: 896998

  30. 9

    321

    290

    Returns: 138923

  31. 8

    33

    28

    Returns: 3429

  32. 10

    319

    712

    Returns: 483305

  33. 6

    22

    56

    Returns: 1580

  34. 6

    25

    2

    Returns: 323

  35. 7

    14

    40

    Returns: 3652

  36. 8

    71

    219

    Returns: 25560

  37. 9

    265

    506

    Returns: 152961

  38. 7

    104

    45

    Returns: 13029

  39. 10

    587

    64

    Returns: 963993

  40. 8

    115

    12

    Returns: 5601

  41. 6

    60

    31

    Returns: 3089

  42. 10

    417

    709

    Returns: 472122

  43. 9

    415

    292

    Returns: 190457

  44. 10

    747

    873

    Returns: 639526

  45. 8

    69

    120

    Returns: 12069

  46. 9

    444

    393

    Returns: 165263

  47. 6

    13

    9

    Returns: 186

  48. 9

    3

    390

    Returns: 81949

  49. 10

    951

    765

    Returns: 735948

  50. 7

    120

    100

    Returns: 11194

  51. 8

    5

    241

    Returns: 21776

  52. 8

    167

    100

    Returns: 53905

  53. 7

    46

    21

    Returns: 1867

  54. 6

    52

    26

    Returns: 3276

  55. 7

    54

    20

    Returns: 1588

  56. 11

    123

    1976

    Returns: 1405145

  57. 12

    4000

    2561

    Returns: 11626837

  58. 13

    6217

    3122

    Returns: 53491137

  59. 14

    455

    15555

    Returns: 89235506

  60. 15

    15678

    9871

    Returns: 158191527

  61. 15

    32767

    17011

    Returns: 805038840

  62. 5

    1

    1

    Returns: 0

  63. 15

    30000

    30000

    Returns: 706873514

  64. 10

    546

    109

    Returns: 955129

  65. 15

    666

    666

    Returns: 557698

  66. 15

    2000

    3000

    Returns: 13645248


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: