#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int maxn=10000; int board[maxn][maxn]; int k; int cnt=1; //左闭右闭区间 void solve(int x1,int x2,int y1,int y2,int m,int n) { //cout<<x1<<" "<<x2<<" "<<y1<<" "<<y2<<endl; if(x2-x1==1) { for(int i=x1;i<=x2;i++) for(int j=y1;j<=y2;j++) if(board[i][j]==-1) board[i][j]=cnt; cnt++; return; } int mx=x1+(x2-x1)/2; int my=y1+(y2-y1)/2; if(m<=mx && n<=my) { board[mx+1][my]=board[mx][my+1]=board[mx+1][my+1]=cnt++; solve(x1,mx,y1,my,m,n); solve(mx+1,x2,my+1,y2,mx+1,my+1); solve(x1,mx,my+1,y2,mx,my+1); solve(mx+1,x2,y1,my,mx+1,my); } if(m>mx && n<=my) { board[mx][my]=board[mx][my+1]=board[mx+1][my+1]=cnt++; solve(x1,mx,y1,my,mx,my); solve(mx+1,x2,my+1,y2,mx+1,my+1); solve(x1,mx,my+1,y2,mx,my+1); solve(mx+1,x2,y1,my,m,n); } if(m<=mx && n>my) { board[mx+1][my]=board[mx][my]=board[mx+1][my+1]=cnt++; solve(x1,mx,y1,my,mx,my); solve(mx+1,x2,my+1,y2,mx+1,my+1); solve(x1,mx,my+1,y2,m,n); solve(mx+1,x2,y1,my,mx+1,my); } if(m>mx && n>my) { board[mx+1][my]=board[mx][my]=board[mx][my+1]=cnt++; solve(x1,mx,y1,my,mx,my); solve(mx+1,x2,my+1,y2,m,n); solve(x1,mx,my+1,y2,mx,my+1); solve(mx+1,x2,y1,my,mx+1,my); } } void print_board() { for(int i=0;i<(1<<k);i++) { for(int j=0;j<(1<<k);j++) printf("%3d ",board[i][j]); printf(" "); } } int main() { memset(board,-1,sizeof(board)); cin>>k; int m,n; cin>>m>>n; board[m][n]=0; solve(0,(1<<k)-1,0,(1<<k)-1,m,n); print_board(); return 0; }
注意m,n的改变和起始位置和终止位置的改变