Fractal Streets 递归+坐标转换

  • n阶分形图是由前面的(n-1)阶分形图所构成:
  • 左上角是沿Y轴右旋转90度再沿X轴选择180度;
  • 右上角不变;
  • 左下角是沿Y轴左旋转90度再沿X轴选择180度;
  • 右下角不变。

再找到对应的坐标变换:
左上角:(y,x)
右上角:(x,y+1<<(n-1))
左下角:(1<<n-y+1,1<<(n-1)-x+1)
右下角:(x+1<<(n-1),y+1<<(n-1))

import java.util.Scanner;

public class MainG {
	static long x,y;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- > 0) {
			int n = sc.nextInt();
			long a = sc.nextLong();
			long b = sc.nextLong();
			G ares = dfs(n,a);
			G bres = dfs(n,b);
			System.out.println(res(ares,bres));
		}
	}
	public static long res(G a,G b) {
		return (long)(Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))*10+0.5);
	}
	public static G dfs(int n,long id) {
		if(n==1) {
			if(id==1) {
				x = 1;
				y = 1;
			}else if(id==2) {
				x = 1;
				y = 2;
			}else if(id==3) {
				x = 2;
				y = 2;
			}else {
				x = 2;
				y = 1;
			}
			return new G(x,y);
		}else {
			G t;
			long tem = (long)1<<(n-1);
			long tid = tem*tem;
			if(id<=tid) {			//左上角
				 t = dfs(n-1,id);
				 long temp = t.x;
				 t.x = t.y;
				 t.y = temp;
				 return t;
			}else if(id<=2*tid) {	//右上角
				t = dfs(n-1,id-tid);
				t.y += tem;
				return t;
			}else if(id<=3*tid) {	//左下角
				t = dfs(n-1,id-2*tid);
				t.x += tem;
				t.y += tem;
				return t;
			}else {					//右下角
				t = dfs(n-1,id-3*tid);
				long temp = t.x;
				long tx = (long)1<<n;
				t.x = tx-t.y+1;
				t.y = tem-temp+1;
				return t;
			}
		}
	}
}
class G{
	long x,y;
	public G(long x,long y) {
		this.x = x;
		this.y = y;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值