The solution of Luogu P5461

Title portal: https://www.luogu.com.cn/problem/P5461

Analysis of the problem, obviously has the nature of recursion, the code is also very simple, simple simulation + recursion.

See the code comments for specific explanations.

code:

1 #include <iostream>
 2  using  namespace std;
 3  int n;
 4  int num [ 15 ] = { 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 }; // Play one Sheet 2 power table 
5  int sum [ 2000 ] [ 2000 ];
 6  void work ( int x1, int y1, intx2, int y2, int flag, int len) { // x1, y1 are the coordinates of the upper left corner, x2, y2 are the coordinates of the lower right corner, flag means (0) not (1) all zeros in this area, len is in this area Side length. (In fact, there is no need for x2y2 with len) 
7      if (flag == 0 ) return ; // If it is all zero, do n’t do it, because the initial value of sum is 0 
8      if (len == 1 ) {
 9          sum [x1] [y1] = 1 ;
 10          return ;
 11      } // If the length of this interval is 1 and not all zeros, assign 1 
12      work (x1, y1, x2-len / 2 , y2-len / 2 , 0 , len / 2 );// The upper left corner interval is 
13      work (x1, y1 + len / 2 , x2-len / 2 , y2, 1 , len / 2 ); // The following are the intervals that need to be recurded again 
14      work (x1 + len / 2 , y1, x2, y2-len / 2 , 1 , len / 2 );
 15      work (x1 + len / 2 , y1 + len / 2 , x2, y2, 1 , len / 2 );
 16  }
 17  int main () {
 18      cin >> n; n = num [n]; // 2 to the power of 
19 19      work ( 1 , 1,n,n,1,n);
20     for(int i=1;i<=n;++i){
21         for(int j=1;j<=n;++j)cout<<sum[i][j]<<" ";
22         cout<<endl;
23     }//输出
24     return 0;
25 }

 

Guess you like

Origin www.cnblogs.com/-YueYang-/p/12731096.html