题目链接:

D1. The Wall (easy)

time limit per test

0.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

"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

题意:

求有多少个连通块;

思路:

dfs,水题;

AC代码:

#include <bits/stdc++.h>
/*
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+;
const int maxn=;
const double eps=1e-; int n,m,vis[][],dir[][]={,,-,,,-,,};
char mp[][]; void dfs(int x,int y)
{
vis[x][y]=;
for(int i=;i<;i++)
{
int fx=x+dir[i][],fy=y+dir[i][];
if(vis[fx][fy]||mp[fx][fy]=='.')continue;
if(fx>&&fx<=n&&fy>&&fy<=m)dfs(fx,fy);
}
} int main()
{
read(n);read(m);
For(i,,n)scanf("%s",mp[i]+);
int ans=;
For(i,,n)
{
For(j,,m)
{
if(!vis[i][j]&&mp[i][j]=='B')
{
dfs(i,j);
ans++;
}
}
}
cout<<ans<<"\n";
return ;
}

 

codeforces 690D1 D1. The Wall (easy)(dfs)的更多相关文章

  1. CodeForces 690D1 The Wall (easy) (判断连通块的数量)

    题意:给定一个图,问你有几个连通块. 析:不用说了,最简单的DFS. 代码如下: #include <bits/stdc++.h> using namespace std; const i ...

  2. Codeforces Global Round 7 D1. Prefix-Suffix Palindrome (Easy version)(字符串)

    题意: 取一字符串不相交的前缀和后缀(可为空)构成最长回文串. 思路: 先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀. #include <bits/stdc++.h> ...

  3. Codeforces Round #602 Div2 D1. Optimal Subsequences (Easy Version)

    题意:给你一个数组a,询问m次,每次返回长度为k的和最大的子序列(要求字典序最小)的pos位置上的数字. 题解:和最大的子序列很简单,排个序就行,但是题目要求字典序最小,那我们在刚开始的时候先记录每个 ...

  4. HDU 1484 Basic wall maze (dfs + 记忆)

    Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)

    题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...

  6. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  7. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  8. codeforces 580C Kefa and Park(DFS)

    题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...

  9. Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)

    题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...

随机推荐

  1. 再看c语言之getchar/putchar

  2. 动态规划—最长回文子串LEETCODE第5题深度剖析

    动态规划对于笔者来说有很重要的意义 一.题目如下: 对于此类题目,笔者常用的的办法是先做个暴力解题思路,然后再对暴力法进行优化. 二.暴力法 //字串遍历 public static String l ...

  3. Linux下xz与tar的区别

    同一文件,tar.xz格式比tar.gz格式小了三分之一! 说明: xz是一个使用LZMA压缩算法的无损数据压缩文件格式. 和gzip与bzip2一样,同样支持多文件压缩,但是约定不能将多于一个的目标 ...

  4. weex 阶段总结

    新年伊始,回顾过去的一年,收获很多,之前一直在研究weex,说心里话感觉心好累,官方文档不全,社区不活跃,遇到很多坑,官方发布的版本有时都有坑,搞得我都不敢更新版本了. 但是,研究了这么久,放弃太可惜 ...

  5. 多线程网页爬虫 python 实现(二)

    #!/usr/bin/env python #coding=utf-8 import threading import urllib import re import time cur=0 last= ...

  6. 【转载】Http协议与TCP协议简单理解

    在C#编写代码,很多时候会遇到Http协议或者TCP协议,这里做一个简单的理解.TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上 ...

  7. error at ::0 can&#39;t find referenced pointcut pointCutName 错误解决方法

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: publi ...

  8. 关于HTTP1.1的长连接

    HTTP是一个构建在传输层的TCP协议之上的应用层的协议,在这个层的协议,是一种网络交互须要遵守的一种协议规范. HTTP1.0的短连接 HTTP 1.0规定浏览器与server仅仅保持短暂的连接.浏 ...

  9. C# Excel批注“哪种开发语言最好”

    Excel批注经常使用于为个别的单元格加入凝视.读者可以从凝视中获取额外的信息. 批注可隐藏,仅仅会在单元格右上方显示红色三角.加入后不会对单元格的内容喧宾夺主.在日常编程处理Excel中,为个别单元 ...

  10. UR#34. 多项式乘法

    #34. 多项式乘法 统计 描述 提交 自定义测试 这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+ ...