分形之城(详细,含画图)

分形之城
算法竞赛进阶指南P16。
注:此文中x,y分别对应数组的横、纵轴。
在这里插入图片描述
对于 z = 0 z=0 z=0 ,我们将原本的 ( x , y ) (x, y) (x,y) ,沿原点顺时针旋转90° → ( y , − x ) \xrightarrow{} (y, -x) (y,x) ,再沿y轴翻转 → ( y , x ) \xrightarrow{} (y, x) (y,x)
对于 z = 1 z=1 z=1 ,我们将原本的 ( x , y ) (x, y) (x,y) ,水平右移 len 个单元格 → ( x , y + l e n ) \xrightarrow{} (x , y + len) (x,y+len)
在这里插入图片描述
对于 z = 2 z=2 z=2 ,我们将原本的 ( x , y ) (x, y) (x,y) ,水平右移 len 个单元格 → ( x , y + l e n ) \xrightarrow{} (x , y + len) (x,y+len), 再竖直下移 len 个单元格 → ( x + l e n , y + l e n ) \xrightarrow{} (x+len , y + len) (x+len,y+len)
对于 z = 3 z=3 z=3 ,我们将原本的 ( x , y ) (x, y) (x,y) ,逆时针旋转90° → ( − y , x ) \xrightarrow{} (-y, x) (y,x), 沿y轴翻转 → ( − y , − x ) \xrightarrow{} (-y, -x) (y,x), 再竖直下移2*len - 1个单元格, 水平右移len个单元格 → ( − y + 2 ∗ l e n − 1 , − x + l e n − 1 ) \xrightarrow{} (- y + 2 * len - 1, - x + len - 1) (y+2len1,x+len1)

代码:

import sys
import copy
import threading
from collections import deque, defaultdict
import heapq
from itertools import accumulate, permutations, combinations
import math

input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")

INF = pow(2, 31) - 1


# sys.setrecursionlimit(100000)


def read():
    line = sys.stdin.readline().strip()
    while not line:
        line = sys.stdin.readline().strip()
    return map(int, line.split())


def mul_input():
    return map(int, input().split())


def I():
    return int(input())


def do(n, m):
    if n == 0:
        return 0, 0
    len = 1 << (n - 1)
    cnt = 1 << (2 * n - 2)
    x, y = do(n - 1, m % cnt)
    z = m // cnt
    if z == 0:
        return y, x
    if z == 1:
        return x, y + len
    if z == 2:
        return x + len, y + len
    if z == 3:
        return 2 * len - y - 1, len - x - 1


for _ in range(I()):
    n, a, b = read()
    x1, y1 = do(n, a - 1)
    x2, y2 = do(n, b - 1)
    print(round(math.sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)) * 10))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值