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. PAT A1112 Stucked Keyboard (20 分)——字符串

    On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the char ...

  2. C++多线程的使用

    很多的时候会遇到多线程跑 ,接下来就写了一个 多线程的demo  废话不说直接上代码 #include <iostream> #include <pthread.h> //多线 ...

  3. [TPYBoard - Micropython之会python就能做硬件 开篇]会python就能做硬件!

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 会python就能做硬件! 在写这套教程之前,首先感觉山东萝卜电子科技有限公司(turnip ...

  4. golang 转换markdown文件为html

    使用blackfriday go get -u gopkg.in/russross/blackfriday.v2 go: package markdown import ( "fmt&quo ...

  5. Angularjs 地址联动2.1.1

    这个地址联动是基于 Angularjs 的 效果图如下: 看着是不是可美观了?哈哈!源码如下: <!DOCTYPE HTML> <html lang="zh-CN" ...

  6. GitFlow原理浅析

    一.Git优点 分布式存储 , 本地仓库包含了远程仓库的所有内容 . 安全性高 , 远程仓库文件丢失了也不怕 优秀的分支模型 , 创建/合并分支非常的方便 方便快速 , 由于代码本地都有存储 , 所以 ...

  7. item 10: 比起unscoped enum更偏爱scoped enum

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 一般情况下,在花括号中声明一个name(包括变量名,函数名),这个 ...

  8. python基础学习笔记(七)

    本章介绍如何将语句组织成函数,这样,可以告诉计算机如何做事. 下面编写一小段代码计算婓波那契数列(前两个数的和是第三个数) fibs = [0,1] # 定义一个列表,初始内容是0,1 for i i ...

  9. 全景3d

    Three.js Tour.js Run.js 3D Css3 酷家乐:https://www.kujiale.com/ 爱空间:http://bj.ikongjian.com/?utm_source ...

  10. 2-Twenty Fourth Scrum Meeting-20151230

    前言 因为服务器关闭,我们的开发项目也遭遇停滞一个星期.与网站开发负责人员协商之后,29号开放服务器.我们的项目也能够继续下去.比规定的开发时间(截止为2015/12/29)推迟. 事项安排 1.开发 ...