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. appbar导航

    import 'package:flutter/material.dart';import 'dart:ui'; void main()=>runApp(MyApp()); class MyAp ...

  2. Android学习之基础知识十五 — 最佳UI体验(Material Design实战)

    一.前言 长久以来,大多数人都认为Android系统的UI并不美观,至少没有iOS系统的美观.以至于很多IT公司在进行应用界面设计的时候,为了保证双平台的统一性,强制要求Android端的界面风格必须 ...

  3. 利用 ProtoThreads实现Arduino多线程处理(2)

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyiQQ群:64770604 感谢小V分享给大家的博文. 我在做产品设计的课题的时候,小五义推荐我使用Protothrea ...

  4. 软概(lesson 2):课堂测试

    一.测试题目 二.完成过程 1.设计思想 ①连接mysql数据库 ②设计user类,增加参数 ③设计add类,向数据库内增加内容 ④设计addInput页面,完成录入操作 ⑤设计add页面,接收录入的 ...

  5. Mapreduce打印调试输出

    Mapreduce打印调试内容: 一.启动JobHistoryServer mr-jobhistory-daemon.sh start historyserver [hadoop@node11 sbi ...

  6. 在平衡树的海洋中畅游(四)——FHQ Treap

    Preface 关于那些比较基础的平衡树我想我之前已经介绍的已经挺多了. 但是像Treap,Splay这样的旋转平衡树码亮太大,而像替罪羊树这样的重量平衡树却没有什么实际意义. 然而类似于SBT,AV ...

  7. python中和生成器协程相关yield from之最详最强解释,一看就懂(二)

    一. 从列表中yield  语法形式:yield from <可迭代的对象实例> python中的列表是可迭代的, 如果想构造一个生成器逐一产生list中元素,按之前的yield语法,是在 ...

  8. Python 学习 第八篇:函数2(参数、lamdba和函数属性)

    函数的参数是参数暴露给外部的接口,向函数传递参数,可以控制函数的流程,函数可以0个.1个或多个参数:在Python中向函数传参,使用的是赋值方式. 一,传递参数 参数是通过赋值来传递的,传递参数的特点 ...

  9. item 1:理解template类型的推导

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 一些用户对复杂的系统会忽略它怎么工作,怎么设计的,但是很高兴去知道它完成的一些事.通过这 ...

  10. git bash返回上一级目录

    YITU-LIUMZ+Administrator@yitu-liumz MINGW64 ~/learngit/gitskills (dev)$ cd ..\ 注意 cd 后面有空格 然后就会弹出一个 ...