DFS(连通块) ZOJ 2743 Bubble Shooter
题意:从炮台射出一个球,三个及以上颜色相同的会掉落,问最后会掉落多少个球
分析:先从炮台找一个连通块,然后与顶部连接的连通块都不会掉落,剩下的就是炮台射出后跟随掉落的。
#include <bits/stdc++.h> const int N = 100 + 5;
char str[N][N];
int H, W, h, w;
int ans; bool check(int x, int y) {
if (x < 1 || x > H || y < 1 || y > W || (x%2==0 && y==W) || str[x][y] == '@') return false;
return true;
} void DFS(int x, int y, char color, int &cnt) {
if (!check (x, y)) {
return ;
}
if (color != '@' && color != str[x][y]) {
return ;
}
cnt++;
str[x][y] = '@';
DFS (x - 1, y, color, cnt);
DFS (x + 1, y, color, cnt);
DFS (x, y - 1, color, cnt);
DFS (x, y + 1, color, cnt);
if (x & 1) {
DFS (x - 1, y - 1, color, cnt);
DFS (x + 1, y - 1, color, cnt);
} else {
DFS (x - 1, y + 1, color, cnt);
DFS (x + 1, y + 1, color, cnt);
}
} int main() {
while (scanf ("%d%d%d%d", &H, &W, &h, &w) == 4) {
for (int i=1; i<=H; ++i) {
scanf ("%s", str[i] + 1);
for (int j=1; j<=W; ++j) {
if (str[i][j] == 'E') {
str[i][j] = '@';
}
}
}
int cnt = 0;
DFS (h, w, str[h][w], cnt);
int ans = cnt;
if (ans < 3) {
puts ("0");
continue;
}
for (int i=1; i<=W; ++i) {
DFS (1, i, '@', cnt);
}
for (int i=1; i<=H; ++i) {
for (int j=1; j<=W; ++j) {
if (i % 2 == 0 && j == W) continue;
if (str[i][j] != '@') {
ans++;
}
}
}
printf ("%d\n", ans);
} return 0;
}
DFS(连通块) ZOJ 2743 Bubble Shooter的更多相关文章
- ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)
D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- P1141 01迷宫 dfs连通块
题目描述 有一个仅由数字000与111组成的n×nn \times nn×n格迷宫.若你位于一格0上,那么你可以移动到相邻444格中的某一格111上,同样若你位于一格1上,那么你可以移动到相邻444格 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- HDU1241 Oil Deposits(dfs+连通块问题)
背景描述 ztw同志负责探测地下石油储藏.ztw现在在一块矩形区域探测石油.他通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石油的小方格相邻(横向相邻,纵向相邻,还有对角相邻),那么它们被认 ...
- Artwork (Gym - 102346A)【DFS、连通块】
Artwork (Gym - 102346A) 题目链接 算法 DFS,连通块 时间复杂度:O(k*n + k * k) 1.这道题就是让你判断从(0,0)到(m,n),避开中途所有的传感器(传感器的 ...
- ZOJ 1709 Oil Deposits(dfs,连通块个数)
Oil Deposits Time Limit: 2 Seconds Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...
- DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)
题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每 ...
随机推荐
- [Android Pro] 小心ReleaseByteArrayElements 中的参数问题
referen to : http://blog.csdn.net/rainlight/article/details/818964 在Sun的官方文档中,关于该函数的用法如下 The array i ...
- [Android] 解析android framework下利用app_process来调用java写的命令及示例
reference to :http://bbs.9ria.com/thread-253058-1-1.html 在android SDK的framework/base/cmds目录下了,有不少目录, ...
- SQL数据库约束
针对维护数据库的完整性,关系型数据库SQL提供了数据约束来管理数据,常用的约束有:外键.唯一.主键. 主键约束:标识数据的唯一,便于数据查询索引: 唯一约束:保证数据的唯一性:常用语法 alter t ...
- 微软改名部再次大显神威——ASP.NET 5改名ASP.NET Core 1.0
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:在计算机科学领域只有两件难事:缓存不可用和命名.--Phil Karlton 今天,S ...
- ASP.NET 5 Beta7发布
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 前几天,ASP.NET 5如期发布了Beta 7,根据路线图 (https://github ...
- 调整vbox虚拟机下的linux全屏模式及分辨率
>>Step1 在VirtualBox菜单栏中选择[设备]->[安装增强功能] >>Step2 点击右上角的[齿轮]图标,然后选择[Log Out],重新登录即可 lin ...
- JSON资料汇总
网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...
- [Java] arraycopy 数组复制(转)
public class ArraycopyTest { public static void main(String[] args) { //静态初始化两个长度不同的 ...
- BZOJ 1861: [Zjoi2006]Book 书架 splay
1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...
- java线程之——synchronized的注意细节
我在学习synchronized的时候,十分好奇当一个线程进入了一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 然后就做了个实验(实验代码最后贴出),最后得到了如下 ...