java实现分治法---棋盘覆盖问题

/**
 * 分治算法
 *
 * 棋盘覆盖问题
 *
 *   2 2 3 3
 *   2 1 1 3
 *   4 1 0 1
 *   4 4 1 1
 * */
public class ChessBoradProblem {

    private int[][] board;  //棋盘
    private int specialRow; //特殊点的行下标
    private int specialCol; //特殊点的列下标
    private int size;
    private int type = 0;

    public ChessBoradProblem(int specialRow, int specialCol, int size) {
        this.specialRow = specialRow;
        this.specialCol = specialCol;
        this.size = size;
        board = new int[size][size];
    }

    /**
     * @param specialRow 特殊点的行下标
     * @param specialCol 特殊点的列下标
     * @param leftRow 矩阵的左边起点行下标
     * @param leftCol 矩阵的左边起点列下标
     * @param size 矩阵的宽或者高
     * */
    private void ChessBoard(int specialRow,int specialCol,int leftRow,int leftCol,int size){
        if(size == 1){
            return;
        }
        int subSize = size / 2;
        type = type % 4 + 1;
        int n = type;
        //假设特殊点在左上角区域
        if(specialRow < leftRow + subSize && specialCol < leftCol + subSize){
            ChessBoard(specialRow,specialCol,leftRow,leftCol,subSize);
        }else{
            //不在左上角,左上角矩阵的右下角就是特殊点
            board[leftRow + subSize - 1][leftCol + subSize - 1] = n;
            ChessBoard(leftRow + subSize - 1,leftCol + subSize - 1,leftRow,leftCol,subSize);
        }
        //假设特殊点在右上方
        if(specialRow < leftRow + subSize && specialCol >= leftCol + subSize){
                ChessBoard(specialRow,specialCol,leftRow,leftCol + subSize,subSize);
        }else{
            //不在右上方,右上角矩阵的左下角就是特殊点
            board[leftRow + subSize - 1][leftCol + subSize] = n;
            ChessBoard(leftRow + subSize - 1,leftCol + subSize,leftRow,leftCol,subSize);
        }
        //特殊点在左下方
        if(specialRow >= leftRow + subSize && specialCol < leftCol + subSize){
                ChessBoard(specialRow,specialCol,leftRow + subSize,leftCol,subSize);
        }else{
            board[leftRow + subSize][leftCol + subSize - 1] = n;
            ChessBoard(leftRow + subSize,leftCol + subSize - 1,leftRow,leftCol,subSize);
        }
        //特殊点在右下角
        if(specialRow >= leftRow + subSize && specialCol >= leftCol + subSize){
            ChessBoard(specialRow,specialCol,leftRow + subSize,leftCol + subSize,subSize);
        }else{
            board[leftRow + subSize][leftCol + subSize] = n;
            ChessBoard(leftRow + subSize,leftCol + subSize,leftRow,leftCol,subSize);
        }


        for(int i = 0;i < size;i++){
            for(int j = 0;j < size;j++){
                System.out.print(board[i][j] + " ");
            }
            System.out.println();
        }

    }

    public static void main(String[] args){
        ChessBoradProblem c = new ChessBoradProblem(0,1,2);
        c.ChessBoard(0,1,0,0,2);
    }
}

 

展开阅读全文

没有更多推荐了,返回首页