A very classical problem, to be done by the method of division and treatment.
Each time the chessboard is divided into 4 pieces, for a chessboard that does not have a blank lattice, an L-domino is used to construct the blank lattice.
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<queue>#defineLL Long Longusing namespacestd;intnum;intgrid[ -][ -];inlineBOOLJudgeintSxintSyintSizeintDxintdy) { return(sx<=dx&&dx<=sx+size-1) && (sy<=dy&&dy<=sy+size-1);}voidDfsintSxintSyintSizeintDxintdy) { if(size==1)return; intlen=size/2; ints; //Top left upper right upper left bottom right bottom is 1234 if(Judge (Sx,sy,len,dx,dy)) s=1; Else if(Judge (Sx,sy+len,len,dx,dy)) s=2; Else if(Judge (Sx+len,sy,len,dx,dy)) s=3; Else if(Judge (Sx+len,sy+len,len,dx,dy)) s=4; intex=sx+len,ey=sy+Len; intc=++num; if(s!=1) {Grid[ex-1][ey-1]=B; DFS (Sx,sy,len,ex-1, ey-1); } ElseDFS (Sx,sy,len,dx,dy); if(s!=2) {Grid[ex-1][ey]=C; DFS (Sx,sy+len,len,ex-1, EY); } ElseDFS (Sx,sy+len,len,dx,dy); if(s!=3) {Grid[ex][ey-1]=C; DFS (SX+len,sy,len,ex,ey-1); } ElseDFS (SX+len,sy,len,dx,dy); if(s!=4) {Grid[ex][ey]=C; DFS (SX+len,sy+Len,len,ex,ey); } ElseDFS (SX+len,sy+len,len,dx,dy);}intMain () {intN,dx,dy; //Enter the length of the board, require N is a power of 2, and then enter the coordinates of the blank lattice. The upper left corner of the chessboard is ( while(SCANF ("%d%d%d", &n,&dx,&dy)! =EOF) {memset (grid,0,sizeof(grid)); Grid[dx][dy]=-1; Num=0; DFS (1,1, N,dx,dy); for(intI=1; i<=n; ++i) { for(intj=1; j<=n; ++j) printf ("%3d", Grid[i][j]); printf ("\ n"); } } return 0;}
Division and rule-board overlay problem C + + code implementation