CodeForces445A DZY Loves Chessboard 【实现 广搜】

A. DZY Loves Chessboard
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
1 1
.
output
B
inputCopy
2 2
..
..
output
BW
WB
inputCopy
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.

思路:一个棋盘有‘.’和’-‘组成,前者为好位置,要求每一个好位置放棋子,棋子有黑白两种。要求不可以有上下左右相邻的两个同色的棋子出现。
我想到的广搜。将每一个‘.’变成’ B’进入队列。然后对队首元素分析四个相邻位置是否有同色。有的话改变颜色,同时进入队列,进行下一次检验。
但是呢,样例到30多就超时了。

 /*试试用广搜行不行*/
#include<iostream>
#include<queue>
using namespace std;
int n,m;
char maze[105][105];
int vis[105][105];
int dx[4] = {-1,1,0,0},dy[4]={0,0,1,-1};
void dfs(int x,int y);

typedef pair<int,int> P;

int main()
{
    cin>>n>>m;
    queue<P> que;
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<m;j++)
        {
            cin>>maze[i][j];
            if(maze[i][j]=='.')
            {
                maze[i][j] = 'B';
                que.push(P(i,j));
            }
        }
    }


    while(!que.empty())
    {
        P p = que.front();
        que.pop();
        for(int i = 0;i<4;i++)
        {
            int nx = p.first + dx[i];
            int ny = p.second + dy[i];
            if(nx>=0&&nx<n&&ny>=0&&ny<m)//先保证是否有越界 
            {
                if(maze[nx][ny] == maze[p.first][p.second])//不是仅仅判断是不是B ,如果B相邻的B都换成W,导致W相邻也是不可以的 
                {
                    if(maze[p.first][p.second] = 'W')
                        maze[nx][ny] = 'B';
                    else maze[nx][ny] = 'W';
                    if(!vis[nx][ny]) que.push(P(nx,ny));//同时对于刚刚更换字符的位置,有可能导致之后他周围字符的重复,所以也需要进入队列再次判断                

                }

            }
        }
    }
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<m;j++)
        {
            cout<<maze[i][j];
        }
        cout<<endl;
    }
    return 0;
}

上网看了大神解法。。。如果(i+j)%2 == 0 就放一个颜色,如果(i+j)% 2 == 1就放另一个颜色,这样导致每一个位置的上下左右都是另一个颜色。
too young….

#include<iostream>
using namespace std;
char maze[105][105];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<m;j++)
        {
            cin>>maze[i][j];
            if(maze[i][j] == '.')
            {
                if((i+j)%2 == 0) maze[i][j] = 'W';
                else maze[i][j] = 'B';
            }
        }
    }
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<m;j++)
        {
            cout<<maze[i][j];
        }
        cout<<endl;
    }

    return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_38293125/article/details/79671943