A. Protect Sheep
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
6 6
..S...
..S.W.
.S....
..W...
...W..
......
output
Yes
..SD..
..SDW.
.SD...
.DW...
DD.W..
......
inputCopy
1 2
SW
output
No
inputCopy
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.

不要求最小,只需判断有没有狼直接攻击羊,否则放狗。

//Stay foolish,stay hungry,stay young,stay simple
#include<iostream>
#include<string>
#include<cstdio>
using namespace std; const int MAXN=505; char map[MAXN][MAXN];
string s[MAXN];
int n,m; int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
}
//
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
map[i][j+1]=s[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(map[i][j]=='W'){
if(map[i-1][j]=='S'||
map[i][j-1]=='S'||
map[i+1][j]=='S'||
map[i][j+1]=='S'){
cout<<"No\n";
return 0;
}
}
}
}
cout<<"Yes\n";
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(map[i][j]=='.'){
cout<<"D";
}
else cout<<map[i][j];
}
cout<<endl;
}
return 0;
}

[CF] 948A Protect Sheep的更多相关文章

  1. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)A. Protect Sheep

    http://codeforces.com/contest/948/problem/A   A. Protect Sheep Bob is a farmer. He has a large pastu ...

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

    Bob is a farmer. He has a large pasture with many sheep. Recently, he has lost some of them due to w ...

  3. 题解 CF948A 【Protect Sheep】

    题目链接 额..这道题亮点在: $you$ $do$ $not$ $need$ $to$ $minimize$ $their$ $number.$ 所以说嘛... 直接判断狼的四周有没有紧挨着的羊,没 ...

  4. 【codeforces】【比赛题解】#948 CF Round #470 (Div.2)

    [A]Protect Sheep 题意: 一个\(R*C\)的牧场中有一些羊和一些狼,如果狼在羊旁边就会把羊吃掉. 可以在空地上放狗,狼不能通过有狗的地方,狼的行走是四联通的. 问是否能够保护所有的羊 ...

  5. VK Cup 2018 - Round 1+Codeforces Round #470

    A. Primal Sport 题意:有两个人轮流玩游戏.给出数X(i-1),轮到的人需要找到一个小于X(i-1)的素数x,然后得到Xi,Xi是x的倍数中大于等于X(i-1)的最小的数.现在已知X2, ...

  6. Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)

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

  7. Codeforces Round #470 Div. 2题解

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

  8. hdu 3046 Pleasant sheep and big big wolf 最小割

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046 In ZJNU, there is a well-known prairie. And it a ...

  9. Pleasant sheep and big big wolf HDU - 3046(最小割)

    Pleasant sheep and big big wolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. (水题)洛谷 - P1003 - 铺地毯

    https://www.luogu.org/problemnew/show/P1003 一开始觉得是用树套树来区间修改单点查询,但是发现空间不够开. 看了题解发现这个是静态的问题,而且只问一个点的结果 ...

  2. stylus基础教程,stylus实例教程,stylus语法总结

    stylus特点富于表现力.具有健壮性.功能丰富.动态编码不需要写CSS的冒号.分号.大括号和LESS.SASS功能类似,会这些的入手很快stylus特点安装使用stylus语法(一)选择器(二)变量 ...

  3. hdu 3367 Pseudoforest 最大生成树★

    #include <cstdio> #include <cstring> #include <vector> #include <algorithm> ...

  4. 洛谷p1115 最大子段和

    题目链接: 最大子段和 题目分析: 动态规划O(n)求解,设f[i]表示以i为终点的最大子段和 分两种情况: 若f[i-1]>0,则显然f[i]=f[i-1]+a[i](a[i]必须包含在内) ...

  5. SPRING-BOOT系列之Spring4快速入门

    上节 : spring boot简介 接着上章节的spring boot简介,我们会发现boot是基于spring的,其中最重要的就是spring容器了.那么本章着重介绍spring容器装配自定义be ...

  6. AJPFX关于hashmap和hashtable 的区别

    Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...

  7. void运算符

    void是一元运算符,它出现在操作数之前,操作数可以是任意类型,操作数会照常计算,但忽略计算结果并返回undefined.由于void会忽略操作数的值,因此在操作数具有副作用的时候使用void来让程序 ...

  8. 第一次提交代码到github时经常遇到的问题

    最近两年在OpenStack方面做了一些工作,写了一些实验性的plugin. 本着Open Source的共享精神,想尝试提交到github,以便他人能下载使用. 当你注册完github帐号之后,点击 ...

  9. 更新github上的代码

    昨晚熬夜写完了"git上传本地项目代码到github"的任务,早上来公司先把早上的工作完成后,抽点时间继续来继续更新文章 更新github上的代码 一.克隆代码 1.把大神的代码c ...

  10. 年度精品 XP,32/64位Win7,32/64位Win10系统【电脑城版】

    随着Windows 10Build 10074 Insider Preview版发布,有理由相信,Win10离最终RTM阶段已经不远了.看来稍早前传闻的合作伙伴透露微软将在7月底正式发布Win10的消 ...