태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

안드로이드용 영어 어학기 Smart LC

스마트LC 소개 링크:
http://blog.ehxm.net/123

티스토어 링크:
http://bit.ly/awW3XW

"divide"에 해당되는 글 1건

  1. 2009/10/26 알고리즘 (Divide , Recursion) Fractal Streets

알고리즘 (Divide , Recursion) Fractal Streets

Posted by EHXM. Posted in " 경험/알고리즘 "2009/10/26 01:56

G. Fractal Streets

With a growing desire for modernization in our increasingly larger cities comes a need for new street designs. Chris is one of the unfortunate city planners responsible for these designs. Each year the demands keep increasing, and this year he has even been asked to design a completely new city.

More work is not something Chris needs right now, since like any good bureaucrat, he is extremely lazy. Given that this is a character trait he has in common with most computer scientists it should come as no surprise that one of his closest friends, Paul, is in fact a computer scientist. And it was Paul who suggested the brilliant idea that has made Chris a hero among his peers: Fractal Streets! By using a Hilbert curve, he can easily fill in rectangular plots of arbitrary size with very little work.

A Hilbert curve of order 1 consists of one “cup”. In a Hilbert curve of order 2 that cup is replaced by four smaller but identical cups and three connecting roads. In a Hilbert curve of order 3 those four cups are in turn replaced by four identical but still smaller cups and three connecting roads, etc. At each corner of a cup a driveway (with mailbox) is placed for a house, with a simple successive numbering. The house in the top left corner has number 1, and the distance between two adjacent houses is 10 m.

The situation is shown graphically in figure 2. As you can see the Fractal Streets concept successfully eliminates the need for boring street grids, while still requiring very little effort from our bureaucrats.

 

(a) Order 1                                                  (b) Order 2                                               (c) Order 3

Figure 2: Hilbert curves of order 1, 2 and 3, with the location of the houses indicated.

As a token of their gratitude, several mayors have offered Chris a house in one of the many new neighborhoods built with his own new scheme. Chris would now like to know which of these offerings will get him a house closest to the local city planning office (of course each of these new neighborhoods has one). Luckily he will not have to actually drive along the street, because his new company “caris one of those new flying cars. This high-tech vehicle allows him to travel in a straight line from his driveway to the driveway of his new office. Can you write a program to determine the distance he will have to y for each offer (excluding the vertical distance at takeoand landing)?


Input

On the first line of the input is a positive integer, the number of test cases. Then for each test case:

A line containing a three positive integers, n< 16 and h,o < 231, specifying the order of the Hilbert curve, and the house numbers of the offered house and the local city planning office.

Output

For each test case:

One line containing the distance Chris will have to y to his work in meters, rounded to the nearest integer.


Example
Input
3
1
1 2
2
16 1
3
4 33
14
Output
10
30
50



Order n일때의 city 모양은 order n-1d의 city 모양을 네 부분에서 이어지는 모양으로, order가 증가함에 따라 모양이 규칙적으로 증가하는 프렉탈입니다.

문제는 위 그림과 같은 규칙으로 order가 증가할때, order n에서 h번째와 o번째 위치의 거리를 구하는 것입니다.



우선 규칙을 살펴보면, order n에서의 1.왼쪽 상단에 있는 city 모양은 order n-1의 city를 시계방향으로 90도 회전한 모양입니다. 그리고 2.오른쪽의 상,하단은 각각 order n-1의 city를 그대로 붙인 모양이고 마지막으로 3. 왼쪽 하단에 있는 모양은 반시계방향으로 90도 회전한 모양입니다. 주의할점은 왼쪽 상,하단의 city 집 번호 순서가 n-1일때의 집 번호 순서와는 반대로 되어 있다는 점입니다.

그래서 order n일때의 m번째 집의 위치를 재귀형식으로 네가지 부분으로 나누어서 생각해 볼 수 있죠.
그리고 그때의 한 부분의 집의 개수 a는 2^(2*(n-1)) , 그 부분의 가로부분의 집의 개수 b는 2^(n-1)임을 알 수 있죠.
그래서 위 n=3에서의 왼쪽 상단부분의 집의 개수 a는 16개이고, b는 4가 되죠.

그래서 order n일때의 m번째의 좌표를 구해보면 다음과 같습니다.
position solve( n, m)
1. m번째 점이 좌측 상단이면, 즉 m <= a
solve(n-1, a-m+1)의 x,y값을 90도 회전

2. m번째 점이 우측 상단이면, 즉 m <= 2*a
solve(n-1, m-a)의 x+b, y

3. m번째 점이 우측 하단이면,
solve(n-1, m-2*a)의 x+b, y+b

4. m번째 점이 좌측 하단이면,
solve(n-1, a-(m-3*a)+1)의 x, y+b

이렇게 구한 x,y값으로 거리를 구하면 됩니다.



typedef struct{
	int x;
	int y;
}point;


