棋盘覆盖

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define num 10000
//#include 
using namespace std;
int board[70][70];

void chessboard(int ci,int cj,int tr,int tc,int n,int &t){
    if(n == 1) return ;
    int s = n / 2;
    bool f = true;
    int temp = t++;
    if(tr < ci + s && tc < cj + s){
        f = false;
        chessboard(ci,cj,tr,tc,s,t);
    }
    else {
        board[ci + s - 1][cj + s - 1] = temp;

        chessboard(ci,cj,ci + s - 1,cj + s - 1,s,t );
    }

    if(f && tr < ci + s && tc < cj + n){
        f = false;
        chessboard(ci,cj + s,tr,tc,s, t);
    }
    else {
        board[ci + s - 1][cj + s] = temp;
        chessboard(ci,cj + s,ci + s - 1,cj + s,s, t);
    }

    if(f && tr < ci + n && tc < cj + s){
        f = false;
        chessboard(ci + s, cj,tr,tc,s,t );
    }
    else {
        board[ci + s][cj + s - 1] = temp;
        chessboard(ci + s,cj,ci + s, cj + s - 1,s,t);
    }

    if(f && tr < ci + n && tc < cj + n){
        f = false;
        chessboard(ci + s, cj + s,tr,tc,s, t);

    }
    else{
        board[ci + s][cj + s] = temp;
        chessboard(ci + s, cj + s,ci + s, cj + s,s, t);
    }
}
int main(){
    int n;
    int tr,tc;
    int t = 1;
    cin >> n >> tr >> tc;

    memset(board,-1,sizeof(board));

    chessboard(1,1,tr,tc,n,t);

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            printf("%5d",board[i][j]);
        }
        cout << endl;
    }
}


你可能感兴趣的:(棋盘覆盖)