Little Pigs and Wolves

CodeForces - 116B

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

Input
2 3
PPW
W.P
Output
2
Input
3 3
P.W
.P.
W.P
Output
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的更多相关文章

随机推荐

  1. python基础学习第五天

    li=[1,2,33,-1,'dbssd',[4,5,6],{4:'rfw',5:'re'}]del(li[1])print(li)print(type(li))#访问元素print(li[0])pr ...

  2. HDU1233(Kruskal&Prim两解)

    https://vjudge.net/problem/HDU-1233 还是畅通工程 某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以 ...

  3. Html5 标签二(超链接)

    1.五种超链接形式 2.超链接属性 一 五种超链接 <!DOCTYPE html> <html lang="en"> <head> <me ...

  4. 苹果与Windows双系统时间不同步的解决办法

    步骤:打开C盘>Windows>System32,找到cmd.exe,右键以管理员的身份运行. Reg add HKLM\SYSTEM\CurrentControlSet\Control\ ...

  5. Bootstrap栅栏布局里col-xs-*、col-sm-*、col-md-*、col-lg-*之间的区别及使用方法

    原文:Bootstrap栅栏布局里col-xs-*.col-sm-*.col-md-*.col-lg-*之间的区别及使用方法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  6. Sharding模式

    将数据存储为一组水平的数据分区.这种模式可以在存储和访问大量的数据的时候提高可扩展性. 场景和问题 由单个服务器托管的数据存储可能受到下列限制: 存储空间限制.基于大规模云应用所使用的数据仓库,可能会 ...

  7. Verilog对数据进行四舍五入(round)与饱和(saturation)截位

    转自https://www.cnblogs.com/liujinggang/p/10549095.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件 ...

  8. 通过容器提交镜像(docker commit)以及推送镜像(docker push)笔记

    在本地创建一个容器后,可以依据这个容器创建本地镜像,并可把这个镜像推送到Docker hub中,以便在网络上下载使用. 查看镜像 [root@docker-test1 ~]# docker image ...

  9. 如何启动Intel VT-X及合理利用搜索

    昨天安装Vmware的时候不幸遇到了Vt-X被禁用的麻烦,上网百度了一下才知道要进入Bois进行设置.说起百度就不得不提到模糊搜索这个概念.这个特性的优点和缺点可谓同等突出,有了模糊搜索大家可以在信息 ...

  10. 12.15 Daily Scrum

      Today's Task Tomorrow's Task 丁辛 实现和菜谱相关的餐厅列表. 实现和菜谱相关的餐厅列表.             邓亚梅             美化搜索框UI. 美 ...