CodeForces 445A-DZY Loves Chessboard(字符串处理)

简介: CodeForces 445A-DZY Loves Chessboard(字符串处理)

题目描述:


DZY loves chessboard, and he enjoys playing with it.


He has a chessboard of n n n rows and m m m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.


You task is to find any suitable placement of chessmen on the given chessboard.


输入:


The first line contains two space-separated integers n n n and m m m (1<=n,m<=100) .


Each of the next n  lines contains a string of m m m characters: the j  -th character of the i  -th string is either "." or "-". A "." means that the corresponding cell (in the i  -th row and the j  -th column) is good, while a "-" means it is bad.


输出:


Output must contain n  lines, each line must contain a string of m m m characters. The j  -th character of the i  -th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.


If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.


样例输入:(有三组)


1 1


.  


2 2


..


..


3 3


.-.


---


--.


样例输出:


B


BW


WB  


B-B


---


--B


题目大意 and 解题思路:


一个棋盘上有一些格子是坏的,另一些是正常的。对于每一个正常的格子,都要在上面放上棋子。 请找到一组解使没有两个相同颜色的棋子相邻(两个格子相邻为它们存在共同的边)


输入格式: 第一行为两个数n,m。(1<=n,m<=100) 下面n行,每个格子上的字符为'-'或'.','-'表示坏掉的格子,'.'表示正常的格子。


输出格式: 输出n行,每行m个字符。第i个字符串的第j个字符应为“W”,“B”或“ - ”。字符“W”是指在格子上放白色的棋子,“B”意味着放黑色的棋子,“ - ”表示坏掉的格子。 如果有多组答案,输出其中的一个。


这道题其实就是简单的字符串处理问题,把字符 "." 转换为 B 或者 W,字符 "-" 不变,那么我们观察样例可以发现规律:对于第i个字符串的第j个字符,只要(i+j)等于奇数,那么就变为 W;偶数,则变为 B。


AC Code:


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main() {
  int n,m;
  char s[101][101];
  scanf("%d %d",&n,&m);
  for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
      scanf(" %c",&s[i][j]);
  for(int i=1;i<=n;i++) {
    for(int j=1;j<=m;j++) {
      if(s[i][j]=='-') {
        printf("-");
      }else {
        if((i+j)&1) {
          printf("W");
        }else {
          printf("B");
        }
      }
    }
    printf("\n");
  }
  return 0;
} 


相关文章
|
4月前
|
C语言
【C语言】鸡兔同笼
【C语言】鸡兔同笼
|
9月前
|
编译器 C语言
爱上C语言:操作符详解(上)
爱上C语言:操作符详解(上)
|
9月前
|
存储 编译器 C语言
爱上C语言:操作符详解(下)
爱上C语言:操作符详解(下)
|
机器学习/深度学习 算法
《算法导论(原书第3版)》一3.2 标准记号与常用函数
本节书摘来自华章出版社《算法导论(原书第3版)》一 书中的第3章,第3.2节,作者:(美)Thomas H.Cormen,Charles E.Leiserson,Ronald L.Rivest,Clifford Stein,更多章节内容可以访问云栖社区“华章计算机”公众号查看。
2441 0
|
存储 C语言
c语言学习第三课—简单介绍数组与操作符
c语言学习第三课—简单介绍数组与操作符
87 0
|
C语言
【维生素C语言】第五章 - 操作符(四)
本章将对C语言操作符进行深度的讲解,将每种操作符都单独拿出来精讲。最后添加了些简单的练习题,并配有详细解析。
123 1
【维生素C语言】第五章 - 操作符(四)
|
C语言
【维生素C语言】第五章 - 操作符(二)
本章将对C语言操作符进行深度的讲解,将每种操作符都单独拿出来精讲。最后添加了些简单的练习题,并配有详细解析。
117 0
【维生素C语言】第五章 - 操作符(二)
|
C语言
【维生素C语言】第五章 - 操作符(五)
本章将对C语言操作符进行深度的讲解,将每种操作符都单独拿出来精讲。最后添加了些简单的练习题,并配有详细解析。
105 1
【维生素C语言】第五章 - 操作符(五)
|
存储 编译器 C语言
【维生素C语言】第五章 - 操作符(一)
本章将对C语言操作符进行深度的讲解,将每种操作符都单独拿出来精讲。最后添加了些简单的练习题,并配有详细解析。
170 0
【维生素C语言】第五章 - 操作符(一)
|
C语言
【维生素C语言】第五章 - 操作符(三)
本章将对C语言操作符进行深度的讲解,将每种操作符都单独拿出来精讲。最后添加了些简单的练习题,并配有详细解析。
106 1
【维生素C语言】第五章 - 操作符(三)

热门文章

最新文章