codeforces 690D1 D1. The Wall (easy)(dfs)
题目链接:
0.5 seconds
256 megabytes
standard input
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.
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.
The number of wall segments in the input configuration.
3 7
.......
.......
.BB.B..
2
4 5
..B..
..B..
B.B.B
BBB.B
2
4 6
..B...
B.B.BB
BBB.BB
BBBBBB
1
1 1
B
1
10 7
.......
.......
.......
.......
.......
.......
.......
.......
...B...
B.BB.B.
3
8 8
........
........
........
........
.B......
.B.....B
.B.....B
.BB...BB
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)的更多相关文章
- CodeForces 690D1 The Wall (easy) (判断连通块的数量)
题意:给定一个图,问你有几个连通块. 析:不用说了,最简单的DFS. 代码如下: #include <bits/stdc++.h> using namespace std; const i ...
- Codeforces Global Round 7 D1. Prefix-Suffix Palindrome (Easy version)(字符串)
题意: 取一字符串不相交的前缀和后缀(可为空)构成最长回文串. 思路: 先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀. #include <bits/stdc++.h> ...
- Codeforces Round #602 Div2 D1. Optimal Subsequences (Easy Version)
题意:给你一个数组a,询问m次,每次返回长度为k的和最大的子序列(要求字典序最小)的pos位置上的数字. 题解:和最大的子序列很简单,排个序就行,但是题目要求字典序最小,那我们在刚开始的时候先记录每个 ...
- HDU 1484 Basic wall maze (dfs + 记忆)
Basic wall maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- codeforces 580C Kefa and Park(DFS)
题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...
- Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)
题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...
随机推荐
- curl抓取数据
抓取数据的代码: $url='抓取数据的网站路径'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //参数为1表示传输数据,为0表示 ...
- android studio AndroidManifest
一.目录结构 1. AndroidManifest.xml 它是一个清单文件,提供应用的基本信息 <?xml version="1.0" encoding="utf ...
- T2602 最短路径问题 codevs
http://codevs.cn/problem/2602/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 平面上有n个点(n& ...
- 初涉Git/Github
初涉Git/Github 第一部分:我的本次作业成果 我自己个人的github地址是:STRSong 我们开发团队小组的github地址是:三组 第二部分:给同学推荐github资源 推荐1 这个推荐 ...
- Redhat 5 无法安装elfutils-libelf-devel-0.137问题
http://whr25.blog.sohu.com/263584338.html 问题: RHEL5.5安装oracle11gR2的时候需要安装elfutils-libelf-devel-0.137 ...
- Linux之时钟中断
from:深入分析Linux内核源码(http://oss.org.cn/kernel-book/) 时钟中断的产生 Linux的OS时钟的物理产生原因是可编程定时/计数器产生的输出脉冲,这个脉冲送入 ...
- Error: cannot call methods on draggable prior to initialization; attempted to call
cannot call methods on draggable prior to initialization; attempted to call 报这个问题的根本原因是由于你的引用文件有问题 ...
- 远程唤醒UP Board
前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我的博客: http://www.cnblogs.co ...
- JNI——访问数组
JNI在处理基本类型数组和对象数组上面是不同的.对象数组里面是一些指向对象实例或者其它数组的引用. 因为速度的原因,先通过GetXXXArrayElements函数把简单类型的数组转化成本地类型的数组 ...
- Effective C++ 条款六 若不想使用编译器自动生成的函数,就该明确拒绝
class HomeForSale //防止别人拷贝方法一:将相应的成员函数声明为private并且不予实现 { public: private: HomeForSale(const HomeForS ...