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. HDU1233(Kruskal&Prim两解)

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

  2. IDEA Maven Jetty Jrebel 热部署

    准备:1.下载Jrebel的离线安装包,版本是6.4.3版本.            2.下载Jrebel的破解补丁包,同样也是针对6.4.3版本的 Jrebel离线安装包官网下载地址:https:/ ...

  3. 每个大主播都是满屏弹幕,怎么做到的?Python实战无限刷弹幕!

    anmu 是一个开源的直播平台弹幕接口,使用他没什么基础的你也可以轻松的操作各平台弹幕.使用不到三十行代码,你就可以使用Python基于弹幕进一步开发.支持斗鱼.熊猫.战旗.全民.Bilibili多平 ...

  4. USB协议枚举过程详解

    一 枚举过程之文字描述 ?主机集线器监视着每个端口的信号电压,当有新设备接入时便可觉察.(集线器端口的两根信号线的每一根都有15kΩ的下拉电阻,而每一个设备在D+都有一个1.5kΩ的上拉电阻.当用US ...

  5. TRIO-basic变量的状态位

    TRIO运动控制器在应用中,我们一般会用一个VR寄存器的状态位来控制一些报警信号,这样有利于代码的优化,同时和触摸屏设置报警信息大大的方便和节约时间. 首先测试了一下一个寄存器可以设置多少个状态位. ...

  6. Tomcat通过自带的Cluster方式实现Session会话共享环境操作记录

    一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败.要 ...

  7. Session会话与Cookie简单说明

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  8. Linux运维笔记-日常操作命令总结(3)

    文本操作:sed sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作. sed命令行格式为: sed [-nefri] ‘c ...

  9. keepalived概述

    一.HA集群中的相关术语 1.节点(node) 运行HA进程的一个独立主机,称为节点,节点是HA的核心组成部分,每个节点上运行着操作系统和高可用软件服务,在高可用集群中,节点有主次之分,分别称之为主节 ...

  10. HDU-6440-费马小定理

    亏我前几天还学数论呢...没有深入研究费马小定理这个东西...做事情一定要静下心来啊... 题目要求满足(m+n)^p=m^p+n^p,要你定义一个封闭的新的加法和乘法运算 我们知道费马小定理中有两种 ...