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列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...
随机推荐
- 从Java看数据结构之——树和他的操作集
写在前面 树这种数据结构在计算机世界中有广泛的应用,比如操作系统中用到了红黑树,数据库用到了B+树,编译器中的语法树,内存管理用到了堆(本质上也是树),信息论中的哈夫曼编码等等等等.而树的实现和他的操 ...
- Spring的IoC容器-Spring BeanFactory容器
虽然这个方法已经在3.1之后已经弃用了,但是可以了解一下. 在Spring中,有大量对BeanFactory接口的实现.其中,最常被使用的是XmlBeanFactory类.这个容器从一个XML文件中读 ...
- java判断字符串中是否含有汉字
原文:http://www.open-open.com/code/view/1426332240717 判断字符串中是否含有汉字: String str = "test中文汉字"; ...
- android 程序退出的对话框
package com.example.yanlei.yl; import android.graphics.Color; import android.support.v7.app.AppCompa ...
- 1054. 求平均值 (20)-PAT乙级真题
今天刚刚到学校,2017年学习正式开始了,今天看到了浙大的<数据结构>这学期又要开课了,决定一定要跟着学习一遍:在大学生mooc网上学习:http://www.icourse163.org ...
- Camtasia Studio如何添加画中画
将录像文件和其他视频文件拖放到剪辑箱,右击录像文件(camrec文件)添加到时间轴,一般这个就是主要的视频文件,我们会在这个基础上添加字幕,配音,画中画等,拖进去之后可以发现多出来了一个视频1和音频1 ...
- Android中View绘制流程以及invalidate()等相关方法分析(转)
转自:http://blog.csdn.net/qinjuning 前言: 本文是我读<Android内核剖析>第13章----View工作原理总结而成的,在此膜拜下作者 .同时真挚地向渴 ...
- Node.js机制及原理理解初步
http://blog.csdn.net/leftfist/article/details/41891407 一.node.js优缺点 node.js是单线程. 好处就是 1)简单 2)高性能,避免了 ...
- Effective C++ 条款五 了解C++默默编写并调用哪些函数
//申明一个类时,编译器会默认为你提供四个函数. //无参构造函数,析构函数,copy构造函数,copy assignment操作符. template <typename T> ...
- VC中常见API函数使用方法(经验版)
***********************************************声明*************************************************** ...