【Fractal】【Luogu P1498】

https://www.luogu.org/problemnew/solution/P1498

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 /*
 7    /\
 8   /__\
 9  /\  /\
10 /__\/__\
11 */
12 int n;
13 char ch[2501][2501];
14 int mypow(int x,int y)
15 {
16     int ans=1;
17      while (y)
 18      {
 19          if (y& 1 )ans*= x;
 20          x*= x;
 21          y/= 2 ;
 22      }
 23      return ans;
 24  }
 25  void dfs( int q, int w, int e , int t)//q controls the recursive boundary of the graph we control the horizontal and vertical coordinates t controls the depth [used to control the size of we, the essence of recursive fractals, must be decomposed into n-1 by n, and then observe the size change of we]
 26  {
 27  //     cout << w << " "<<e << endl; 
28      if (q== 1 )
29     {
30         ch[w][e]='/';
31         ch[w][e+1]='\\';
32         ch[w+1][e-1]='/';
33         ch[w+1][e+2]='\\';
34         ch[w+1][e]='_';
35         ch[w+1][e+1]='_';
36         return;
37     }
38     dfs(q/2,w,e,t*2);
39     dfs(q/2,w+t,e-t,t*2);
40     dfs(q/2,w+t,e+t,t*2);    
41 }
42 int main()
43 {
44     scanf("%d",&n);
45     for(int i = 0 ; i < 2501 ; i++)
46     {
47         int j;
48         for( j = 0 ; j < 2501 ; j++)
49         {
50             ch[i][j]=' ';
51         }
52     }
53     dfs(mypow(2,n-1),0,mypow(2,n)-1,2);
54     for(int i = 0 ; i < mypow(2,n) ; i++)
55     {
56         for(int j = 2500 ; j >= 0 ; j--)
57         {
58             if(ch[i][j]=='\\')
59             {
60                 ch[i][j+1]='\0';
61                 break;
62             }
63         }
64     }
65     for(int i = 0 ; i < mypow(2,n) ; i++)
66     {
67         printf("%s\n",ch[i]);
68     }
69     return 0;
70 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325258065&siteId=291194637