Reprint please indicate the source: http://blog.csdn.net/u012860063? Viewmode = Contents
Link: http://codeforces.com/problemset/problem/445/A
Dzy loves chessboard, and he enjoys playing with it.
He has a chessboardNRows andMColumns. 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 IntegersNAndM(1? ≤?N,?M? ≤? 100 ).
Each of the nextNLines contains a stringMCharacters:J-Th character ofI-Th string is either "." or "-". A "." means that the corresponding cell (inI-Th row andJ-Th Column) is good, while a "-" means it is bad.
Output
Output must containNLines, each line must contain a stringMCharacters.J-Th character ofI-Th string shoshould 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.
Sample test (s) 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.
The Code is as follows:
#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main(){ int i, j; int n, m; char map[117][117],G[117][117]; while(scanf("%d%d",&n,&m)!=EOF) { getchar(); for(i = 0; i < n; i++) { scanf("%s",map[i]); } for( i = 0; i < n; i++) { for(j = 0; j < m; j++) { if(map[i][j] == '-') G[i][j] = '-'; else if(i %2 == 0) { if(j%2 == 0) { G[i][j] = 'B'; } else G[i][j] = 'W'; } else if(i%2 == 1) { if(j%2 == 1) { G[i][j] = 'B'; } else G[i][j] = 'W'; } } } for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { printf("%c",G[i][j]); } printf("\n"); } } return 0;}