codeforces 之 Little Pigs and Wolves
B-Little Pigs and Wolves
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Once upon a time there were several little pigs and several wolves on a two-dimensional
grid of size n × m. Each cell in this grid was either empty, containing one little pig, or
containing one wolf.
A little pig and a wolf are adjacent if the cells that they are located at share a side. The
little pigs are afraid of wolves, so there will be at most one wolf adjacent to each little pig.
But each wolf may be adjacent to any number of little pigs.
They have been living peacefully for several years. But today the wolves got hungry. One
by one, each wolf will choose one of the little pigs adjacent to it (if any), and eats the poor
little pig. This process is not repeated. That is, each wolf will get to eat at most one little
pig. Once a little pig gets eaten, it disappears and cannot be eaten by any other wolf.
What is the maximum number of little pigs that may be eaten by the wolves?
Input
The first line contains integers n and m (1 ≤ n, m ≤ 10) which denotes the number of
rows and columns in our two-dimensional grid, respectively. Then follow n lines
containing m characters each — that is the grid description. "." means that this cell is
empty. "P" means that this cell contains a little pig. "W" means that this cell contains a wolf.
It is guaranteed that there will be at most one wolf adjacent to any little pig.
Output
Print a single number — the maximal number of little pigs that may be eaten by the
wolves.
Sample test(s)
input
2 3
PPW
W.P
output
2
input
3 3
P.W
.P.
W.P
output
0
算法分析:待续!
代码:
#include <stdio.h>
#include <string.h> char s[11][11];
int f[11][11];
int g[11][11]; int cnt;
int n,m; void bfs(int dd, int ff )
{
if( dd-1>=0 )
{
if(s[dd-1][ff]=='P' )
{
f[dd-1][ff] ++;
g[dd][ff]=1;
}
}
if(ff-1>=0)
{
if(s[dd][ff-1]=='P' )
{
f[dd][ff-1] ++;
g[dd][ff]=1;
}
}
if(dd+1<n )
{
if(s[dd+1][ff]=='P' )
{
f[dd+1][ff] ++;
g[dd][ff]=1;
}
}
if(ff+1<m)
{
if(s[dd][ff+1]=='P' )
{
f[dd][ff+1] ++;
g[dd][ff]=1;
}
}
} char ch; int main()
{
int i, j, cc;
while(scanf("%d %d%*c", &n, &m) !=EOF )
{
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
ch=getchar();
while(ch!='W' && ch!='P' && ch!='.' )
{
ch=getchar();
}
s[i][j] = ch;
}
}
memset(f, 0, sizeof(f) );
memset(g, 0, sizeof(g));
cnt=0;
cc=0;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++ )
{
if(s[i][j] == 'W' )
{
bfs(i, j) ;
}
}
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(g[i][j]==1)
cc++;
}
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(f[i][j]>0)
cnt++;
}
}
if(cnt >= cc)
printf("%d\n", cc );
else
printf("%d\n", cnt );
}
return 0;
}
codeforces 之 Little Pigs and Wolves的更多相关文章
- CF116B Little Pigs and Wolves 题解
Content 有一张 \(n\times m\) 的地图,其中,\(\texttt{P}\) 代表小猪,\(\texttt{W}\) 代表狼.如果狼的上下左右有一头以上的小猪,那么它会吃掉其中相邻的 ...
- codeforces116B
Little Pigs and Wolves CodeForces - 116B Once upon a time there were several little pigs and several ...
- [题解] Codeforces 1548 C The Three Little Pigs 组合数学,生成函数
题目 首先令\(x=i\)时的答案为\(f_i\) ,令\(f_i\)对应的普通生成函数为\(F(x)\). 很容易发现\(F(x)=\sum_{i=0}^n (1+x)^{3i}\),sigma是在 ...
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- 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 ...
- POJ1149 PIGS [最大流 建图]
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20662 Accepted: 9435 Description ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- 细数国外SEO,SEM,SNS资深博客论坛和站点
如果你有时间,如果有英语还不错能看懂国外的推广营销知识,如果你想做个实战者,如果你想比别人多领先,如果你爱好这个推广行业,如果你不想做河塘里的小鱼,如果····请一个个的看以下的站点,个人觉得会给你另 ...
- 黑苹果+win10双系统折腾笔记
寒假趁机在家折腾一下黑苹果 笔记本配置:神船K610D I7 4600 ,其他配置思路一样,驱动要自己找 镜像和工具:OS X Yosemite 10.10.3 镜像 WIN10 TLSB 2016 ...
- 升级Jekyll 3.0
每一次的升级都得又一次折腾一次,jekyll也不例外 从jekyll 2.5.2 升级为jekyll 3.0.1 错误一: jekyll 3.0.1 | Error: Permission denie ...
- 移动端开发者福利-免费收费api收藏
一 .api 1.https://www.juhe.cn/ 跟百度api集市差不多,超级赞,做好认证就行了,我有20+认证能用的免费api 2.http://apistore.baidu.com/as ...
- vs code编辑器
1.vs code配置 { "editor.tabSize": 2, "workbench.startupEditor": "newUntitledF ...
- Character set 'utf8mb4' is not a compiled character set
近期在一次MySQL数据迁移的过程中遭遇了字符集的问题,提示为"Character set 'utf8mb4' is not a compiled character set".即 ...
- java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0的错误 [转]
一:要解决的问题 我们在尝鲜 JDK1.5 的时候,相信不少人遇到过 Unsupported major.minor version 49.0 错误,当时定会茫然不知所措.因为刚开始那会儿,网上与此相 ...
- syslog,rsyslog and syslog-ng
http://en.wikipedia.org/wiki/Syslog Syslog is a standard for computer message logging. It permits se ...
- Nginx绑定多个域名的方法
nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里. 一.每个域名一个 ...
- PHP session回收机制(转)
由于PHP的工作机制,它并没有一个daemon线程,来定时地扫描session信息并判断其是否失效.当一个有效请求发生时,PHP会根据全局变量 session.gc_probability/sessi ...