一个赛季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.
也在懵懂中,算法有时需要悟,确实需要悟!