# 棋盘覆盖_分治策略_java实现

public class qipan {
static int MAX_SIZE=16;
static int board[][]=new int[MAX_SIZE][MAX_SIZE];
static int tile=0;
public void chessboard(int tr,int tc,int dr,int dc,int size){
if(size==1)
return;

int t=tile++,s=size/2;
//覆盖左上角
if(dr<tr+s&&dc<tc+s)
chessboard(tr,tc,dr,dc,s);
else{
board[tr+s-1][tc+s-1]=t;
chessboard(tr,tc,tr+s-1,tc+s-1,s);
}
//覆盖右上角
if(dr<tr+s&&dc>=tc+s)
chessboard(tr,tc+s,dr,dc,s);
else{
board[tr+s-1][tc+s]=t;
chessboard(tr,tc+s,tr+s-1,tc+s,s);
}
//覆盖左下角
if(dr>=tr+s&&dc<tc+s)
chessboard(tr+s,tc,dr,dc,s);
else{
board[tr+s][tc+s-1]=t;
chessboard(tr+s,tc,tr+s,tc+s-1,s);
}
//覆盖右下角
if(dr>=tr+s&&dc>=tc+s)
chessboard(tr+s,tc+s,dr,dc,s);
else{
board[tr+s][tc+s]=t;
chessboard(tr+s,tc+s,tr+s,tc+s,s );
}
}

public static void main(String[] args) {
int i,j;

qipan p1=new qipan();
p1.chessboard(0,0,0,1,4);
for(i=0;i<4;i++){
for(j=0;j<4;j++)
System.out.print(board[i][j]+" ");
System.out.print("\n");
}
}
}

1 0 2 2
1 1 0 2
3 0 0 4
3 3 4 4

• 本文已收录于以下专栏：

举报原因： 您举报文章：棋盘覆盖_分治策略_java实现 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)