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; } }