您的位置:首页 > 其它

A. DZY Loves Chessboard (CF)

2014-07-07 08:55 281 查看
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.

Sample test(s)

Input
1 1
.


Output
B


Input
2 2
..
..


Output
BW
WB


Input
3 3
.-.
---
--.


Output
B-B---
--B


#include <iostream>
using namespace std;
#define M 1000
char mat[M][M];
int vis[M][M],v[M][M];
void F(int x,int y,char a)
{   
        vis[x][y]=1;
        if(a=='B')
        mat[x][y]='W';
        else
        mat[x][y]='B';
    if(mat[x+1][y]=='.'&&vis[x+1][y]==0)
        F(x+1,y,mat[x][y]);
    if(mat[x][y+1]=='.'&&vis[x][y+1]==0)
        F(x,y+1,mat[x][y]);
    if(mat[x-1][y]=='.'&&vis[x-1][y]==0)
        F(x-1,y,mat[x][y]);
    if(mat[x][y-1]=='.'&&vis[x][y-1]==0)
        F(x,y-1,mat[x][y]);
}
int main()
{
 int n,m,i,j;
 while(scanf("%d%d",&n,&m)!=EOF)
 {   getchar();
     memset(mat,'-',sizeof(mat));
     memset(vis,0,sizeof(vis));
     for(i=1;i<=n;i++)
     {
         for(j=1;j<=m;j++)
         scanf("%c",&mat[i][j]);
         getchar();
     } 
     for(i=1;i<=n;i++)
     {
         for(j=1;j<=m;j++)
             if(mat[i][j]=='.'&&vis[i][j]==0)
                 F(i,j,'W');
             continue;
     }
      for(i=1;i<=n;i++)
     {
         for(j=1;j<=m;j++)
         printf("%c",mat[i][j]);
         printf("\n");
     } 
 }
 return 0;
}

本来想打个奇偶BW的底表,不过卡壳了。因为在学搜索,所以把它用搜索做了一下,所以要考虑回车的影响。

别人的代码感觉是数学问题,没怎么明白。

我的感觉是深度优先搜索,旁边是B我就用w.是-我就不管。结果最后又被数组坑了一下,最后的数据正好是100,我又围了一圈。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: