Codeforces Round 254 (Div. 2) A. DZY Loves Chessboard (模拟,思维,DFS)

本题的定位较为奇怪,总的来看DFS不像是正解。

DZY Loves Chessboard

题面翻译

一个棋盘上有一些格子是坏的,另一些是正常的。对于每一个正常的格子,都要在上面放上棋子。
请找到一组解使没有两个相同颜色的棋子相邻(两个格子相邻为它们存在共同的边)
输入格式:
第一行为两个数n,m。(1<=n,m<=100)
下面n行,每个格子上的字符为’-‘或’.‘,’-‘表示坏掉的格子,’.'表示正常的格子。
输出格式:
输出n行,每行m个字符。第i个字符串的第j个字符应为“W”,“B”或“ - ”。字符“W”是指在格子上放白色的棋子,“B”意味着放黑色的棋子,“ - ”表示坏掉的格子。
如果有多组答案,输出其中的一个

感谢@zhaotiensn 提供的翻译

题目描述

DZY loves chessboard, and he enjoys playing with it.

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

输入格式

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

Each of the next n n n lines contains a string of $ m $ characters: the j j j -th character of the i i i -th string is either “.” or “-”. A “.” means that the corresponding cell (in the i i i -th row and the j j j -th column) is good, while a “-” means it is bad.

输出格式

Output must contain n n n lines, each line must contain a string of m m m characters. The j j j -th character of the i i 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.

样例 #1

样例输入 #1

1 1
.

样例输出 #1

B

样例 #2

样例输入 #2

2 2
..
..

样例输出 #2

BW
WB

样例 #3

样例输入 #3

3 3
.-.
---
--.

样例输出 #3

B-B
---
--B

提示

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 4 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 3 3 chessmen, no matter what their colors are.


DFS

首先给出核心思想,我们只有两种字符可以填并且他们还不能有相同的相邻,那么就只需要保证当前坐标要填入的字符和上一次填的不一样就可以了。

CODE:

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};

int n,m;
char g[N][N];
int cnt;

void dfs(int x,int y,char c,int u){
    if(x<1||x>n||y<1||y>m)return;
    
    g[x][y] = c;
    
    for(int i = 0;i < 4;i++){    
        int a = x + dx[i],b = y + dy[i];
        if(g[a][b] == '.'){
            if(c == 'W')dfs(a,b,'B',u+1);
            else dfs(a,b,'W',u+1);
        }
    }
}

int main(){
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cin >> g[i][j];
            if(g[i][j] == '.')cnt++;
        }
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(g[i][j] == '.'){
                dfs(i,j,'B',0);
            }
        }
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            cout << g[i][j];
        }
        puts("");
    }    

    return 0;
}

模拟

这道题也可以这样想,我们先不管所有的被破坏的方格,加入全部都是好的方格,如果要填入两种字符且让他们不相邻就会很容易找到一种方法,在此基础上判断如果输入的是破坏的方格就让他去覆盖字符就可以了。

在这里采用了坐标相加为奇数填W,偶数填B

CODE:

#include<bits/stdc++.h>
using namespace std;
const int N = 110;

int n,m;

int main(){
    cin >> n >> m;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            char c;cin >> c;
            if(c == '-'){cout << c;continue;}
            cout << ((i+j)&1 ? 'W':'B');
        }
        puts("");
    }
    return 0;
}
  • 23
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值