codeforces116B
Little Pigs and Wolves
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.
Examples
2 3
PPW
W.P
2
3 3
P.W
.P.
W.P
0
Note
In the first example, one possible scenario in which two little pigs get eaten by the wolves is as follows.
sol:看上去貌似不会贪心,也不会XJB搜索,于是打了个二分图匹配(智减inf)
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
const int dx[]={-,,,},dy[]={,,-,};
int n,m;
char Map[N][N];
int Id[N][N],Pig=,Wolf=;
namespace Picture
{
int tot=,Next[M],to[M],head[N*N];
inline void add(int x,int y)
{
Next[++tot]=head[x];
to[tot]=y;
head[x]=tot;
return;
}
int Pipei[N*N];
bool Used[N*N];
inline bool dfs(int x)
{
int i;
for(i=head[x];i;i=Next[i])
{
if(Used[to[i]]) continue;
Used[to[i]]=;
if(!Pipei[to[i]]||dfs(Pipei[to[i]]))
{
Pipei[to[i]]=x; return true;
}
}
return false;
}
inline int ErfenPipei()
{
int i,ans=;
for(i=;i<=Wolf;i++)
{
memset(Used,,sizeof Used);
if(dfs(i)) ans++;
}
return ans;
}
}
#define Pic Picture
int main()
{
int i,j,k;
R(n); R(m);
for(i=;i<=n;i++)
{
scanf("%s",Map[i]+);
for(j=;j<=m;j++)
{
if(Map[i][j]=='P') Id[i][j]=++Pig;
else if(Map[i][j]=='W') Id[i][j]=++Wolf;
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++) if(Map[i][j]=='W')
{
for(k=;k<;k++)
{
int xx=i+dx[k],yy=j+dy[k];
if(Map[xx][yy]=='P')
{
Pic::add(Id[i][j],Id[xx][yy]);
}
}
}
}
Wl(Picture::ErfenPipei());
return ;
}
codeforces116B的更多相关文章
随机推荐
- java 面向对象内部类
1.内部类概念:将一个类定义在另一个类的内部,该类就称为内部类 类中定义的内部类特点1) 内部类作为外部类的成员,可以直接访问外部类的成员(包括 private 成员),反之则不行.2) 内部类做为外 ...
- 重装系统之制作U盘启动盘
准备: 1.需要一个大于4G的U盘. 2.一个原版系统. 3.制作U盘启动盘的工具—ultraliso. 一.一个大于4G的U盘 制作启动盘将会格式化U盘,记得做好备份. 二.一个原版系统 至于你要装 ...
- mui 下拉刷新和上拉加载
<body> mui文档提供了两种不同模式的下拉刷新,具体情况看文档,链接:http://dev.dcloud.net.cn/mui/pulldown/ 单 webview 模式和 双 w ...
- [Python]Python 使用 for 循环的小例子
[Python]Python 使用 for 循环的小例子: In [7]: for i in range(5): ...: print "xxxx" ...: print &quo ...
- 将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像
将当前的Ubuntu系统封装成为可以安装(发布)的iso镜像 在使用以上方法安装依赖的时候xresprobe 会找不到安装地址,采用下面的方式: Package xresprobe is not in ...
- 51nod 抽卡大赛
抽卡大赛 链接 分析: $O(n^4)$的做法比较好想,枚举第i个人选第j个,然后背包一下,求出有k个比他大的概率. 优化: 第i个人,选择一张卡片,第j个人选的卡片大于第i个人的概率是$p_j$,那 ...
- echarts柱状图标签显示不完全的问题
echarts 柱状图当x轴标签数目超过一定数目时在小尺寸设备上第一个和最后一个标签不显示(不是重叠),axisLabel设置interval:0也不起作用; 解决办法: 这个问题存在于4.0版本以上 ...
- 关于Win10下IE11只能以管理员身份运行的处理方式
今天无意间发现IE无法启动,后来研究发现只有用管理员身份运行才能打开,初步分析应该是用户权限的问题,在网上百度了一番,找到了处理的方法,在此分享一下 1.win+R 调出“运行”命令,输入“reged ...
- tomcate+keepalived配置双机热备
环境清单: 应用1:192.168.51.101 应用2:192.168.51.75 虚拟IP:192.168.51.179 一.安装Tomcat(参照其他文档): 二.部署应用,并修改响应的端口(9 ...
- sheet制作返回按钮
=HYPERLINK("#目录!A1","目录!A1") =HYPERLINK("#"&A2&"!A1" ...