棋盘覆盖问题
1 #include<iostream> 2 using namespace std; 3 #define MAX 1024 4 int a[MAX][MAX]; 5 int num=1; 6 void solve(int fromx,int fromy,int x,int y,int size) 7 { 8 if(size==1) 9 return; 10 if(x<fromx+size/2&&y<fromy+size/2) 11 { 12 a[fromx+size/2][fromy+size/2]=num; 13 a[fromx+size/2-1][fromy+size/2]=num; 14 a[fromx+size/2][fromy+size/2-1]=num++; 15 solve(fromx,fromy,x,y,size/2); 16 solve(fromx,fromy+size/2,fromx+size/2-1,fromy+size/2,size/2); 17 solve(fromx+size/2,fromy,fromx+size/2,fromy+size/2-1,size/2); 18 solve(fromx+size/2,fromy+size/2,fromx+size/2,fromy+size/2,size/2); 19 } 20 if(x<fromx+size/2&&y>=fromy+size/2) 21 { 22 a[fromx+size/2][fromy+size/2]=num; 23 a[fromx+size/2-1][fromy+size/2-1]=num; 24 a[fromx+size/2][fromy+size/2-1]=num++; 25 solve(fromx+size/2,fromy,x,y,size/2); 26 solve(fromx,fromy+size/2,fromx+size/2-1,fromy+size/2,size/2); 27 solve(fromx,fromy,fromx+size/2-1,fromy+size/2-1,size/2); 28 solve(fromx+size/2,fromy+size/2,fromx+size/2,fromy+size/2,size/2); 29 } 30 if(x>=fromx+size/2&&y<fromy+size/2) 31 { 32 a[fromx+size/2][fromy+size/2]=num; 33 a[fromx+size/2-1][fromy+size/2]=num; 34 a[fromx+size/2-1][fromy+size/2-1]=num++; 35 solve(fromx+size/2,fromy,x,y,size/2); 36 solve(fromx,fromy+size/2,fromx+size/2-1,fromy+size/2,size/2); 37 solve(fromx,fromy,fromx+size/2-1,fromy+size/2-1,size/2); 38 solve(fromx+size/2,fromy+size/2,fromx+size/2,fromy+size/2,size/2); 39 } 40 if(x>=fromx+size/2&&y>=fromy+size/2) 41 { 42 a[fromx+size/2-1][fromy+size/2-1]=num; 43 a[fromx+size/2-1][fromy+size/2]=num; 44 a[fromx+size/2][fromy+size/2-1]=num++; 45 solve(fromx+size/2,fromy+size/2,x,y,size/2); 46 solve(fromx,fromy+size/2,fromx+size/2-1,fromy+size/2,size/2); 47 solve(fromx+size/2,fromy,fromx+size/2,fromy+size/2-1,size/2); 48 solve(fromx,fromy,fromx+size/2-1,fromy+size/2-1,size/2); 49 } 50 51 } 52 int main() 53 { 54 int k,x,y,i,j; 55 while(cin>>k) 56 { 57 cin>>x>>y; 58 int size=1<<k; 59 a[x][y]=0; 60 solve(0,0,x,y,size); 61 for(i=0;i<size;i++) 62 { 63 for(j=0;j<size;j++) 64 cout<<a[i][j]<<" "; 65 cout<<endl; 66 } 67 } 68 return 0; 69 }
输入:对每个测试有两行,第一行是k,第二行是特殊方格位置坐标x,y。
输出:边长为2的k次幂的方阵,特殊方格编号为0。
eg.
输入:
2
0 1
输出:
2 0 3 3
2 2 1 3
4 1 1 5
4 4 5 5