Fractal City (Recursive)

topic:

image

 

analysis

In general, this kind of problem has certain rules. According to observation, it can be found that N = 2 is the result of copying or copying rotation.

 

Let me talk about the coordinate calculation formula after rotation in linear algebra \

Use matrix multiplication to calculate the rotated (x, y)

image

 

Then proceed as shown

The picture is too large to be enlarged, so to view the picture, please visit: https://img2020.cnblogs.com/blog/1938255/202004/1938255-20200413013807377-2043893218.png

Then write the code, recursively as a sub-problem

 

  1 #include <iostream>
   2 #include <algorithm>
   3 #include <cstring>
   4 #include <cmath>
   5  using  namespace std;
   6  typedef  long  long ll;
   7  typedef pair <ll, ll> PLL;
   8 PLL calc (ll n, ll m) {
   9      if (n == 0) return make_pair (0,0);
 10      ll len = 1ll << n-1, cnt = 1ll << 2 * n -2; // len is the side length Half, cnt is the number of urban areas of a sub-block 
11      pair <ll, ll> pos = calc (n-1, m% cnt); // Recursion into sub-problems and then the problem 
12     ll x = pos.first, y = pos.second;
 13     ll z = m/cnt;
 14     if (z == 0) return make_pair(y,x);
 15     if (z == 1) return make_pair(x,y + len);
 16     if (z == 2) return make_pair(x + len, y + len);
 17     return make_pair(2 * len - 1 - y, len - 1 - x);
 18 
 19 }
 20 int main(){
 21     int t;
 22     cin >> t;
 23     while(t--){
 24         ll N, A, B;
 25         cin >> N >> A >> B;
 26          pair <ll, ll> ac = calc (N, A-1); // Find A-1 to ensure that 
27          pair <ll, ll in the corresponding sub-block > bc = calc (N, B-1);
 28          double x = ac.first-bc.first, y = ac.second-bc.second;
 29          printf (" % .0lf \ n ", sqrt (x * x + y * y) * 10);
 30      }
 31      return 0;
 32 }

Guess you like

Origin www.cnblogs.com/rstz/p/12689039.html