Philosopher ' s Walk (recursive)

Source: Internet
Author: User
Tags in degrees

In programming land, there is several pathways called philosopher ' s walks for philosophers to has a rest. A philosopher ' s Walk is a pathway in a square-shaped region with plenty of woods. The woods is helpful for philosophers and think, but they planted so densely as 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 is 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 are to take a right-turn in degrees after every 1-meter step when K is 1, and the big GER one for which the integer k is greater than 1 are 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 degree rotate D clockwise and counter-clockwise, respectively; The upper ones has the same structure with W1. The same is true if Wk for which, the integer k is greater than 1. This rule had 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 are designed according to this method.
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 had taken, and Tae-cheon knows the L Ength 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 are pla CED in the 1st quadrant of a Cartesian plain with one meter unit length. Assume the coordinate of the Lower-left corner block is. The entrance of a philosopher ' s Walk are always at (+) and the exit is all (n,1) where n is the length of a side. Also assume that the philosopher had walked one meter step when he was in the entrance, and so he always go forward to T He 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 Your program should (3,4).

Your mission is to write a program to help Tae-cheon by making a program reporting the location of the lost philosopher GI Ven 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.

Enter your program was to read from standard input. The input consists of a single line containing the positive integers, N and m, representing the length of a side of the sq Uare 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.

Output your program was to write to standard output. The single output line should contain-integers, x and y, separated by a space, where (x, y) are the location of the lost Philosopher in the given philosopher ' s Walk.

Sample input
4 10

Sample output
3 4


Test instructions: is to give you the size of the map, let you seek out the coordinates of a certain number of steps.
Why change position, in fact, it is equivalent to the original direction of the diagram is the same as the central symmetry of the flip, so that this rotation

1#include <bits/stdc++.h>2 using namespacestd;3typedef pair<int,int>P;4 intn,m;5 6P Solve (intNintm)7 {8     if(n = =2)9     {Ten         if(M = =0)returnP1,1); One         Else if(M = =1)returnP1,2); A         Else if(M = =2)returnP2,2); -         Else returnP2,1); -     } the     intK = n/2; -     intpart = m/(k*k); -P tmp = Solve (n>>1, m% (k*k)); -     if(Part = =0) swap (tmp.first,tmp.second); +     Else if(Part = =1) tmp.second+=K; -     Else if(Part = =2) tmp.first+=k,tmp.second+=K; +     Else A     { at P ret; -Ret.first = k-tmp.second+1; -Ret.second = k-tmp.first+1; -Ret.first + =K; -TMP =ret; -     } in     returntmp; - } to intMain () + { -scanf"%d%d",&n,&m); them--; *P ans =solve (n,m); $printf"%d%d\n", Ans.first,ans.second);Panax Notoginseng}
View Code

Philosopher ' s Walk (recursive)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.