题目链接:

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. MYSQL 中GROUP BY

    group by 用法解析 group by语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. SELECT子句中的列名必须为分组列或列函数.列函数对于GROUP BY子 ...

  2. yii 之数据库关联查询

    <?php namespace app\controllers; use yii\web\Controller; use app\models\Customer; class CustomerC ...

  3. SELinux 服务检查与关闭

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  4. T1553 互斥的数 codevs

    http://codevs.cn/problem/1553/ 题目描述 Description 有这样的一个集合,集合中的元素个数由给定的N决定,集合的元素为N个不同的正整数,一旦集合中的两个数x,y ...

  5. Go语言并发之美

    简介           多核处理器越来越普及,那有没有一种简单的办法,能够让我们写的软件释放多核的威力?答案是:Yes.随着Golang, Erlang, Scale等为并发设计的程序语言的兴起,新 ...

  6. Android中View自己定义XML属性具体解释以及R.attr与R.styleable的差别

    为View加入自己定义XML属性 Android中的各种Widget都提供了非常多XML属性,我们能够利用这些XML属性在layout文件里为Widget的属性赋值. 例如以下所看到的: <Te ...

  7. MRP Force Reservation的作用

    生产单根据BOM计算出相应的物料需求,生产领料单stock.picking ( internal moves) Stock.picking使用工作流自动计算库存量,如果库存量够,则使用 test_as ...

  8. CS 和 BS 的区别和优缺点(转)

    转自:http://www.cnblogs.com/scnuyz/p/5808808.html bs是浏览器(browser)和服务器(server) cs是静态客户端程序(client)和服务器(s ...

  9. Collection接口和Collections类的简单区别和讲解

    这里仅仅进行一些简单的比较,如果你想要更加详细的信息话,请自己百度. 1.Collection: 是集合类的上层接口.本身是一个Interface,里面包含了一些集合的基本操作. Collection ...

  10. Mac 下配置 Cocos2d-x 3-x android 的环境

    本人初学Cocos2d 3-x,环境配置,搭建android环境弄了好长时间,走了不少弯路,翻阅了好多人的博客和文档,包括官方文档讲的似乎有些似懂非懂,好多依然是旧的版本,所以把我的整个过程梳理一下. ...