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 ...
随机推荐
- Git可视化极简易教程 — Git GUI使用方法
Git可视化极简易教程 — Git GUI使用方法 学习了:http://www.runoob.com/w3cnote/git-gui-window.html
- html5,audio音乐播放器
最终,做了自己原来一直想要实现的事儿.得出的结果是,有些事儿一旦開始做了,那么它就并非非常难. 如今的我,正听着自己的播放器放出的<光辉岁月>写这篇周六清晨的博文.写的不是非常好.但也请各 ...
- 【Python】使用制表符换行符来添加空白
在编程中,在打印时,有时候需要显示出来的数据看着舒服一点,那么使用制表符(\t).换行符(\n)即可轻松实现 >>> print('zhangsan')zhangsan 加入制表符后 ...
- CentOS 6.5语言包裁剪
https://www.ibm.com/developerworks/cn/linux/l-cn-linuxglb/ 浅析 Linux 的国际化与本地化机制 Linux 是一个国际化的操作系统,它的工 ...
- Struts2学习二----------访问Servlet API
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...
- jquery垂直滚动插件一个参数用于设置速度,兼容ie6
利用外层的块级元素负外边距来滚动 1.使用 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://ww ...
- 关于HDFS NFS3的配置
1.在core-site.xml中配置 <property> <name>hadoop.proxyuser.root.groups</name> <value ...
- 小鬼PSer
我是小鬼PSer,主要修图和做网站的,有需要可以call QQ:1805843351 微信:1805843351
- WebRTC for android ios开发官方指南
The WebRTC native code package can be found at: https://chromium.googlesource.com/external/webrtc ht ...
- 多媒体开之之rtp 时间戳和负载类型介绍
(1)时间戳 (2)负载类型 (3)rtp 包头 (1)时间戳 有三个 一个实时间单位 timestamp_increse=(unsigned int)(90000.0 / framerate); / ...