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 ...
随机推荐
- Usage of API documented as @since1.6+
Usage of API documented as @since1.6+ File ->Project Structure->Project Settings -> Modules ...
- Azkban上传文件报错installation Failed.Error chunking
azkaban 上传文件报错Caused by: java.sql.SQLException: The size of BLOB/TEXT data inserted in one transacti ...
- 版本号控制软件:TortoiseSVN高速上手
百度百科对于SVN的一点解释: TortoiseSVN是Subversion版本号控制系统的一个免费开源client,能够超越时间的管理文件和文件夹.文件保存在中央版本号库,除了能记住文件和文件夹的每 ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第15章节--开发SP2013工作流应用程序 本章节你将学到: SP中工作流的新功能: 理解工作流管理服务 ...
- DisplayPort的时钟隐藏和时钟恢复
转:DisplayPort的时钟隐藏和时钟恢复 无时钟线的视频数据传输是DP协议的一大特点,将时钟信号隐藏在数据中是传输协议的设计趋势.时钟恢复技术也是DP芯片设计的关键技术.在这说一下在发送端时钟是 ...
- 36:字符串排序SortString
题目描述:编写一个程序,将输入字符串中的字符按如下规则排序. 规则1:英文字母从A到Z排列,不区分大小写. 如,输入:Type 输出:epTy 规则2:同一个英文字母的大小写同时存在时,按照输入顺序排 ...
- Java开启/关闭tomcat服务器
© 版权声明:本文为博主原创文章,转载请注明出处 通过java代码实现Tomcat的开启与关闭 1.项目结构 2.CallTomcat.java package com.calltomcat.test ...
- 《JavaScript》——DOM
DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树,执行开发者加入.移除和改动页 ...
- 2014年7月微软MVP名单揭晓!
微软公司于2001年8月起開始在亚洲与各大基本的第三方站点上的微软技术相关论坛合作,微软称之为"亚洲社区支持"计划. 为了鼓舞大家在论坛中更好地互相帮助,共同提高,微软在全亚 ...
- Redis专题(2):Redis数据结构底层探秘
前言 上篇文章Redis闲谈(1):构建知识图谱介绍了redis的基本概念.优缺点以及它的内存淘汰机制,相信大家对redis有了初步的认识.互联网的很多应用场景都有着Redis的身影,它能做的事情远远 ...