分治法(球賽安排和L形骨牌棋盤覆蓋)

一個賽季N個球隊的比賽安排(N= 2的n次冪)

public class SportSchedule {
    /**
     * 分治法: 球隊聯賽安排
     * 
     * 
     * @param table
     * @param n
     */
    public void scheduleTable(int [][] table, int n ) {
        if(n == 1) {
            table[0][0] =1;
            return;
        }else {
            //左上區域
            int m = n/2;
            scheduleTable(table, m);
            //填充右上區域
            for(int i =0; i<m;i++) {
                for(int j =m;j<n;j++) {
                    table[i][j] = table[i][j-m]+m;
                }
            }
            //填充左下區域
            for(int i =m; i<n;i++) {
                for(int j =0;j<m;j++) {
                    table[i][j] = table[i-m][j]+m;
                }
            }
            //填充右下區域
            for(int i =m; i<n;i++) {
                for(int j =m;j<n;j++) {
                    table[i][j] = table[i-m][j-m]+m;
                }
            }
        }
    }

    public static void main(String[] args) {
        SportSchedule sc = new  SportSchedule();
        int n = 4;
        int [][] table = new int [n][n];
        sc.scheduleTable(table, n);
        for(int i=0;i<n;i++ ) {
            for(int j=0;j<n;j++) {
                System.out.print(table[i][j]+" ");
            }
            System.out.println();
        }
    }
}

L形骨牌棋盤覆蓋

public class ChessBoardProblem {
    private int[][] board;//棋盤
    private int specialRow;//特殊點的行下標  L 對的角的下標
    private int specialCol;//特殊點的列下標
    private int size;
    private int type =0;
    public int getSpecialRow() {
        return specialRow;
    }
    public void setSpecialRow(int specialRow) {
        this.specialRow = specialRow;
    }
    public int getSpecialCol() {
        return specialCol;
    }
    public void setSpecialCol(int specialCol) {
        this.specialCol = specialCol;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getType() {
        return type;
    }
    public void setType(int type) {
        this.type = type;
    }


    public ChessBoardProblem( int size) {
        super();
        this.board = new int [size][size];
        this.size = size;
    }
    /**
     * 
     * @param specialRow  特殊點的行下標
     * @param specialCol  特殊點的列下標
     * @param leftRow  矩陣的左邊起點行下標
     * @param leftCol  矩陣的左邊起點列下標
     * @param size   矩陣的邊
     */
    public 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, 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+subSize, 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+subSize, leftCol+subSize, subSize);
        }

    }

    public void printBoard() {
        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) {
        int N  =  8;
        ChessBoardProblem cbp =  new  ChessBoardProblem(N);
        int row = 3;
        int col = 3;
        cbp.ChessBoard(row ,col, 0, 0, N);
        cbp.printBoard();
    }
}

分治法的核心思想就是把大問題變成小問題,最終解決問題n=1.
也在懵懂中,算法有時需要悟,確實需要悟!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章