建系,坐标转换
Acwing 98 - 分形之城
思路: 这道题重点在于坐标的转换。一个等级分成四块,第一块沿着 y = x y=x y=x 对称,第四块沿着 y = − x y=-x y=−x 对称,其余两块平移。四块再自己的基础上要加上偏移量。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
struct point {
LL x, y;
};
double get_dis(point a, point b){
return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + 1.0 * (a.y - b.y) * (a.y - b.y));
}
point get_idx(LL n, LL id)
{
if(n == 1){
if(id == 0) return {0, 0};
if(id == 1) return {0, 1};
if(id == 2) return {1, 1};
if(id == 3) return {1, 0};
}
LL len = 1ll << n - 1, cnt = 1ll << 2 * n - 2;
point res = get_idx(n - 1, id % cnt);
LL x = res.x, y = res.y;
if(id / cnt == 0) return {y, x};
if(id / cnt == 1) return {x, y + len};
if(id / cnt == 2) return {x + len, y + len};
return {2 * len - 1 - y, len - 1 - x};
}
int main()
{
int T; scanf("%d", &T);
while(T -- ){
LL a, b, n; scanf("%lld%lld%lld", &n, &a, &b);
a -- , b -- ;
point pa = get_idx(n, a), pb = get_idx(n, b);
printf("%.0f\n", get_dis(pa, pb) * 10);
}
return 0;
}
河南省2021ICPC B - Honeycomb
思路: 这题和上一题有点类似,都和坐标有关。这题关键在于建系,建一个x轴和y轴成60°角的二维坐标系。先算出来编号位于第几圈,再从此圈的末数一步一步往回走。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int id[2], x[2], y[2];
vector <int> vec;
int dx[] = {1, 0, -1, -1, 0, 1}, dy[] = {0, 1, 1, 0, -1, -1};
int main()
{
int T; scanf("%d", &T);
for(int i = 0; 3 * i * (i - 1) + 1 <= 1000000000; i ++ ) vec.push_back(3 * i * (i + 1) + 1); // 要多储存一圈,因此注意终止条件。从第0圈开始,储存每圈末数
while(T -- ){
scanf("%d%d", &id[0], &id[1]);
for(int i=0; i<2; i++){
int p = lower_bound(vec.begin(), vec.end(), id[i]) - vec.begin();
x[i] = 0, y[i] = - p;
int res = vec[p] - id[i];
for(int j=0; j<6 && res; j ++ ){
int del = min(p, res); res -= del;
x[i] += dx[j] * del;
y[i] += dy[j] * del;
}
}
x[0] -= x[1], y[0] -= y[1];
printf("%d\n", x[0] * y[0] > 0 ? abs(x[0]) + abs(y[0]) + 1 : max(abs(x[0]), abs(y[0])) + 1);
}
return 0;
}