分治算法(循环赛事问题,L型棋盘问题)

本文探讨了两种算法实现:一是循环赛事的安排算法,通过递归方式生成比赛时间表;二是解决L型棋盘问题的算法,利用递归与条件判断填充棋盘上的数字,避免特定点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

循环赛事

package com.ycit.sportsschedule;

public class SportsSchedule {
	public void sportsSchedule(int n,int[][] tables){
		if(n==1){
			tables[0][0]=1;
		}else{
			//填充左上角
			int m = n/2;
			sportsSchedule(m,tables);
			//填充右上角
			for(int i=0;i<m;i++){
				for(int j =m;j<n;j++){
					tables[i][j]=tables[i][j-m]+m;
				}
			}
			//填充左下方
			for(int i=m;i<n;i++){
				for(int j=0;j<m;j++){
					tables[i][j]=tables[i-m][j]+m;
				}
			}
			//填充右下方
			for(int i=m;i<n;i++){
				for(int j=m;j<n;j++){
					tables[i][j]=tables[i-m][j-m];
				}
			}
		}
	}
	public static void main(String[] args) {
		int	[][] tables =new int[8][8];
		int n=8;
		SportsSchedule schedule =new SportsSchedule();
		schedule.sportsSchedule(n, tables);
		int c = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				System.out.print(tables[i][j]+" ");
				c++;
				if(c%8==0){
					System.out.println();
				}
			}
		}
	}
}

L型棋盘问题

package com.ycit.chessboradproblem;

public class ChessBoradProblem {
	private int[][] board;
	private int size;
	private int specialRow; //特殊点行下标
	private int specialCol; //特殊点列下标
	private int type=0;
	public ChessBoradProblem(int specialRow,int specialCol,int size){
		super();
		this.specialCol=specialCol;
		this.specialRow=specialRow;
		this.size = size;
		board = new int[size][size];
	}
	public void ChessBoary(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){
			 ChessBoary(specialCol,specialCol,leftRow,leftCol,subsize);
		 }else{
			 //不在左上方区域的右下方为特殊点
			 board[leftRow+subsize-1][leftCol+subsize-1]=n;
			 ChessBoary(leftRow+subsize-1,leftCol+subsize-1,leftRow,leftCol,subsize);
		 }
		 //特殊点在右上方
		 if(specialRow<leftRow+subsize&&specialCol>=leftCol+subsize){
			 ChessBoary(specialCol,specialCol,leftRow,leftCol+subsize,subsize);
		 }else{
			 board[leftRow+subsize-1][leftCol+subsize]=n;
			 ChessBoary(leftRow+subsize-1,leftCol+subsize,leftRow,leftCol+subsize,subsize);
		 }
		 //特殊点在左下方
		 if(specialRow>leftRow+subsize&&specialCol<specialCol-1){
			 ChessBoary(specialCol,specialCol,leftRow+subsize,leftCol,subsize);
		 }else{
			 board[leftRow+subsize][leftCol+subsize-1]=n;
			 ChessBoary(leftRow+subsize-1,leftCol+subsize,leftRow+subsize,leftCol,subsize);
		 }
		 //特殊点在右下方
		 if(specialRow>=leftRow+subsize&&specialCol>=specialCol+subsize){
			 ChessBoary(specialCol,specialCol,leftRow+subsize,leftRow+subsize,subsize);
		 }else{
			 board[leftRow+subsize][leftCol+subsize]=n;
			 ChessBoary(leftRow+subsize,leftCol+subsize,leftRow+subsize,leftRow+subsize,subsize);
		 }
	}
	public void printBoard(int specialRow,int specialCol,int size){
		ChessBoary(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 = 8;
		int specialRow = 0;
		int specialCol = 1;
		ChessBoradProblem boradProblem = new ChessBoradProblem(specialRow, specialCol, N);
		boradProblem.printBoard(specialRow, specialCol, N);
	}
}

 

 

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、付费专栏及课程。

余额充值