题目
原题:link
思路(易错点)
-
first
矩形方阵的大小为2 ^ n * 2 ^ n ,
用 “ << “ 来实现cin>>n; m=2<<n-1;
-
second
为实现将每个分出来的矩阵进行左上角清零,我们需要设置三个变量。假设这些变量分别是 x,y,z。那么x代表这个矩阵的起始横坐标位置,y代表这个矩阵的起始纵坐标位置,z则代表目前矩阵的边长。int solve(int x,int y,int z)
-
third
因为矩阵越分越小,所以矩阵的起始横坐标位置和起始纵坐标位置都在变,根据矩形的位置进行递归。solve(b+x,b+y,b); solve(x,b+y,b); solve(b+x,y,b);
代码 (矩阵初始化为1,递归置0)
```
#include<iostream>
using namespace std;
int n;
int m;
int a[1024][1024];
int solve(int x,int y,int z) {
int b;
b=z/2;
if(z==0) {
return 0;
}
for(int i=x; i<x+b; i++)
for(int j=y; j<y+b; j++)
a[i][j]=0;
//cout<<"m-b:"<<m-b<<endl;
solve(b+x,b+y,b);
solve(x,b+y,b);
solve(b+x,y,b);
}
int main() {
cin>>n;
m=2<<n-1;
//cout<<"m:"<<m<<endl;
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++)
a[i][j]=1;
}
solve(0,0,m);
for(int i=0; i<m; i++) {
for(int j=0; j<m; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
```
代码 (递归置1)
#include <iostream>
#include <stdio.h>
using namespace std;
int n , a[1024][1024];
void cal( int n , int x , int y )
{
if( n == 0 ) a[x][y] = 1;
else
{
cal( n - 1 , x + ( 1 << n - 1 ) , y );
cal( n - 1 , x , y + ( 1 << n - 1 ) );
cal( n - 1 , x + ( 1 << n - 1 ) , y + ( 1 << n - 1 ) );
}
}
int main()
{
cin >> n;
cal( n , 0 , 0 );
for( register int i = 0 ; i < 1 << n ; i++ )
for( register int j = 0 ; j < 1 << n ; j++ )
cout << a[i][j] << ( j == ( 1 << n ) - 1 ? '\n' : ' ' );
return 0;
}