分治法(球赛安排和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.
也在懵懂中,算法有时需要悟,确实需要悟!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
L型组件填图问题 1.问题描述 设B是一个n×n棋盘,n=2k,(k=1,2,3,…)。用分治法设计一个算法,使得:用若干个L型条块可以覆盖住B的除一个特殊方格外的所有方格。其中,一个L型条块可以覆盖3个方格。且任意两个L型条块不能重叠覆盖棋盘。 例如:如果n=2,则存在4个方格,其中,除一个方格外,其余3个方格可被一L型条块覆盖;当n=4时,则存在16个方格,其中,除一个方格外,其余15个方格被5个L型条块覆盖。 2. 具体要求 输入一个正整数n,表示棋盘的大小是n*n的。输出一个被L型条块覆盖的n*n棋盘。该棋盘除一个方格外,其余各方格都被L型条块覆盖住。为区别出各个方格是被哪个L型条块所覆盖,每个L型条块用不同的数字或颜色、标记表示。 3. 测试数据(仅作为参考) 输入:8 输出:A 2 3 3 7 7 8 8 2 2 1 3 7 6 6 8 4 1 1 5 9 9 6 10 4 4 5 5 0 9 10 10 12 12 13 0 0 17 18 18 12 11 13 13 17 17 16 18 14 11 11 15 19 16 16 20 14 14 15 15 19 19 20 20 4. 设计与实现的提示 对2k×2k的棋盘可以划分成若干块,每块棋盘是原棋盘的子棋盘或者可以转化成原棋盘的子棋盘。 注意:特殊方格的位置是任意的。而且,L型条块是可以旋转放置的。 为了区分出棋盘上的方格被不同的L型条块所覆盖,每个L型条块可以用不同的数字、颜色等来标记区分。 5. 扩展内容 可以采用可视化界面来表示各L型条块,显示其覆盖棋盘的情况。 经典的递归问题, 这是我的大代码, 只是本人很懒, 不想再优化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值