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
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.
5
1
1
Returns: 0
The upper left corner.
15
32768
1
Returns: 1073741823
The upper right corner.
1
2
2
Returns: 2
10
546
109
Returns: 955129
15
30000
30000
Returns: 706873514
1
1
1
Returns: 0
2
3
1
Returns: 14
2
3
2
Returns: 13
3
6
7
Returns: 39
3
7
2
Returns: 61
4
9
2
Returns: 233
4
8
3
Returns: 25
5
21
21
Returns: 544
5
18
23
Returns: 573
7
52
87
Returns: 7353
10
17
47
Returns: 1878
6
20
12
Returns: 480
8
92
186
Returns: 29742
6
12
11
Returns: 137
7
5
108
Returns: 5343
7
52
72
Returns: 7984
8
162
162
Returns: 34818
9
492
143
Returns: 202257
8
78
114
Returns: 12114
6
21
2
Returns: 313
10
777
476
Returns: 850821
7
82
19
Returns: 14509
10
574
264
Returns: 896998
9
321
290
Returns: 138923
8
33
28
Returns: 3429
10
319
712
Returns: 483305
6
22
56
Returns: 1580
6
25
2
Returns: 323
7
14
40
Returns: 3652
8
71
219
Returns: 25560
9
265
506
Returns: 152961
7
104
45
Returns: 13029
10
587
64
Returns: 963993
8
115
12
Returns: 5601
6
60
31
Returns: 3089
10
417
709
Returns: 472122
9
415
292
Returns: 190457
10
747
873
Returns: 639526
8
69
120
Returns: 12069
9
444
393
Returns: 165263
6
13
9
Returns: 186
9
3
390
Returns: 81949
10
951
765
Returns: 735948
7
120
100
Returns: 11194
8
5
241
Returns: 21776
8
167
100
Returns: 53905
7
46
21
Returns: 1867
6
52
26
Returns: 3276
7
54
20
Returns: 1588
11
123
1976
Returns: 1405145
12
4000
2561
Returns: 11626837
13
6217
3122
Returns: 53491137
14
455
15555
Returns: 89235506
15
15678
9871
Returns: 158191527
15
32767
17011
Returns: 805038840
5
1
1
Returns: 0
15
30000
30000
Returns: 706873514
10
546
109
Returns: 955129
15
666
666
Returns: 557698
15
2000
3000
Returns: 13645248