Division and rule-board overlay problem C + + code implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.