point solve(long order,long num){
	if(order == 1){
		point a;
		if(num == 1){
			a.x = 1;
			a.y = 1;
		}
		else if(num == 2){
			a.x = 2;
			a.y = 1;
		}
		else if(num == 3){
			a.x = 2;
			a.y = 2;
		}
		else if(num == 4){
			a.x = 1;
			a.y = 2;
		}
		return a;
	}
	long div = pow(2.0,2*(order-1));
	FOR(i, 4){
		if(num <= (i+1)*div){
			
			if(i==0){
				point tmp = solve(order-1, div - (num-i*div-1));
				
				long double dx = (pow(2.0,order-1)+1)/2.0;
				long double dy = dx;

				long double cx = tmp.x - dx;
				long double cy = dy - tmp.y;
				
				long double fx = cy;
				long double fy = -cx;
				
				tmp.x = dx + fx;
				tmp.y = dy - fy;
		
				
				return tmp;
			}
			else if(i==1){
				point tmp = solve(order-1, num-i*div);
				tmp.x = tmp.x + pow(2.0,order-1);
				return tmp;
			}
			else if(i==2){
				point tmp = solve(order-1, num-i*div);
				tmp.x = tmp.x + pow(2.0,order-1);
				tmp.y = tmp.y + pow(2.0,order-1);
				return tmp;
			}
			if(i==3){
				
				point tmp = solve(order-1, div - (num-i*div-1));
				long double dx = (pow(2.0,order-1)+1)/2.0;
				long double dy = dx;

				long double cx = tmp.x - dx;
				long double cy = dy - tmp.y;
				
				long double fx = -cy;
				long double fy = cx;
				
				tmp.x = dx + fx;
				tmp.y = dy - fy;

				
				tmp.y = tmp.y + pow(2.0,order-1);
				return tmp;
			}
		}
	}
}

int main(){
	int testCase;
	cin >> testCase;
	FOR(testi, testCase){
		long o,a,b;
		cin >> o >> a >> b;
		point pa = solve(o,a);
		point pb = solve(o,b);
		long double tmp = (pa.x-pb.x)*(pa.x-pb.x)+(pa.y-pb.y)*(pa.y-pb.y);
		long result = (long)(sqrt(tmp)*10+0.5);
		cout << result << endl;
	}
	
	return 0;
}

관련 TAG로 검색해보세요. : , , , ,

¬ COMMENT [0]

여러분의 커뮤니케이션을 기다리고 있습니다.

  1. : 이름
  2. : 홈페이지

  1. : 비밀번호

[안드로이드] 영어 어학기 어플

영어 듣기 공부 많이들 하시나요? 따로 어학기를 장만하시기는 비용이 들죠? 스마트폰에서 MP3 파일을 터치를 이용해서 자유롭게 듣을 수 있는 영어 어학기 어플입니다. 동아리.....

2010년 대한민국 매쉬업 경진대회 후기, 아이디어 전쟁을 다녀와서..

아이디어의 전쟁의 현장이었던 2010년 대한민국 매쉬업 경진대회에 다녀왔습니다. 이번 대회는 지난 2월 6일(토요일), 삼성동 코엑스 컨퍼런스룸 401에서 열렸습니다. 이번.....

2010년 100가지가 넘는 안드로이드폰이 몰려온다!

2010년에 100가지가 넘는 안드로이드 폰 출시가 될 예정입니다. Mobile World Congress keynote에서 Google CEO Eric Schmidt의 연설.....

[안드로이드] 모토로이 체험할 수 있는 곳 (전국)

서울, 안양, 부산, 대구, 광주, 대전에 안드로이드 폰 체험 할 수 있는 곳이 있네요. 저는 코엑스 메가박스 입구에 있는 모토로라 체험 부스에서 우연히 모토로이를 만져보게 되.....

위 3D 갤러리는 http://www.fotoviewr.com/ 사이트의 Fotoviewr 입니다. Flex와 Papervision3D를 이용하여 위와같은 3D 갤러리를 구현해.....

무료 MP3 포멧 변경 툴 - Free MP3 WMA Converter
무료 MP3 포멧 변경 툴 - Free MP3 WMA Converter
무료 MP3 포멧 변경 툴 - Free MP3 WMA Converter
무료 MP3 포멧 변경 툴 - Free MP3 WMA Converter
언톡 2010년 신입생 모집 포스터
Android, LEGO NXT를 이용한 Sudoku Solving Robot 제작
Android, LEGO NXT를 이용한 Sudoku Solving Robot 제작
Android, LEGO NXT를 이용한 Sudoku Solving Robot 제작
Android, LEGO NXT를 이용한 Sudoku Solving Robot 제작

Category

전체보기 (108)
Anycall Dreamers (1)
안드로이드 (39)
Adobe Flash Platform (20)
Algorithm (0)
개발노트 (6)
경험 (33)

글 보관함

2011/02 (3)

2011/01 (1)

2010/09 (1)

2010/08 (1)

2010/07 (2)

Calendar

«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

믹시


Total : 115,868 Today : 224 Yesterday : 155