Codeforces Round #225 (Div. 2) A.Coder

本文探讨了在n*n棋盘上放置C语言棋子(Coders)以避免相互攻击的最大数量,并提供了求解算法及实现代码。通过分析边界条件和对称性,对于偶数和奇数n给出了不同策略,确保棋盘上棋子分布最优。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Coder
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y)(x–1, y)(x, y + 1)and (x, y–1).

Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder.

Input

The first line contains an integer n (1 ≤ n ≤ 1000).

Output

On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.

On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.

If there are multiple correct answers, you can print any.

Sample test(s)
input
2
output
2
C.
.C

24分钟A掉第一题,目测这一次又要长跪10万里。。。
这次题很对我的口味,都是数字处理的,做的很爽。虽然只做出了一道题。。。。大哭
这题跪了一次就是因为坑爹的输出要大些"C"!!!题目中说的明明是小写“c”发火
只要第一个位置放C,就可以保证C最多
因为对n为偶数时来说,C的个数和.的个数相同
n为奇数时,第一个位置放C,可保证边界的两端都为C,因此C的个数最多
嗯,这就是思路,下面又到了贴代码的时候了羡慕
话说我真的开始从C向C++进阶了害羞
#include <iostream>
#include <string>
#include <vector>
#define rep(i,j,k) for(int i=(j); i<k; ++i)
#define maxn  1000

using namespace std;

int main(void){
    int a;
    while(cin >> a){
    int sum = 0;
    if(a%2){
     for(int i=1; i<a; i+=2){
      sum += 2*i;
     }
     sum += a;
    }
    else{
     for(int i=1; i<=(a-1); i+=2)
      sum += 2*i;
    }
     cout << sum << endl;
     rep(i,0,a){
      rep(j,0,a){
       if((i%2&&j%2) || (i%2==0 && j%2==0))
        cout << "C";
       else cout << ".";
      }
      cout << endl;
     }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值