贪心算法棋盘覆盖

#include <iostream> using namespace std;

const int BOARD_SIZE=8;

static int pile=1;

static int BOARD[BOARD_SIZE][BOARD_SIZE];

//tr:棋盘左上角行号

//tc:棋盘左上角列号

//dr:特殊方格行号

//dc:特殊方格列号

//pile:L形块号码

//

void chessboard(int tr,int tc,int dr,int dc,int size){     if(size==1){

        return;

    }

    int t=pile++;

    int s=size/2;

    //覆盖左上角

    if(dr<tr+s&&dc<tc+s){

        chessboard(tr,tc,dr,dc,s);

    }else{       BOARD[tr+s-1][tc+s-1]=t;

      chessboard(tr,tc,tr+s-1,tc+s-1,s);

    }

    //覆盖右上角

    if(dr<tr+s&&dc>=tc+s){

        chessboard(tr,tc+s,dr,dc,s);

    }else{

      BOARD[tr+s-1][tc+s]=t;

      chessboard(tr,tc+s,tr+s-1,tr+s,s);

    }

    //覆盖左下角

    if(dr>=tr+s&&dc<tc+s){

        chessboard(tr+s,tc,dr,dc,s);

    }else{

       BOARD[tr+s][tc+s-1]=t;

       chessboard(tr+s,tc,tr+s,tc+s-1,s);

    }

    //覆盖右下角

    if(dr>=tr+s&&dc>=tc+s){

        chessboard(tr+s,tc+s,dr,dc,s);

    }else{

        BOARD[tr+s][tc+s]=t;

        chessboard(tr+s,tc+s,tr+s,tc+s,s);

    }

} void print(){ for(int i=0;i<BOARD_SIZE;i++){

    for(int j=0;j<BOARD_SIZE;j++){

        cout<<BOARD[i][j]<<” “;

    }

    cout<<endl;

} }

int main()

{    chessboard(0,0,3,4,BOARD_SIZE);

     print();     return 0;

}

    原文作者:贪心算法
    原文地址: https://blog.csdn.net/qq_39147516/article/details/78429833
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