public class test2_6 {
static int tile = 0;
static int size = 4;
static int[][] board = new int[size][size];
public static void chessBoard(int H,int L,int TH,int TL,int size){
if(size==1) return;
int t = tile++;
int s = size/2;
//1.左上角
//特殊方格在此棋盘里
if(TH<H+s&&TL<L+s){
chessBoard(H,L,TH,TL,s);
}else{//特殊方格不在此棋盘里
board[H+s-1][L+s-1] = t;
chessBoard(H,L,H+s-1,L+s-1,s);
}
//2.右上角
//特殊方格在此棋盘里
if(TH<H+s&&TL>=L+s){
chessBoard(H,L+s,TH,TL,s);
}else{//特殊方格不在此棋盘里
board[H+s-1][L+s] = t;
chessBoard(H,L+s,H+s-1,L+s,s);
}
//3.左下角
//特殊方格在此棋盘里
if(TH>=H+s&&TL<L+s){
chessBoard(H+s,L,TH,TL,s);
}else{//特殊方格不在此棋盘里
board[H+s][L+s-1] = t;
chessBoard(H+s,L,H+s,L+s-1,s);
}
//4.右下角
//特殊方格在此棋盘里
if(TH>=H+s&&TL>=L+s){
chessBoard(H+s,L+s,TH,TL,s);
}else{//特殊方格不在此棋盘里
board[H+s][L+s] = t;
chessBoard(H+s,L+s,H+s,L+s,s);
}
}
public static void main(String[] args) {
int H=0,L=0,TH=0,TL=1;
chessBoard(H,L,TH,TL,size);
for(int i=0;i<size;i++){
for(int j=0;j<size;j++)
System.out.print(board[i][j]+" ");
System.out.println();
}
}
}
输出结果如下:
1 0 2 2
1 1 0 2
3 0 0 4
3 3 4 4