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
  1. 2 3
    PPW
    W.P
Output
  1. 2
Input
  1. 3 3
    P.W
    .P.
    W.P
Output
  1. 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)

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef int ll;
  4. inline ll read()
  5. {
  6. ll s=;
  7. bool f=;
  8. char ch=' ';
  9. while(!isdigit(ch))
  10. {
  11. f|=(ch=='-'); ch=getchar();
  12. }
  13. while(isdigit(ch))
  14. {
  15. s=(s<<)+(s<<)+(ch^); ch=getchar();
  16. }
  17. return (f)?(-s):(s);
  18. }
  19. #define R(x) x=read()
  20. inline void write(ll x)
  21. {
  22. if(x<)
  23. {
  24. putchar('-'); x=-x;
  25. }
  26. if(x<)
  27. {
  28. putchar(x+''); return;
  29. }
  30. write(x/);
  31. putchar((x%)+'');
  32. return;
  33. }
  34. #define W(x) write(x),putchar(' ')
  35. #define Wl(x) write(x),putchar('\n')
  36. const int N=,M=;
  37. const int dx[]={-,,,},dy[]={,,-,};
  38. int n,m;
  39. char Map[N][N];
  40. int Id[N][N],Pig=,Wolf=;
  41. namespace Picture
  42. {
  43. int tot=,Next[M],to[M],head[N*N];
  44. inline void add(int x,int y)
  45. {
  46. Next[++tot]=head[x];
  47. to[tot]=y;
  48. head[x]=tot;
  49. return;
  50. }
  51. int Pipei[N*N];
  52. bool Used[N*N];
  53. inline bool dfs(int x)
  54. {
  55. int i;
  56. for(i=head[x];i;i=Next[i])
  57. {
  58. if(Used[to[i]]) continue;
  59. Used[to[i]]=;
  60. if(!Pipei[to[i]]||dfs(Pipei[to[i]]))
  61. {
  62. Pipei[to[i]]=x; return true;
  63. }
  64. }
  65. return false;
  66. }
  67. inline int ErfenPipei()
  68. {
  69. int i,ans=;
  70. for(i=;i<=Wolf;i++)
  71. {
  72. memset(Used,,sizeof Used);
  73. if(dfs(i)) ans++;
  74. }
  75. return ans;
  76. }
  77. }
  78. #define Pic Picture
  79. int main()
  80. {
  81. int i,j,k;
  82. R(n); R(m);
  83. for(i=;i<=n;i++)
  84. {
  85. scanf("%s",Map[i]+);
  86. for(j=;j<=m;j++)
  87. {
  88. if(Map[i][j]=='P') Id[i][j]=++Pig;
  89. else if(Map[i][j]=='W') Id[i][j]=++Wolf;
  90. }
  91. }
  92. for(i=;i<=n;i++)
  93. {
  94. for(j=;j<=m;j++) if(Map[i][j]=='W')
  95. {
  96. for(k=;k<;k++)
  97. {
  98. int xx=i+dx[k],yy=j+dy[k];
  99. if(Map[xx][yy]=='P')
  100. {
  101. Pic::add(Id[i][j],Id[xx][yy]);
  102. }
  103. }
  104. }
  105. }
  106. Wl(Picture::ErfenPipei());
  107. return ;
  108. }

codeforces116B的更多相关文章

随机推荐

  1. BMP280 driver对接单片机I2C或者SPI总线接口

    1:登录github网站搜BMP280,找到 BoschSensortec/BMP280_driver 2:gitclone或者download zip都可以,把驱动下载到本地,记得fork哦! 3: ...

  2. Omi框架学习之旅 - 插件机制之omi-finger 及原理说明

    以前那篇我写的alloyfinger源码解读那篇帖子,就说过这是一个很好用的手势库,hammer能做的,他都能做到, 而且源码只有350来行代码,很容易看懂. 那么怎么把这么好的库作为omi库的一个插 ...

  3. ASP.NET Core 释放 IDisposable 对象的四种方法

    本文翻译自<Four ways to dispose IDisposables in ASP.NET Core>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! IDispos ...

  4. eclipse 如何引入本地dtd

    一.首先修改xml的打开方式为:XML editor 1.菜单:Window -> Preferences ->General -> Editors -> File  Asso ...

  5. odoo学习之带出信息

    # 输入客户带出它默认的发运方式和包装方式 def on_change_partner_id_return(self,cr,uid,ids,partner_id,context=None): resu ...

  6. JDK+JAVA+maven+IDEA

    JDK+JAVA https://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html maven+IDEA http://blog.csdn ...

  7. qa_model

    [code=python] import os import sys import time import numpy import shelve import theano import thean ...

  8. THE First Individual Project - Word frequency program

    第一次写博客,这次也是本学期写到第一个程序. 老师要求网址:http://www.cnblogs.com/jiel/p/3311400.html#2777556 一.项目预计时间 一开始想使用不熟悉的 ...

  9. linux内核分析第六次实验

    使用gdb跟踪创建新进程的过程 rm menu -rf git clone https://github.com/mengning/menu.git  mv test_fork.c test.c  执 ...

  10. Jquery画折线图、柱状图、饼图

    1.今天做了一个折线图,首先需要导js文件.这里有一个demo:http://files.cnblogs.com/files/feifeishi/jquery_zhexiantubingtuzhuzh ...