Description

"The zombies are lurking outside. Waiting. Moaning. And when they come..."

"When they come?"

"I hope the Wall is high enough."

Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are.

The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide.

Input

The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows:

  • each of the R lines contains a string of length C,
  • the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise.

The input will contain at least one character B and it will be valid.

Output

The number of wall segments in the input configuration.

Examples
input
3 7
.......
.......
.BB.B..
output
2
input
4 5
..B..
..B..
B.B.B
BBB.B
output
2
input
4 6
..B...
B.B.BB
BBB.BB
BBBBBB
output
1
input
1 1
B
output
1
input
10 7
.......
.......
.......
.......
.......
.......
.......
.......
...B...
B.BB.B.
output
3
input
8 8
........
........
........
........
.B......
.B.....B
.B.....B
.BB...BB
output
2
Note

In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second.

判断连通块

#include <bits/stdc++.h>
using namespace std; int n,m;
char ma[200][200];
int vis[200][200];
void dfs(int x,int y)
{
if(x<0||x>=n||y<0||y>=m)return;
if(ma[x][y]=='.')return;
if(vis[x][y])return;
vis[x][y]=1;
if(ma[x][y]=='B')ma[x][y]='.';
dfs(x+1,y);
dfs(x-1,y);
dfs(x,y+1);
dfs(x,y-1); }
int main()
{
while(~scanf("%d%d",&n,&m)){
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
scanf("%s",ma[i]);
int ans=0;
for(int i=0;i<n;i++)
for(int k=0;k<m;k++)
if(ma[i][k]=='B')
ans++,dfs(i,k);
printf("%d\n",ans);}
return 0;
}

  

最新文章

  1. CSS之viewport 2
  2. 9月10日,美团网2014校招研发笔试哈尔滨站 1、链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现
  3. iOS - Regex 正则表达式
  4. bn
  5. Checked ==true ? &quot;Y&quot;:&quot;N&quot; ;
  6. 5个最顶级jQuery图表类库插件-Charting plugin
  7. Python应用02 Python服务器进化
  8. css笔记07:通配符选择器
  9. mysql安装启动教程(两种方法)
  10. Asterisk 安装与配置
  11. Swift使用单个案件管理FMDB数据库
  12. Spring之SpringMVC的MethodNameResolver(源码)分析
  13. 为异常处理做准备,熟悉一下WinDbg工具
  14. win7彻底卸载iis
  15. ES6浅谈之Promise
  16. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
  17. Lua保留指定小数位数
  18. PDF裁剪页面,PDF怎么裁剪页面的方法
  19. MySQL-如何删除hash表分区
  20. 重载的方式写Python的get请求

热门文章

  1. jquery--find与children方法的区别
  2. CF 293E Close Vertices——点分治
  3. redhat 安装lamp
  4. 2006年清华:N的阶乘
  5. 人物-IT-任正非:任正非
  6. CCNet说明文档
  7. kvm基础 虚拟机内存、CPU调整
  8. HTTP之缓存首部
  9. 差一点搞混了Transactional注解
  10. TCP/IP的3次握手和4次握手