CodeForces - 1221B Knights

CodeForces - 1221B

题目

You are given a chess board with n rows and n 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 (x, y2) from the cell (x1, y1) if one of the following conditions is met:

  • |x1−x2|=2 and |y1−y2|=1 , or
  • |x1−x2|=1 and |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 n (3≤n≤100) — the number of rows (and columns) in the board.


Output

lines with n characters in each line. The j-th character in the i-th line should be W, if the cell (i, j) 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.
题意: 骑士能攻击到8个方向不同颜色的骑士。有W,B两种颜色的骑士,怎样安排骑士的位置使得互相攻击的骑士数目最多。

思维题,在草稿纸上画一画就出来了,用一个二维数组打印出来就可以,注意一下格式

一行BWBW,一行WBWB就可以了。把图画出来就可以看到了。因为骑士能攻击到曼哈顿距离为3的点,这样安排刚好使得曼哈顿距离为3的点,都是不同颜色的。

AC代码

#include <stdio.h>
#include <math.h>
int main()
{
 int n,i,j;
 scanf("%d",&n);
 char q[n][n];
 for (i=0;i<n;i++)
 {  
 	if(i%2==0)
 	{
 		for(j=0;j<n;j++)
 		{
 		if(j%2==0)
 		q[i][j]='B';
 		else q[i][j]='W';
 		}	
 		
	 }
	 if(i%2!=0)
 	{
 		for(j=0;j<n;j++)
 		{
 		if(j%2==0)
 		q[i][j]='W';
 		else q[i][j]='B';
 		}
	 
	  } 	
 }
 for(i=0;i<n;i++)
 {
 	for (j=0;j<n;j++)
 	printf("%c",q[i][j]);
 	printf("\n");
 }
 return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值