Luo Gu P1948 Nanban totem divide and conquer to find the law

The meaning of problems:
n-= 1-10, the output pattern
is easy to see the pattern when n = k, that is, to n = k-1 when the whole pattern once rightward, rightward shift once a new pattern obtained.
But how to achieve it? ? Thinking. .
I could not help but read this dish, the chicken or solution to a problem. Matrix storage, no problems.
A very interesting idea is backwards storage because storage in order, difficult to deal with when expanding from the lower left
put the entire existing information back pan to the bottom left corner, the upper half empty, it is difficult to achieve, so we put the entire graphic upside down
so that when you can simply extend right down.
But it is still stored in order to meet the characteristic matrix it I think.
Really clever, big brother %%%! !
Also, the record size of the matrix, the use of two parameters, each time expanding both doubled.

void ll(){
 for(int j=1;j<=b;j++){
 for(int i=1;i<=l/2;i++){
  a[j][i+l/2]=a[j][i];
 }}
}
void bb(){
 for(int i=b/2;i<=b-1;i++){
  for(int j=1;j<=l/2;j++){
   a[i+1][j+l/4]=a[i-b/2+1][j];
  }
 }
}
//这是main里面的拓展操作,拓展n-1次
 for(int i=2;i<=n;i++){
  l*= 2;
  ll();
  b*= 2;
  bb();
 }

initialization

//是n=1的情况来着
 a[1][1]=a[2][2]='\\';
 a[1][2]=a[1][3]='_';
 a[1][4]=a[2][3]='/';

Output operation is very wonderful, mainly because of the symmetry of the answer

for(int i=b;i>=1;i--){
  for(int j=1;j<=l;j++){
   if(a[i][j]=='_')cout << '_';
   if(a[i][j]=='/')cout << '\\';
   if(a[i][j]=='\\')cout << '/';
   if(a[i][j]==' ')cout << ' '; 
  }
  cout << endl;
 }
Published 24 original articles · won praise 2 · Views 974

Guess you like

Origin blog.csdn.net/weixin_43521836/article/details/88599559