Divide and conquer strategy exercises for algorithm design and analysis (part 2)

Divide and conquer strategy to solve problem six: round robin schedule problem

Problem Description

Assuming that n=2^k players want to play round robin, design a game schedule that meets the following requirements:

  • Each player must compete with n-1 other players once
  • Each player can only compete once a day
  • The round robin will last for a total of n-1 days

Examples of 8 players are as follows:
Insert picture description here

problem analysis

From the example diagram, it can be analyzed that this schedule can be generated by diagonal copying in the smallest unit. Please watch the demo diagram I made:
Insert picture description here

Algorithm implementation

#include <iostream>
#include <cmath>
#include <algorithm>

#define N 100
using namespace std;
//存放日程表
int schedule[N][N];
//拷贝方块
void copyBox(int,int,int,int,int);
//生成循环赛日程表
void generateRRS(int);
int main()
{
    
    
    int n;
    cout<<"请输入参加人数:";
    cin>>n;
    generateRRS(n);
    for(int i=0;i<n;i++){
    
    
        for(int j=0;j<n;j++){
    
    
            cout<<schedule[i][j]<<"\t";
        }
        cout<<"\n\n";
    }
    return 0;
}
/*
    拷贝方块实现
    toi,toj为拷贝到的位置的左上角在二维数组中的位置
    fromi,fromj为原始的位置的左上角在二维数组中的位置
    size为拷贝方块的边长
*/
void copyBox(int toi,int toj,int fromi,int fromj,int size){
    
    
    for(int i=0;i<size;i++)
        for(int j=0;j<size;j++)
            schedule[toi+i][toj+j] = schedule[fromi+i][fromj+j];
}
/*
    生成循环赛日程表
    n=2^k,选手个数,参数错误则提示并结束程序
*/
void generateRRS(int n){
    
    

    double temp = log(n)/log(2);
    if(temp != (int)temp || temp == 0){
    
    
        cout<<"函数参数异常!";
        exit(0);
    }

    for(int i=0;i<n;i++)    //初始化第一行
		schedule[0][i]=i+1;

    for(int size=1;size<n;size*=2)
 		for(int i=0;i<n;i+=2*size)	{
    
    
			copyBox(size,size+i,0,i,size);     //左上角拷贝到右下角
			copyBox(size,i,0,size+i,size);     //右上交角拷贝到左下角
		}

}

Divide and conquer strategy to solve problem seven: special board coverage

Problem Description

  In a chessboard composed of 2k×2k squares, exactly one square is different from the other squares. This square is called a special square and the board is called a special chessboard.
  In the chessboard coverage problem, 4 different shapes of L-shaped dominoes as shown in the figure shall be used to cover all squares on a given special chessboard except for the special squares, and any two L-shaped dominoes shall not overlap.
  For a given special chessboard, design a chessboard coverage scheme.
    For example:
    Input:
      2 // k, the side length of the chessboard is 2k
      0 1 //The coordinates of the special square (using a 2k * 2k matrix to represent a chessboard)
    Output:
     2 0 3 3
     2 2 1 3
     4 1 1 5
     4 4 5 5

problem analysis

Insert picture description here
The picture above is a completed 4*4 special checkerboard cover. Let’s analyze it a little bit. If we want to cover it in an orderly manner, the problem is to decompose the problem into sufficiently small sub-problems such as this:
Insert picture description here
because we want to use the same number code to express, we It should be divided into smaller ones. When there is only one empty box, fill in the corresponding domino numbers if it is not a special square.
In a decomposition, we need to put a number in a special corner
  ,
  add it to the
  lower right corner of the upper left corner, add the
  lower left corner of the upper right corner, add the upper left corner of the lower right corner, add the upper right corner of the lower left corner, and
continue This strategy is still used during decomposition.
Insert picture description here

Algorithm implementation

#include <iostream>
#include <cmath>
#include <algorithm>
#define N 100
using namespace std;

//棋盘
int chessboard[N][N];
//骨牌编号
int gno = 0;

//覆盖特殊棋盘函数
void putChessboard(int,int,int,int,int);

int main()
{
    
    
    //棋盘大小
    int n;
    //特殊方格行列号
    int si,sj;

    cout<<"请输入棋盘大小:";
    cin>>n;
    //检测一下n
    double temp = log(n)/log(2);
    if(temp != (int)temp || temp == 0){
    
    
        cout<<"棋盘大小异常!";
        exit(0);
    }

    cout<<"请输入特殊方格位置:";
    cin>>si>>sj;

    putChessboard(0,0,si,sj,n);

    for(int i=0;i<n;i++){
    
    
        for(int j=0;j<n;j++){
    
    
            cout<<chessboard[i][j]<<"\t";
        }
        cout<<"\n\n";
    }
    return 0;
}
/*
    覆盖特殊棋盘函数
    ti,tj 棋盘左上角的坐标
    si,sj 特殊方格的位置
    size 棋盘宽度 不符提示并退出程序
*/
void putChessboard(int ti,int tj,int si,int sj,int size){
    
    
    if(size == 1) return;
    //当前骨牌编号
    int no = ++gno;

    int mSize = size/2; // 分割棋盘

    //处理左上角棋盘
    if(si<ti+mSize && sj<tj+mSize)
        putChessboard(ti,tj,si,sj,mSize);
    else{
    
    
        chessboard[ti+mSize-1][tj+mSize-1] = no;
        putChessboard(ti,tj,ti+mSize-1,tj+mSize-1,mSize);
    }
    //处理左下角棋盘
    if(si>=ti+mSize && sj<tj+mSize)
        putChessboard(ti+mSize,tj,si,sj,mSize);
    else{
    
    
        chessboard[ti+mSize][tj+mSize-1] = no;
        putChessboard(ti+mSize,tj,ti+mSize,tj+mSize-1,mSize);
    }
    //处理右上角棋盘
    if(si<ti+mSize && sj>=tj+mSize)
        putChessboard(ti,tj+mSize,si,sj,mSize);
    else{
    
    
        chessboard[ti+mSize-1][tj+mSize] = no;
        putChessboard(ti,tj+mSize,ti+mSize-1,tj+mSize,mSize);
    }
    //处理右下角棋盘
    if(si>=ti+mSize && sj>=tj+mSize)
        putChessboard(ti+mSize,tj+mSize,si,sj,mSize);
    else{
    
    
        chessboard[ti+mSize][tj+mSize] = no;
        putChessboard(ti+mSize,tj+mSize,ti+mSize,tj+mSize,mSize);
    }
}

Recommended by other articles in this blog

Divide and conquer strategy exercises for algorithm design and analysis (on)

Divide and conquer strategy for algorithm design and analysis

Algorithm design and analysis of recursive algorithm exercises (part 2)

Algorithm design and analysis of recursive algorithm exercises (on)

Digital triangle problem in algorithm design and analysis

Algorithm design and analysis of ZOJ2104- Let the Balloon Rise

Algorithm design and analysis of priority queue and solution ZOJ1167

Guess you like

Origin blog.csdn.net/L333333333/article/details/102640726