棋盘覆盖问题(分治)

#include
#include
#include
#include
#include
using namespace std;
const int MAXN=105;
int cnt;
int mat[MAXN][MAXN];

void chessBoard(int tr,int tc,int dr,int dc,int sz)
{
    int s,t;
    if(sz==1) return;
    s=sz/2;
    t=++cnt;

    if(dr<tr+s&&dcs)
    {
        chessBoard(tr,tc,dr,dc,s);
    }else
    {
        mat[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
    {
        mat[tr+s-1][tc+s]=t;
        chessBoard(tr,tc+s,tr+s-1,tc+s,s);
    }

    if(dr>=tr+s&&dcs)
    {
        chessBoard(tr+s,tc,dr,dc,s);
    }else
    {
        mat[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
    {
        mat[tr+s][tc+s]=t;
        chessBoard(tr+s,tc+s,tr+s,tc+s,s);
    }
}

int main()
{
    int sz;
    int dr,dc;
    while(cin>>sz)
    {
        cin>>dr>>dc;
        memset(mat,0,sizeof(mat));
        chessBoard(0,0,dr,dc,sz);
        cnt=0;
        for(int i=0;ifor(int j=0;jprintf("%2d ",mat[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

你可能感兴趣的:(#,减治分治变治)