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

news/2025/2/26 13:41:27

题目描述:

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;
} 


http://www.niftyadmin.cn/n/712804.html

相关文章

java清空字符串_Java——JVM篇干货系列(一)

1 基本概念&#xff1a;JVM 是可运行 Java 代码的假想计算机 &#xff0c;包括一套字节码指令集、一组寄存器、一个栈、 一个垃圾回收&#xff0c;堆 和 一个存储方法域。JVM 是运行在操作系统之上的&#xff0c;它与硬件没有直接 的交互。2 运行过程&#xff1a;我们都知道 Ja…

《机器学习实战》读书笔记6:朴素贝叶斯源码

下面是经过我注释并添加 docstring 的朴素贝叶斯分类器源码 from numpy import *def loadDataSet(): () -> list of lists, listReturn a list of lists which are some posts in a forum anda list containing the corresponding class of each post>>> postingL…

UVA572-油田 Oil Deposits(DFS)

样例输入&#xff1a; 1 1 * 3 5 *** **** *** 1 8 ***** 5 5 **** ** *** * ** 0 0 样例输出&#xff1a; 0 1 2 2 AC Code&#xff1a; #include<iostream> #include<cstdio> #include<cstring> using namespace std; char s[101][101];//存入地图的…

如何申请并使用七牛免费证书

如何申请并使用免费证书 本文档介绍如何申请并使用七牛免费证书&#xff0c;主要步骤有&#xff1a; 1.证书申请 2.免费证书使用 2.1创建https加速域名时使用2.2http升级为https时使用免费证书使用说明: 七牛免费证书只能在七牛融合cdn平台使用&#xff0c;不提供下载申请七牛免…

java实现串口ymodem 传输文件_在java中使用SFTP协议安全的传输文件

Java中如何使用基于SSH的文件传输协议(SFTP)将文件从本地上传到远程服务器&#xff0c;或者将文件在两个服务器之间安全的传输。拉勾IT课小编带我们先来了解一下这几个协议&#xff0c;SSH 是较可靠&#xff0c;专为远程登录会话和其他网络服务提供安全性的协议。比如&#xff…

会声会影安装闪退解决办法_会声会影使用中常见问题及解决办法(一)

1、用会声会影刻录好的DVD光盘为什么在家用DVD机上不能播放&#xff1f;可能是以下几个原因&#xff1a;a.在刻录光盘时&#xff0c;设置成miniDVD的格式&#xff0c;这种格式某些DVD机不支持。b.用可檫写光盘刻录DVD光盘对某DVD 机来说也是不能很好兼容的。c.刻录速度太快也会…

周总结8

花费时间&#xff1a;15小时 代码行数&#xff1a;400 学习内容&#xff1a;数据库html 转载于:https://www.cnblogs.com/lianghang/p/11070244.html

2014 年安卓出货首超 10 亿部 占全球市场 81%

市场研究公司Strategy Analytics公布最新数据显示&#xff0c;谷歌(微博)Android移动操作系统在2014年达到一个重要里程碑&#xff0c;即搭载Android操作系统的智能手机在全球出货量首次超过10亿部。2013年&#xff0c;全球Android手机出货量为7.808亿部。Android系统的表现远超…