CodeForces 690D1 The Wall (easy) (判断连通块的数量)
题意:给定一个图,问你有几个连通块。
析:不用说了,最简单的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) (判断连通块的数量)的更多相关文章
- P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)
P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...
- Doves and bombs UVA - 10765(统计割顶所连接的连通块的数量)
题意:给定一个n个点的连通的无向图,一个点的“鸽子值”定义为将它从图中删去后连通块的个数. 求对应的点 和 每个点的“鸽子值” 用一个数组在判断割顶的那个地方 累加标记一下所连接的连通块的数量即可 初 ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】
一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
题目链接: C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standa ...
- FZU 1063 三维扫描(三维连通块)
Accept: 415 Submit: 1291 Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 工业 ...
- Artwork (Gym - 102346A)【DFS、连通块】
Artwork (Gym - 102346A) 题目链接 算法 DFS,连通块 时间复杂度:O(k*n + k * k) 1.这道题就是让你判断从(0,0)到(m,n),避开中途所有的传感器(传感器的 ...
- UVa 572 油田(DFS求连通块)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- JZOJ 3453.【NOIP2013中秋节模拟】连通块(connect)
3453.[NOIP2013中秋节模拟]连通块(connect) Time Limits: 1000 ms Memory Limits: 262144 KB (File IO): input:conn ...
- 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 ...
随机推荐
- SQL判断NULL的几种常见方式
第一种 where XX ='NULL' ,XX字段存的值就是NULL这四个字符, 第二种 where XX is null ,XX字段什么也没存,这是数据库的判断语法, 第三种 where isnu ...
- sersync之不洗澡
inotiry图片参考 sersync图片参考 inotify文字教程 该软件对系统有要求,内核2.6以上,并且有如下目录,后面会讲解三个文件用途 [root@jokerpro ~]# uname - ...
- 浅谈PHP面向对象编程(六、自动加载及魔术方法)
6.0 自动加载及魔术方法 6.1 自动加载 在PHP开发过程中,如果希望从外部引入一个class.通常会使用incluae和requre方法把定义这个class的文件包含进来.但是,在大型的开发项 ...
- window.location和window.open的区别
window.location = "http://www.baidu.com" 跳转后有后退功能 window.location.replace("http://www ...
- ror笔记2
在rails app的 config 文件夹中新建unicorn.rb内容如下 worker_processes 2 working_directory "/home/mage/boleht ...
- Mysql EXPLAIN列的解释
转自:http://blog.chinaunix.net/uid-540802-id-3419311.html explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择 ...
- 【297】IDL 过程、函数&关键字参数
目录: 一.Procedure 1.1 基本说明&定义 1.2 关键字参数 二.Function 2.1 基本说明&定义 2.2 关键字参数 参考:IDL中函数中的带有关键字的参数的使 ...
- git命令用法
git svn 说明 git pull svn update git add 要提交的文件名 svn add git rm svn rm,del git commit -m '备注一下提 ...
- c3p0、dbcp和proxool比较
现在常用的开源数据连接池主要有c3p0.dbcp和proxool三种,其中: hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样 ...
- 设置Windows开机自动启动VirtualBox虚拟机系统
如果常用VirtualBox虚拟机系统的话,设置随开机启动也是很方便的.不需要打开VirtualBox窗口,直接启动VirtualBox虚拟机系统就可以了. 设置开机自启动VirtualBox虚拟机系 ...