In Programming Land, there are several pathways called Philosopher’s Walks for philosophers to have a rest. A Philosopher’s Walk is a pathway in a square-shaped region with plenty of woods. The woods are helpful for philosophers to think, but they planted so densely like a maze that they lost their ways in the maze of woods of a Philosopher’s Walk.
Fortunately, the structures of all Philosopher’s Walks are similar; the structure of a Philosopher’s Walk is designed and constructed according to the same rule in a 2k meter square. The rule for designing the pathway is to take a right-turn in 90 degrees after every 1-meter step when k is 1, and the bigger one for which the integer k is greater than 1 is built up using four sub-pathways with k - 1 in a fractal style. Figure F.1 shows three Philosopher’s Walks for which k is 1, 2, and 3. The Philosopher’s Walk W2 consists of four W1 structures with the lower-left and the lower-right ones are 90 degree rotated clockwise and counter-clockwise, respectively; the upper ones have the same structure with W1. The same is true for any Wk for which the integer k is greater than 1. This rule has been devised by a mathematical philosopher David Hilbert (1862 – 1943), and the resulting pathway is usually called a HILBERT CURVE named after him. He once talked about a space filling method using this kind of curve to fill up a square with 2k sides, and every Philosophers’ Walk is designed according to this method.
Figure F.1. Three Philosopher's Walks with size (a) 21 = 2, (b) 22 = 4, and (c) 23 = 8, repectively.
Tae-Cheon is in charge of the rescue of the philosophers lost in Philosopher’s Walks using a hot air balloon. Fortunately, every lost philosopher can report Tae-Cheon the number of meter steps he has taken, and Tae-Cheon knows the length of a side of the square of the Philosopher’s Walk. He has to identify the location of the lost philosopher, the (x,y) coordinates assuming that the Philosopher’s Walk is placed in the 1st quadrant of a Cartesian plain with one meter unit length. Assume that the coordinate of the lower-left corner block is (1,1). The entrance of a Philosopher’s Walk is always at (1,1) and the exit is always (n,1) where n is the length of a side. Also assume that the philosopher have walked one meter step when he is in the entrance, and that he always go forward to the exit without back steps.
For example, if the number of meter-steps taken by a lost philosopher in the Philosopher’s Walk in W2 in Figure F.1(b) is 10, your program should report (3,4).
Your mission is to write a program to help Tae-Cheon by making a program reporting the location of the lost philosopher given the number of meter-steps he has taken and the length of a side of the square of the Philosopher’s Walk. Hurry! A philosopher urgently needs your help.
Your program is to read from standard input. The input consists of a single line containing two positive integers, n and m, representing the length of a side of the square of the Philosopher’s Walk and the number of meter-steps taken by the lost philosopher, respectively, where n = 2k and m ≤ 22k for an integer k satisfying 0 < k ≤ 15.
Your program is to write to standard output. The single output line should contain two integers, x and y, separated by a space, where (x,y) is the location of the lost philosopher in the given Philosopher’s Walk.
힐베르트 커브의 방문 순서가 주어질 때, 그 정점을 묻는 문제로 처음 보면 조금 당황할 수 있다.
도형의 규칙을 잘 찾아보면 되는데
<왼쪽 위, 오른쪽 위의 경우> Base Case(n=2)와 같은 방문 순서대로 풀어주면 된다
<왼쪽 하단의 경우> Base Case의 경우를 y = x직선에 대칭한 것이다.
(x', y') = (y, x)가 된다.
<오른쪽 하단의 경우> Base Case의 경우를 먼저 y = -x 직선에 대칭시킨다.
(x', y') = (-y, -x)
Base Case를 기준으로 보면, 변형된 x좌표의 경우, 맨 오른쪽에서 빼므로
현재 보는 사각형을 감싸는 큰 사각형의 가로길이(W) - y + 1을 해주면 된다(좌표가 1,1부터 시작함)
변형된 y좌표의 경우, 중간 선부터 한칸 내려가므로 큰 사각형의 절반에서 -x+1을 해주면 된다,
즉 정리하면 (x,y) -> (2w-y+1, h-w+1)로 이동한다고 볼 수 있다.
이를 재귀적으로 풀어주면 O(log n)에 정답을 구할 수 있다.