2017-08-26 20:18:50
Writer:pprp
The question probably describes:
Have a 2k? 2k checkers, just one square is black, the other is white. Your task is to cover all white squares with an L-shaped card containing 3 squares.
Black squares cannot be overwritten, and any white squares cannot be overwritten by two or more cards at the same time.
Divide and conquer the solution, when the division of the time to determine the state, need what state, the end of conditions
The end condition is that when size becomes 1, it exits;
Desired state, starting point, position of black block, size side length
/*@theme: Checkerboard with L-shaped block heap full @writer:pprp@declare: The most classical @date:2017/8/26 algorithm of divide and conquer*/#include<bits/stdc++.h>#include<iostream>using namespacestd;Const intMAXN =10000;intCHESS[MAXN][MAXN];intNumber ;voidChessboard (intRowintColumnintXintYintSize) { if(Size = =1)return;//Exit Criteria intSS = size/2;//divide and conquer, halve the scale intT = + +Number ; intCenterrow = row +SS; intCentercolumn = column +SS; //start judging Four directions//Upper left corner judgment if(x < Centerrow && y <centercolumn) {chessboard (ROW,COLUMN,X,Y,SS); } Else{Chess[centerrow-1][centercolumn-1] =T; Chessboard (Row,column,centerrow-1, centercolumn-1, SS); } //Upper right corner judgment if(x < Centerrow && y >=centercolumn) {chessboard (ROW,CENTERCOLUMN,X,Y,SS); } Else{Chess[centerrow-1][centercolumn] =T; Chessboard (Row,centercolumn,centerrow-1, CENTERCOLUMN,SS); } //Bottom left corner judgment if(x >= centerrow && y <centercolumn) {chessboard (CENTERROW,COLUMN,X,Y,SS); } Else{Chess[centerrow][centercolumn-1] =T; Chessboard (Centerrow,column,centerrow,centercolumn-1, SS); } //Bottom right corner judgment if(x >= centerrow && y >=centercolumn) {chessboard (CENTERROW,CENTERCOLUMN,X,Y,SS); } Else{Chess[centerrow][centercolumn]=BT; Chessboard (CENTERROW,CENTERCOLUMN,CENTERROW,CENTERCOLUMN,SS); } }intMain () {intSize; intx, y; while(Cin >> Size &&Size) {memset (chess,0,sizeof(chess)); CIN>> x >>y; Chessboard (0,0, x,y,size); Chess[x][y]= Number =1; for(inti =0; i < Size; i++) { for(intj =0; J < Size; J + +) {cout<< chess[i][j]<<"\ t"; } cout<< Endl <<Endl;; } } return 0;}
Classic case of divide and conquer algorithm-checkerboard problem