# Chessboard coverage (C + + | divide and conquer | recursion)

Posted by bogeyman on Mon, 21 Feb 2022 14:50:08 +0100

# Title Description

Description

Chessboard coverage problem: given a chessboard with a size of 2^n2^n small squares, one of the positions has been filled. Now we need to cover the remaining small squares with an L-shape (remove one of the small squares from the large squares composed of 22 small squares). Find out the covering scheme, that is, the small squares in which coordinates are covered by the same L-shaped lattice. Note: the coordinates start at 0. The first grid coordinate in the upper left is (0,0), the second coordinate in the first row is (0,1), the first coordinate in the second row is (1,0), and so on.

Input

Enter the number of test cases for the first behavior. Each subsequent use case has two lines. The first behavior has an n value and the coordinates of a special grid (separated by spaces). The second behavior needs to find the grid coordinates belonging to the same L-shaped grid.

Output

Output the solution of a use case for each behavior. First, output the two coordinates of each use case in the order of row value from small to large, and then column value from small to large; Separated by commas.

Sample Input 1

```1
1 1 1
0 0```

Sample Output 1

`0 1,1 0`

# Guo guonian

I did this question for more than two hours and cried. At first, I wrote it according to my own ideas, but the answer was wrong. Later, according to the online tutorial, bad luck began. The code is almost identical, but it is wrong. Sobbing sobbing

After more than an hour of careful matching (cry. jpg), two errors were found. One was that the coordinates of the upper left corner and the upper right corner were confused, and the other was that lefty was written as lefty, which caused me to go home directly from the laboratory after doing this problem.

There will be an interview tomorrow. I hope good luck.

Back to this topic, the classic recursive method is divided into four areas: upper left, lower left, upper right and lower right. The recursive method is determined according to the location of the special point, which will not be repeated here. It should be noted that the boundary conditions must be clear. Also, if the code can't be adjusted, don't indulge in it. It may be better to see it later. All right, that's it.

# code

```#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

//len, the length of the lattice
//Upper left coordinate, lower right coordinate
//tx ty special position coordinates, located in the square

int number;
void chessBoard(vector<vector<int>> &chess,int leftx,int lefty,int tx,int ty,int size){
//	if((x<leftx&&y<lefty)||(x>leftx+size&&y>lefty+size)){
//		return;
//	}
if(size==1)
return;

int t=++number;

int s=size/2;

//Upper left square
if(tx<leftx+s&&ty<lefty+s){
chessBoard(chess,leftx,lefty,tx,ty,s);
}else{
chess[leftx+s-1][lefty+s-1]=t;
// It's too evil. lefty here was written as leftx at the beginning. It was adjusted for more than two hours. It's really good
chessBoard(chess,leftx,lefty,leftx+s-1,lefty+s-1,s);
}

//Upper right square
if(tx<leftx+s&&ty>=lefty+s){
chessBoard(chess,leftx,lefty+s,tx,ty,s);
}else{
chess[leftx+s-1][lefty+s]=t;
chessBoard(chess,leftx,lefty+s,leftx+s-1,lefty+s,s);
}

//Lower left square
if(tx>=leftx+s&&ty<lefty+s){
chessBoard(chess,leftx+s,lefty,tx,ty,s);
}else{
chess[leftx+s][lefty+s-1]=t;
chessBoard(chess,leftx+s,lefty,leftx+s,lefty+s-1,s);
}

//Lower right square
if(tx>=leftx+s&&ty>=lefty+s){
chessBoard(chess,leftx+s,lefty+s,tx,ty,s);
}else{
chess[leftx+s][lefty+s]=t;
chessBoard(chess,leftx+s,lefty+s,leftx+s,lefty+s,s);
}
}

//tr,tc are the positions of the upper left corner of the grid, and dr,dc are the positions of the special grid
void ChessBoard(vector<vector<int>> &Board,int tr,int tc,int dr,int dc,int size)
{
if(size==1)
return;
int ind=++number;//Number of dominoes
int s=size/2;//The chessboard is divided into four sub problems, that is, half of the row and half of the column

if(dr<tr+s && dc<tc+s)//When the special lattice is in the upper left subproblem
ChessBoard(Board,tr,tc,dr,dc,s);
else//If the special grid is not in the upper left, mark the upper left and lower right corner as black
{
Board[tr+s-1][tc+s-1]=ind;//Cover the lower right corner of the subproblem with the dominoes numbered ind
ChessBoard(Board,tr,tc,tr+s-1,tc+s-1,s);//Continue to cover the rest of the grid
}

if(dr<tr+s && dc>=tc+s)//When the special lattice is in the upper right subproblem
ChessBoard(Board,tr,tc+s,dr,dc,s);
else//If the special grid is not on the top right, mark the lower left corner of the top right as a black grid
{
Board[tr+s-1][tc+s]=ind;
ChessBoard(Board,tr,tc+s,tr+s-1,tc+s,s);
}

if(dr>=tr+s && dc<tc+s)//When the special lattice is in the lower left subproblem
ChessBoard(Board,tr+s,tc,dr,dc,s);
else//If the special grid is not in the lower left, mark the upper right corner of the lower left as a black grid
{
Board[tr+s][tc+s-1]=ind;
ChessBoard(Board,tr+s,tc,tr+s,tc+s-1,s);
}

if(dr>=tr+s && dc>=tc+s)//When the special lattice is in the lower right subproblem
ChessBoard(Board,tr+s,tc+s,dr,dc,s);
else//If the special grid is not in the lower right, mark the upper left corner of the lower right as a black grid
{
Board[tr+s][tc+s]=ind;
ChessBoard(Board,tr+s,tc+s,tr+s,tc+s,s);
}

}

int main(){
int Num;
int n,tx,ty;//(x,y) special lattice, (x1,y1) lattice found
int x,y;//Where to look
cin>>Num;
while(Num--){
cin>>n>>tx>>ty>>x>>y;
vector<vector<int>> chess(pow(2,n),vector<int>(pow(2,n)));
chess[tx][ty]=0;
number=0;
chessBoard(chess,0,0,tx,ty,pow(2,n));
//		ChessBoard(chess,0,0,tx,ty,pow(2,n));
bool flag=false;
for(int i=x-1;i<=x+1;i++){
if(i>=0&&i<pow(2,n)){
for(int j=y-1;j<=y+1;j++){
if(flag==false&&j>=0&&j<pow(2,n)&&!(i==x&&j==y)&&chess[i][j]==chess[x][y]){
cout<<i<<" "<<j<<",";
flag=true;
}else if(flag==true&&j>=0&&j<pow(2,n)&&!(i==x&&j==y)&&chess[i][j]==chess[x][y]){
cout<<i<<" "<<j<<endl;
}
}
}

}
//		for(int i=0;i<pow(2,n);i++){
//			for(int j=0;j<pow(2,n);j++){
//				cout<<chess[i][j]<<" ";
//			}
//			cout<<endl;
//		}
}

return 0;
}
//
//5
//3 0 0 1 1
//4 0 0 15 15
//2 0 0 2 2
//1 0 0 1 1
//1 0 0 1 1
```

Topics: C++ Algorithm partitioning