算法之棋盘覆盖问题--java版

java 3 2016-02-29 13:03
女装

package Hell.hao;

public class ChessBoard {

/*int tr;
int tc;//棋盘左上角方格的行列
int dr;
int dc;//分别是特殊方格的行和列*/
int SIZE;
int [][]board;
int count=1;

ChessBoard(){}
ChessBoard(int size){
    SIZE=size;
    board=new int[SIZE][SIZE];
}

public void chessBoard(int tr,int tc,int dr,int dc,int size){
    if(size==1)
        return;
    int cover=count++;
    int s=size/2;
    if(dr<tr+s&&dc<tc+s){
        chessBoard(tr,tc,dr,dc,s);
    }//特殊方格在左上角
    else{
        board[tr+s-1][tc+s-1]=cover;//用L形覆盖其右下角
        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]=cover;
        chessBoard(tr,tc+s,tr+s-1,tc+s,s);
    }
    
    if(dr>=tr+s&&dc<tc+s){
        chessBoard(tr+s,tc,dr,dc,s);//tr+s作为左下角
    }
    else{
        board[tr+s][tc+s-1]=cover;
        chessBoard(tr+s,tc,tr+s,tc+s-1,s);//左下角
    }
    /*
    www.tengxunyun.me
    小兔子腾讯云:转载请告知
    */
    if(dr>=tr+s&&dc>=tc+s){
        chessBoard(tr+s,tc+s,dr,dc,s);
    }
    else{
        board[tr+s][tc+s]=cover;//覆盖左上角
        chessBoard(tr+s,tc+s,tr+s,tc+s,s);//没有的话覆盖其他方格
    }
}

public void show(){
    for(int i=0;i<SIZE;i++){
        for(int j=0;j<SIZE;j++){
            if(j==0){
                System.out.print(board[i][j]);
            }
            else{
                System.out.printf("%4d",board[i][j]);
            }
        }
        System.out.println();
        System.out.println();
    }
}

}

/*
    www.quzhuanpan.com
    去转盘网:转载请告知
*/

主函数:
package Hell.hao;

public class Application_01 {

public static void main(String args[]){
    ChessBoard cb =new ChessBoard(4);
    cb.board[2][2]=0;
    cb.chessBoard(0,0,2,2,4);
    cb.show();
 }

}
运行结果:

女装
文章评论