Long curve [fractal] Law implemented to violence from the DFS

Copyright: https: //blog.csdn.net/qq_40831340 welcome treatise https://blog.csdn.net/qq_40831340/article/details/91391489

Long curve
dragon curve is based on a simple mathematical rules drawn one kind of curve, which has the following form. Curve segment starting from a simple, according to certain rules to complete the conversion curve line segment. Every time a transformation referred to as "a complete transformation generation", and each generation is completed, the curve will evolve to a more complex form. Such like "shape of the small portion is enlarged one, it exhibits the overall shape of the structure is very similar pattern" is fractal.
Long method to draw curves for the time being is called dragon curve string it! Long strings by the curves X, Y, F, +, - composition.
Then, to draw the curve on the long starting from a point below the curve can be drawn.

F: a grid moving forward and draw lines.
+: Rotating 90 degrees left.
-: Rotate right 90 degrees.
X, Y: ignored.
0 Generation Long curve drawn string is FX. From the start the next generation, in the following manner using the previous generation replacement character strings, thereby obtaining a long string of the current generation curves.
X-> YF + X-
Y-> the Y + FX

The alternative formula above, there is the Second Generation Long curves of all strings.
The first generation: FX + YF
second generation: FX + YF + FX-YF
we would like to request the generation n Long curve string. However, given the answer may be very long, so I just want to calculate the starting p-th character string of length l characters. Please write a program to achieve this functionality.

Enter
the first line number C (C <= 50) of test inputs. A first row of each test three integers are input, i.e., dragon curve generation n (0 <= n <= 50), p and l (1 <= p <= 1 000 000 000,1 <= l <= 50). Long curve length string generation n can be assumed to always be greater than or equal to the value p + l.
Output
each test case generation n output curve long string in a p-th character line begins, the output l characters.

Examples of input
. 4
0 2. 1
. 1. 5. 1
2. 6. 5
42 is 30 764 853 475
Example Output
FX
FX + YF
+ the Y-FX
FX FX-YF-YF + + + FX-YF-FX-FX-YF YF-

first codes violence

#include<iostream>
using namespace std;

void dfs(string str, int N) {
    if(N == 0) {
        cout << str;
        return;
    }
    for(int i = 0; i < str.size(); ++i) {
        if(str[i] == 'X')
            dfs("X+YF", N - 1);
        else if(str[i] == 'Y')
            dfs("FX-Y", N - 1);
        else
            cout << str[i];
    }
}

int main() {
    int n;
    cin >> n;
    dfs("FX", n);
}

Here we broke the data of Table 1-5

第一代:FX+YF // 5
第二代:FX+YF+FX-YF // 11
第三代:FX+YF+FX-YF+FX+YF-FX-YF // 23
第四代:FX+YF+FX-YF+FX+YF-FX-YF+FX+YF+FX-YF-FX+YF-FX-YF // 47
五:	    FX+YF+FX-YF+FX+YF-FX-YF+FX+YF+FX-YF-FX+YF-FX-YF  \
		+FX+YF+FX-YF+FX+YF-FX-YF-FX+YF+FX-YF-FX+YF-FX-YF

Number of characters: sum [i] = (sum [i-1] << 1) + 1;

Letters Law: Section FXYFFXYF cyclic
symbol pattern:

1.    +
2.    + + -
3.    + + - + + - -
4.    + + - + + - - + + + - + + - -
5. 	  + + - + + - - + + + - + + - - + + + - + + - - + + + - + + - -

We found void before and after each round the interpolated character + -;
so on and then multiplying the same time a + - sequentially inserted
which is why the number of characters: sum [i] = (sum [i-1] << 1) + 1; this is the law

Then we choose each time the non-0, 2, FXY 4 position other than the first table / 3
and then continue / 2 odd bit is reached
, for example, 3rd generation

  1 2 3 4 5 6 7
  + + - + + - - 
  第一位 + 1 mod 4  = 2 :+
  第二位 / 2 之后 + 1 mod 4 = 2:+
  第三位 +1 mod 4 = 0 :-
  第四位 / 2 / 2 + 1 mod 4 = 2 :+
  第五位 + 1 mod 4 = 2 : +
  第六位 / 2 + 1 mod 4 =  0 : -
  第七位 + 1 mod 4 = 0 : -

	大体上是这样的一般规律 其实mod 2在搞规律也行 我找的时候当4的循环节了 同时mod 4也是因为我前面没有处理好
#include<bits/stdc++.h>
using namespace std;

void get_c(int &x) {
    while(x % 2 == 0) {
        x /= 2;
    }
} // 确定第 x 位 属于O, E层次

void output(int x) {
    if(x % 6 == 2) // 循环节 2p
            cout << "X";
        else if(x % 6 == 4) // 循环节 3p
            cout << "Y";
        else if(x % 3 == 0) {
            x = x / 3;
            get_c(x); // 降 k
            if((x + 1) % 4 == 2) {
                cout << "+";
            } else
                cout << "-";
        } else
            cout << "F"; // 循环节 1p
}

void sol(int p, int l) {
    for(int i = 0; i < l; i++) {
        int k = p + i;
        output(k);
    }
    cout << endl;
}

int main() {
    int cas;
    for(cin >> cas; cas--;) {
        int n, p, l;
        cin >> n >> p >> l;
        sol(p, l);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_40831340/article/details/91391489
law