DZY Loves Chessboard
Description
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 andm(1 ≤ n, m ≤ 100).
Each of the next n lines contains a string ofm characters: thej-th character of thei-th string is either "." or "-". A "." means that the corresponding cell (in thei-th row and thej-th column) is good, while a "-" means it is bad.
Output
Output must contain n lines, each line must contain a string ofm characters. Thej-th character of thei-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.
Sample Input
1 1 .
B
2 2 .. ..
BW WB
3 3 .-. --- --.
B-B --- --B
今天看到别人写的,发现我想复杂了,根本不需要这么做,引用一下:#include<stdio.h> #define N 100 char chess[N][N]; int n,m; void Fillin(int i,int j) { int di,dj; if(chess[i][j]=='-')return ; for(di=-1; di<=1; di++) for(dj=-1; dj<=1; dj++) { //向该点的四个相邻格子扩展 if(di==dj||di==-dj|| i+di<0||i+di>=n|| j+dj<0||j+dj>=m|| chess[i+di][j+dj]!='.')continue; if(chess[i][j]=='W') chess[i+di][j+dj]='B'; else chess[i+di][j+dj]='W'; Fillin(i+di,j+dj); } } int main() { int i,j; scanf("%d%d",&n,&m); getchar(); for(i=0; i<n; i++) { for(j=0; j<m; j++) scanf("%c",&chess[i][j]); getchar(); } for(i=0; i<n; i++) for(j=0; j<m; j++) if(chess[i][j]=='.') { chess[i][j]='W'; Fillin(i,j); } for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%c",chess[i][j]); printf("\n"); } return 0; }
#include<stdio.h> int main() { char a[101][101]; int n,m,i,j; char c; scanf("%d%d",&n,&m); getchar(); for(i=0;i<n;i++) { for(j=0;j<m;j++) { scanf("%c",&a[i][j]); if(a[i][j]=='.') a[i][j]=(i+j)%2==0?'W':'B'; } getchar(); } for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%c",a[i][j]); printf("\n"); } return 0; }
</pre>