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相关命令教程
一.在GitHub上创建新项目 (1)在GitHub首页 “New repository”,创建新版本库“test” (2)在本地使用GitBash,将repository clone到本地 git ...
- C 错误处理
C 错误处理 C 语言不提供对错误处理的直接支持,但是作为一种系统编程语言,它以返回值的形式允许您访问底层数据.在发生错误时,大多数的 C 或 UNIX 函数调用返回 1 或 NULL,同时会设置一个 ...
- sql 从另外一张表查询数据存入本表. (有关联的)
UPDATE friends INNER JOIN users ON friends.friendid=users.userid SET friends.friendname=users.userna ...
- 决策树之 C4.5
C4.5 是对 ID3 的一个优化,它依据信息增益率来进行属性选择. 关于决策树.请參见:http://blog.csdn.net/bone_ace/article/details/46299681 ...
- Oracle 中session和processes的初始设置
http://blog.163.com/succu/blog/static/193917174201252911727149/ 1.sessions 在初始化参数所设定的限制中,最为人所知的估计就 ...
- Java储存过程
存储过程:是指保存在数据库并在数据库端执行的程序. CallableStatement 对象为所有的 DBMS 提供了一种以标准形式调用已储存过程的方法.已储存过程储存在数据库中. 对已储存过程的调用 ...
- P13在O(1)时间内删除链表结点
package offer; //在 O(1)时间删除链表结点 public class Problem13 { public static void main(String[] args) { Li ...
- Codeforces Round #316 (Div. 2) (ABC题)
A - Elections 题意: 每一场城市选举的结果,第一关键字是票数(降序),第二关键字是序号(升序),第一位获得胜利. 最后的选举结果,第一关键字是获胜城市数(降序),第二关键字是序号(升序) ...
- Android模糊效果总结
1. 软件模糊 用软件的方法.利用cpu计算,无sdk版本号要求. 效果图: 关键模糊代码 github链接 原文 链接 译文 链接 演示样例 代码 本文地址 :http://blog.csdn.ne ...
- 设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法
东北大亨: 说明:如果想在eclipse的properties文件打开的方式出现source和properties视图就需要添加JBossTools插件 下面介绍如果添加插件: 1.打开官网 http ...