坐标转换 + 分治 + 递归

98. 分形之城 - AcWing题库

 思路就是:分成四个区域, 一直递归细分, 考虑坐标的转化,转换公式:

 要注意的是 a, b 传进去的时候要减1, 这样 0 就相当于 1, 取模就是会有其他为0的情况!!

/*//
Problem: 98. 分形之城
Contest: 递归与递推
URL: https://www.acwing.com/problem/content/100/
/*/
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
using namespace std;
const int inf = 0x3f3f3f3f;
const ll ll_inf = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int maxn = 1e5 + 5;

struct node {
	double x, y;
};

node solve(ll n, ll cnt) {
	node now;
	if (n == 0) {
		now.x = 0, now.y = 0;
		return now;
	}
	ll len = (ll)1 << (n - 1);
	ll num = (ll)1 << (2 * n - 2);
	node last = solve(n - 1, cnt % num);
	int flag = cnt / num;
	if (flag == 0) {
		now.x = -last.y - len;
		now.y = -last.x + len;
		return now;
	}
	if (flag == 1) {
		now.x = last.x + len;
		now.y = last.y + len;
		return now;
	}
	if (flag == 2) {
		now.x = last.x + len;
		now.y = last.y - len;
		return now;
	}
	if (flag == 3) {
		now.x = last.y - len;
		now.y = last.x - len;
		return now;
	}
	return now;
}

int main(int argc, char const *argv[]) {
	ios::sync_with_stdio(false);
	cin.tie(0);     cout.tie(0);

	int T;
	cin >> T;
	while (T --) {
		ll n, a, b;
		cin >> n >> a >> b;
		node A = solve(n, a-1);
		node B = solve(n, b-1);
		double ans = 5 * (double)sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
		printf("%.0lf\n", ans);
	}
	return 0;
}

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值