题意:给定一个图,问你有几个连通块。

析:不用说了,最简单的DFS。

代码如下:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 100 + 5;
const int dr[] = {1, -1, 0, 0};
const int dc[] = {0, 0, 1, -1};
char a[maxn][maxn];
int vis[maxn][maxn]; void dfs(int r, int c){
vis[r][c] = 1;
for(int i = 0; i < 4; ++i){
int x = r + dr[i];
int y = c + dc[i];
if(!vis[x][y] && a[x][y] == 'B') dfs(x, y);
}
} int main(){
int r, c;
cin >> r >> c;
getchar();
memset(vis, 0, sizeof(vis));
for(int i = 0; i < r; ++i) cin >> a[i];
int ans = 0;
for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j)
if(a[i][j] == 'B' && !vis[i][j]){
++ans;
dfs(i, j);
}
cout << ans << endl;
return 0;
}

CodeForces 690D1 The Wall (easy) (判断连通块的数量)的更多相关文章

  1. P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)

    P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...

  2. Doves and bombs UVA - 10765(统计割顶所连接的连通块的数量)

    题意:给定一个n个点的连通的无向图,一个点的“鸽子值”定义为将它从图中删去后连通块的个数. 求对应的点 和 每个点的“鸽子值” 用一个数组在判断割顶的那个地方 累加标记一下所连接的连通块的数量即可 初 ...

  3. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  4. codeforces 590C C. Three States(bfs+连通块之间的最短距离)

    题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...

  5. FZU 1063 三维扫描(三维连通块)

    Accept: 415    Submit: 1291 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 工业 ...

  6. Artwork (Gym - 102346A)【DFS、连通块】

    Artwork (Gym - 102346A) 题目链接 算法 DFS,连通块 时间复杂度:O(k*n + k * k) 1.这道题就是让你判断从(0,0)到(m,n),避开中途所有的传感器(传感器的 ...

  7. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. JZOJ 3453.【NOIP2013中秋节模拟】连通块(connect)

    3453.[NOIP2013中秋节模拟]连通块(connect) Time Limits: 1000 ms Memory Limits: 262144 KB (File IO): input:conn ...

  9. codeforces 690D1 D1. The Wall (easy)(dfs)

    题目链接: D1. The Wall (easy) time limit per test 0.5 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. Linux学习笔记 -- stdin/stdout 重定向

    输入/输出重定向 Linux系统通常从一个叫标准输入的地方读取输入并且将一个命令的结果以写入到标准输出反馈给我们:默认情况下,这也是我们使用的终端(命令行).如果我们想改变输入和输出的方式,就需要使用 ...

  2. ubuntu 12.04 配置-1

    今天对ubuntu 12.04 系统进行了相关的配置,配置的主要内容有: 1)php + mysql + apache2 web开发环境的搭建: 2)vim的简单保存退出指令: 3)文件和文件夹权限的 ...

  3. linux中sed命令

    sed sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. ...

  4. halcon的长度和角度测量

    halcon代码: 1: *读取并截取图片 2: dev_close_window() 3: read_image (Image, 'D:/MyFile/halcon/长度和角度测量/图.png') ...

  5. yarn学习

  6. spring-boot-starter-security Spring Boot中集成Spring Security

    spring security是springboot支持的权限控制系统. security.basic.authorize-mode 要使用权限控制模式. security.basic.enabled ...

  7. 获取properties配置

    1.      使用@Value @Value("${swagger.enable}") 使用Spring的PropertyPlaceholderConfigurer关联 @Val ...

  8. fatal error C1010: 在查找预编译头时遇到意外的文件结尾 (转)

    错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾.是否忘记了向源中添加“#include "stdafx.h"”? 错误分析:     此错误发生 ...

  9. MVC地址输出变为小写

    其实个人不知道有什么区别,看到大多数都是小写,还是小写吧 去nuget安装一个第三方,然后对路由简单修改一下 插件名称:LowercaseRoutesMVC 方法名:MapRouteLowercase ...

  10. Android 定时重复启动弹出窗口。

    本来想着用handlerpostdelay就可以实现,没想到演示后关闭应用居然报错. 后来想到是没有了activity. ((Activity)context).isFinishing() 可以传入c ...