【CodeForces】445A - DZY Loves Chessboard(dfs)

来源:互联网 发布:哪款网络电视机好 编辑:程序博客网 时间:2024/05/17 19:23

题目链接:点击打开链接

A. DZY Loves Chessboard
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

DZY loves chessboard, and he enjoys playing with it.

He has a chessboard of n rows and 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.

Input

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

Each of the next n lines contains a string of 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

Output must contain n lines, each line must contain a string of 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.

Examples
input
1 1.
output
B
input
2 2....
output
BWWB
input
3 3.-.-----.
output
B-B-----B
Note

In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.

In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.

In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.




跟刚开始学dfs的题差不多,直接从第一个点开始放棋子就行了,依次往后扫描。


代码如下:

#include <cstdio>#include <stack>#include <queue>#include <cmath>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3f#define LL long long#define MAX 100int h,w;char mapp[MAX+5][MAX+5];int mx[] = {0,0,1,-1};int my[] = {1,-1,0,0};void dfs(int x,int y,bool flag){mapp[x][y] = flag ? 'B' : 'W';for (int i = 0 ; i < 4 ; i++){int nx = x + mx[i];int ny = y + my[i];if (mapp[nx][ny] == '.')dfs(nx,ny,!flag);}}int main(){scanf ("%d %d",&h,&w);for (int i = 1 ; i <= h ; i++)scanf ("%s",mapp[i]+1);for (int i = 1 ; i <= h ; i++){for (int j = 1 ; j <= w ; j++){if (mapp[i][j] == '.')dfs(i,j,true);}}for (int i = 1 ; i <= h ; i++)printf ("%s\n",mapp[i]+1);return 0;}


0 0