Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to wolf attacks. He thus decided to place some shepherd dogs in such a way that all his sheep are protected.

The pasture is a rectangle consisting of R × C cells. Each cell is either empty, contains a sheep, a wolf or a dog. Sheep and dogs always stay in place, but wolves can roam freely around the pasture, by repeatedly moving to the left, right, up or down to a neighboring cell. When a wolf enters a cell with a sheep, it consumes it. However, no wolf can enter a cell with a dog.

Initially there are no dogs. Place dogs onto the pasture in such a way that no wolf can reach any sheep, or determine that it is impossible. Note that since you have many dogs, you do not need to minimize their number.

Input

First line contains two integers R (1 ≤ R ≤ 500) and C (1 ≤ C ≤ 500), denoting the number of rows and the numbers of columns respectively.

Each of the following R lines is a string consisting of exactly C characters, representing one row of the pasture. Here, 'S' means a sheep, 'W' a wolf and '.' an empty cell.

Output

If it is impossible to protect all sheep, output a single line with the word "No".

Otherwise, output a line with the word "Yes". Then print R lines, representing the pasture after placing dogs. Again, 'S' means a sheep, 'W' a wolf, 'D' is a dog and '.' an empty space. You are not allowed to move, remove or add a sheep or a wolf.

If there are multiple solutions, you may print any of them. You don't have to minimize the number of dogs.

Examples
input

Copy
6 6
..S...
..S.W.
.S....
..W...
...W..
......
output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
input

Copy
1 2
SW
output
No
input

Copy
5 5
.S...
...S.
S....
...S.
.S...
output
Yes
.S...
...S.
S.D..
...S.
.S...
Note

In the first example, we can split the pasture into two halves, one containing wolves and one containing sheep. Note that the sheep at (2,1) is safe, as wolves cannot move diagonally.

In the second example, there are no empty spots to put dogs that would guard the lone sheep.

In the third example, there are no wolves, so the task is very easy. We put a dog in the center to observe the peacefulness of the meadow, but the solution would be correct even without him.

题意:输入如题。。在 '.' 中添加 'D' 使得W不能碰到S

题解:直接先判断S旁边有没有W,如果有就NO,然后把所有的点变成D即可。。

这题坑的是输入。。。这场一直在刚输入,发现自己对于最基础的字符串输入掌握的很差劲啊

这题不能这样读数据:

//不能这样!
scanf("%d %d", &n, &m);
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)scanf("%c",&a[i][j]);

原因:读入n、m后,要敲回车完成m的读入,于是回车'\n'便被读入到a[0][0]中了!同样,在二维数组的每一行末输出,都导致了一个\n被读进去

于是这题无限GG

参考别人的代码,总结出了几种读入的办法:

scanf("%d %d", &n, &m);
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)scanf(" %c",&a[i][j]);//注意这里有空格
char a[maxn][maxn];
for (int i=0;i<n;i++)scanf("%s",a[i]);//这里可以没有空格(加空格也阔以),同时读入字符串,前面没有取地址符
//因为题目给的输入方式,一行中的字符之间没有空格,所以可以以字符串的形式读取
//如果输入形式为 S S W W . . W这样的话,scanf遇到空格会停止读入,cin也是

scanf读入空格的方法:scanf("%[^\n]",a[i]);

其中^\n代表以\n为结束符

当然也可以cin.getline(a[i], maxn);

以下是本题的几个完整代码,注意此类题查找相邻位置以及处理边界的方法。

还有可以不用看到'.'就更改为D,可以在确定Yes之后遍历数组,看到'.'就打印D,妙哉。

