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 ...
随机推荐
- 给交换机端口设ip
先给端口设vlan,再给vlan设ip [H3C]vlan [H3C-vlan100]port GigabitEthernet // <H3C>sy System View: return ...
- 微信小程序 - gulp插件压缩(代码、图片等)
最后更新时间: 2018.7.18 :更新了所有package.json插件版本以及修复极个别问题. 2018.8.12 : 增加提示,所有标签必须闭合(不然打包会报错) 2018.10.13:需要用 ...
- javascript的window.open()具体解释
通过button打开一个新窗体.并在新窗体的状态栏中显示当前年份. 1)在主窗体中应用下面代码加入一个用于打开一个新窗体的button: <body> <script type=&q ...
- 【Python】创建和使用类
面向对象编程是最有效的软件编写方法之一 创建Dog类 class Dog(): '''一次模拟小狗的简单测试''' def __init__(self,name,age): self.name = n ...
- Android学习笔记(36):Android的两种事件处理方式
Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...
- 【翻译自mos文章】执行utlpwdmg.sql之后报ORA-28003, ORA-20001, ORA-20002, ORA-20003, ORA-20004 错误
执行utlpwdmg.sql之后报ORA-28003, ORA-20001, ORA-20002, ORA-20003, ORA-20004 错误. 适用于: Oracle Server - Ente ...
- JavaScript实现对象数组按不同字段排序
如果有一个对象数组,我们想要依据某个对象属性对数组进行排序.而传递给数组sort()方法的比較函数要接收两个參数,即要比較的值.但是.我们须要一种方式来指明依照哪个属性来排序.要解决问题,能够定义一个 ...
- 不安装Oracle客户端也能使用PL/SQL
解压缩 instantclient_12_1 到 D:\Oracle\instantclient_12_1 在文件夹内建立目录, /NETWORK/ADMIN 在该目录下,新建文件tnsnames.o ...
- 22 nginx配置与集群
一:编译nginx ,并配置 Cd /app/pcre-8.12 ./configure Make && make install Cd nginx-1.2.7 ./configure ...
- 模式识别开发之项目---基于opencv的手势识别
我使用OpenCV2.4.4的windows版本+Qt4.8.3+VS2010的编译器做了一个手势识别的小程序. 本程序主要使到了Opencv的特征训练库和最基本的图像处理的知识,包括肤色检测等等. ...