You are given a chess board with nn rows and nn columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board.
A knight is a chess piece that can attack a piece in cell (x2x2, y2y2) from the cell (x1x1, y1y1) if one of the following conditions is met:
- |x1−x2|=2|x1−x2|=2 and |y1−y2|=1|y1−y2|=1, or
- |x1−x2|=1|x1−x2|=1 and |y1−y2|=2|y1−y2|=2.
Here are some examples of which cells knight can attack. In each of the following pictures, if the knight is currently in the blue cell, it can attack all red cells (and only them).
A duel of knights is a pair of knights of different colors such that these knights attack each other. You have to put a knight (a white one or a black one) into each cell in such a way that the number of duels is maximum possible.
Input
The first line contains one integer nn (3≤n≤1003≤n≤100) — the number of rows (and columns) in the board.
Output
Print nn lines with nn characters in each line. The jj-th character in the ii-th line should be W, if the cell (ii, jj) contains a white knight, or B, if it contains a black knight. The number of duels should be maximum possible. If there are multiple optimal answers, print any of them.
Example
input
Copy
3
output
Copy
WBW BBB WBW
Note
In the first example, there are 88 duels:
- the white knight in (11, 11) attacks the black knight in (33, 22);
- the white knight in (11, 11) attacks the black knight in (22, 33);
- the white knight in (11, 33) attacks the black knight in (33, 22);
- the white knight in (11, 33) attacks the black knight in (22, 11);
- the white knight in (33, 11) attacks the black knight in (11, 22);
- the white knight in (33, 11) attacks the black knight in (22, 33);
- the white knight in (33, 33) attacks the black knight in (11, 22);
- the white knight in (33, 33) attacks the black knight in (22, 11).
思路:
由n=3和n=4的圖可知:W無法攻擊其對角相鄰的塊,所以只需要在其對角相鄰的塊中放入W,其它塊中放入B,就可以使攻擊次數最多。可得WB必定相間。
代碼如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
if(i&1){
for(int j=0;j<n;j++){
printf("B");
j++;
if(j<n){
printf("W");
}
}
printf("\n");
}else{
for(int j=0;j<n;j++){
printf("W");
j++;
if(j<n){
printf("B");
}
}
printf("\n");
}
}
return 0;
}
別人dfs代碼:
#include<bits/stdc++.h>
#define maxl 110
using namespace std;
int n,m,ans;
int a[maxl][maxl];
bool vis[maxl][maxl];
char s[maxl];
int tx[9]={0,1,1,2,2,-1,-1,-2,-2};
int ty[9]={0,2,-2,1,-1,2,-2,1,-1};
inline void prework()
{
scanf("%d",&n);
}
inline void dfs(int x,int y,int d)
{
a[x][y]=d;vis[x][y]=true;
int xx,yy;
for(int i=1;i<=8;i++)
{
xx=x+tx[i];yy=y+ty[i];
if(xx<1 || xx>n || yy<1 || yy>n)
continue;
if(vis[xx][yy])
continue;
dfs(xx,yy,3-d);
}
}
inline void mainwork()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(!vis[i][j])
dfs(i,j,1);
}
inline void print()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(a[i][j]==1)
putchar('W');
else
putchar('B');
puts("");
}
}
int main()
{
int t=1;
//scanf("%d",&t);
for(int i=1;i<=t;i++)
{
prework();
mainwork();
print();
}
return 0;
}