#include <bits/stdc++.h>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin \
ios_base::sync_with_stdio(0); \
cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;
const int N = 505;
char s[N][N];
bool vis[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};//←←重点在这里
int n, m;
void dfs(int x, int y, const char& c)
{
if (x < 0 || y < 0 || x >= n || y >= m) return;//边界处理(这题因为可以直接全D,所以不一定要dfs)
if (vis[x][y] || s[x][y] == '.') return;
if (s[x][y] != c)
{
cout << "No";
exit(0);
}
vis[x][y] = 1;
for (int i = 0; i < 4; i++) dfs(x + dx[i], y + dy[i], c);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> s[i][j];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] != '.' && !vis[i][j])
dfs(i, j, s[i][j]);
cout << "Yes" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (s[i][j] == '.')
cout << "D";
else
cout << s[i][j];
}
cout << endl;
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0}; const int N=510;
char s[N][N]; int main() {
int n,m; scanf("%d%d",&n,&m);
for (int i=0;i!=n;++i) scanf("%s",s[i]);
int flag=0;
for (int i=0;i!=n;++i) {
for (int j=0;j!=m;++j) {
for (int k=0;k!=4;++k) {
int px=i+dx[k], py=j+dy[k];
if (px>=0 && px<n && py>=0 && py<m && s[i][j]=='W' && s[px][py]=='S') flag=1;
}
}
}
if (flag) {
printf("No\n");
} else {
printf("Yes\n");
for (int i=0;i!=n;++i)
for (int j=0;j!=m;++j)
if (s[i][j]=='.') s[i][j] = 'D';
for (int i=0;i!=n;++i) printf("%s\n",s[i]);
}
}

Codeforces Round #470 (Div. 2) A Protect Sheep (基础)输入输出的警示、边界处理的更多相关文章

  1. Codeforces Round #470 Div. 2题解

    A. Protect Sheep time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. 【二分】Producing Snow @Codeforces Round #470 Div.2 C

    time limit per test: 1 second memory limit per test: 256 megabytes Alice likes snow a lot! Unfortuna ...

  3. Codeforces Round #470 Div. 1

    A:暴力枚举x2的因子,由此暴力枚举x1,显然此时减去其最大质因子并+1即为最小x0. #include<iostream> #include<cstdio> #include ...

  4. Codeforces Round #241 (Div. 2) B. Art Union 基础dp

    B. Art Union time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  5. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  6. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  7. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  8. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  9. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. K8S集群搭建

    K8S集群搭建 摘要 是借鉴网上的几篇文章加上自己的理解整理得到的结果,去掉了一些文章中比较冗余的组件和操作,力争做到部署简单化. K8S组件说明 Kubernetes包含两种节点角色:master节 ...

  2. 【转】Java 面试题问与答:编译时与运行时

    在开发和设计的时候,我们需要考虑编译时,运行时以及构建时这三个概念.理解这几个概念可以更好地帮助你去了解一些基本的原理.下面是初学者晋级中级水平需要知道的一些问题. Q.下面的代码片段中,行A和行B所 ...

  3. await Task.Yield(); 超简单理解!

    上面的代码类似于: Task.Run(() => { }).ContinueWith(t => Do(LoadData())); 意思就是: loadData 如果耗时较长那么上述代码会产 ...

  4. Java 几道常见String面试题

    String s1="abc"; String s2="abc"; System.out.println(s1==s2); System.out.println ...

  5. 实战_Spring_Cloud

    目录 前言 开发环境 源码地址 创建工程 服务注册中心(Eureka) Eureka Server Eureka Client 注册中心高可用 小结 负载均衡(Ribbon) RestTemplate ...

  6. 《利用python进行数据分析》——Numpy基础

    一.创建数组 1.创建数组的函数 array:将输入数据(列表.元组.数组或其他序列类型)转换为ndarray,可用dtype指定数据类型. >>> import numpy as ...

  7. LeetCode 第26题--数组中重复元素

    1. 题目 2.题目分析与思路 3.代码 1. 题目 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超 ...

  8. 深入学习MySQL 02 日志系统:bin log,redo log,undo log

    上一篇文章中,我们了解了一条查询语句的执行过程,按理说这篇应该讲一条更新语句的执行过程,但这个过程比较复杂,涉及到了好几个日志与事物,所以先梳理一下3个重要的日志,bin log(归档日志).redo ...

  9. Python+Excel 操作对比

    前言 从网页爬下来的大量数据需要excel清洗成堆的科学实验数据需要导入excel进行分析作为一名面向逼格的Python程序员该如何合理而又优雅的选择生产力工具呢? 得益于辛勤劳作的python大神们 ...

  10. 创建自定义的RouteBase实现(Creating a Custom RouteBase Implementation) |定制路由系统 |