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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章