欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分治法

程序员文章站 2022-03-03 08:33:11
...

分治法解决循环赛,即观察矩阵的规律,采用分治法,将大矩阵分为小矩阵,分解至最小部分计算(运用到递归的策略),具体的Java代码实现如下所示:

/**
 * 分治法解决球赛的循环赛
 */
public class Divide {
    public void divide(int[][] tab,int n){
        if (n==1){
            tab[0][0]=1;
        }else{
            //填充矩阵的左上区域
            int m=n/2;
            divide(tab,m);
            //填充右上区域
            for (int i=0;i<m;i++){
                for (int j=m;j<n;j++){
                    tab[i][j]=tab[i][j-m]+m;
                }
            }
            //填充左下区域
            for (int i=m;i<n;i++){
                for (int j=0;j<m;j++){
                    tab[i][j]=tab[i-m][j]+m;
                }
            }
            //填充右下区域
            for (int i=m;i<n;i++){
                for (int j=m;j<n;j++){
                    tab[i][j]=tab[i-m][j-m];
                }
            }
        }
    }
    public static void main(String[] args){
        int[][] tab=new int[8][8];
        int n=8;
        Divide divide=new Divide();
        divide.divide(tab,n);
        int flag=0;
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                System.out.print(tab[i][j]+"  ");
                flag++;
                if (flag%n==0){
                    System.out.println();
                }
            }
        }
    }
}

L棋盘分治法/**

 * L棋盘
 */
public class ChessBoard {
    private int[][] board;//棋盘
    private int specialRow;//特殊点的行坐标
    private int specialCol;//特殊点的列坐标
    private int size;
    private int type=0;

    public ChessBoard(int specialRow, int specialCol, int size) {
        super();
        this.specialRow = specialRow;
        this.specialCol = specialCol;
        this.size = size;
        board=new int[size][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,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 print(int specialRow,int specialCol,int size){
        chessBoard(specialRow,specialCol,0,0,size);
        printResult();
    }

    private void printResult() {
        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=4;
        int specialRow=0;
        int specialCol=1;
        ChessBoard chessBoard=new ChessBoard(specialRow,specialCol,N);
        chessBoard.print(specialRow,specialCol,N);
    }
}
相关标签: 分治法 Java

上一篇: 分治法

下一篇: 分治法