【CodeForces】445A - DZY Loves Chessboard(dfs)
来源:互联网 发布:哪款网络电视机好 编辑:程序博客网 时间:2024/05/17 19:23
题目链接:点击打开链接
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.
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 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.
1 1.
B
2 2....
BWWB
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 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.
跟刚开始学dfs的题差不多,直接从第一个点开始放棋子就行了,依次往后扫描。
代码如下:
#include <cstdio>#include <stack>#include <queue>#include <cmath>#include <vector>#include <cstring>#include <algorithm>using namespace std;#define CLR(a,b) memset(a,b,sizeof(a))#define INF 0x3f3f3f3f#define LL long long#define MAX 100int h,w;char mapp[MAX+5][MAX+5];int mx[] = {0,0,1,-1};int my[] = {1,-1,0,0};void dfs(int x,int y,bool flag){mapp[x][y] = flag ? 'B' : 'W';for (int i = 0 ; i < 4 ; i++){int nx = x + mx[i];int ny = y + my[i];if (mapp[nx][ny] == '.')dfs(nx,ny,!flag);}}int main(){scanf ("%d %d",&h,&w);for (int i = 1 ; i <= h ; i++)scanf ("%s",mapp[i]+1);for (int i = 1 ; i <= h ; i++){for (int j = 1 ; j <= w ; j++){if (mapp[i][j] == '.')dfs(i,j,true);}}for (int i = 1 ; i <= h ; i++)printf ("%s\n",mapp[i]+1);return 0;}
- 【CodeForces】445A - DZY Loves Chessboard(dfs)
- Codeforces 445A. DZY Loves Chessboard
- Codeforces 445A DZY Loves Chessboard
- Codeforces 445A DZY Loves Chessboard(水题)
- CodeForces 445A DZY Loves Chessboard
- CodeForces 445A DZY Loves Chessboard
- Codeforces 445A DZY Loves Chessboard
- 【Codeforces 445A DZY Loves Chessboard 】+ 思维
- codeforces 445 A. DZY Loves Chessboard
- Codeforces 445 A DZY Loves Chessboard(预处理)
- 【codeforces】-445A- DZY Loves Chessboard(思维)
- A. DZY Loves Chessboard
- A. DZY Loves Chessboard
- Codeforces Round #254 (Div. 2)445A DZY Loves Chessboard(字符串处理)
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
- CF 445A DZY Loves Chessboard
- CodeForcse 445A DZY Loves Chessboard
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard
- Leetcode #407 Trapping Rain Water
- 微信公众号平台开发(二)信息的分类.md
- MongoDB基本使用
- java标识符 运算符
- Docker学习笔记之top,inspect,exec,export命令
- 【CodeForces】445A - DZY Loves Chessboard(dfs)
- C语言之——左旋字符串
- 第九周OJ总结<7>-大奖赛计分
- 技术转行产品总结思考(一)
- jquery加载页面的方法(页面加载完成就执行)
- Java回顾之多线程
- c语言算法;排列过程及其例子罗列
- nginx简介
- 并查集