Oil Deposits HDU - 1241 (dfs)
Oil Deposits
InputThe input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
OutputFor each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2 注意:相邻是指8个方向
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<queue> using namespace std; int dx[] = {,-,,,,,-,-};
int dy[] = {,,,-,-,,,-};
char mp[][];
int vis[][];
int m, n; void dfs(int x, int y)
{
for(int i = ; i < ; ++i)
{
int xx = x + dx[i];
int yy = y + dy[i]; if(xx >= && xx < m && yy >= && yy < n && !vis[xx][yy] && mp[xx][yy] == '@')
{
vis[xx][yy] = ;
dfs(xx, yy);
}
}
} int main()
{
std::ios::sync_with_stdio(false);
while(cin >> m >> n)
{
if(m == )
break;
for(int i = ; i < m; ++i)
for(int j = ; j < n; ++j)
cin >> mp[i][j];
memset(vis, , sizeof(vis));
int ans = ;
for(int i = ; i < m; ++i)
{
for(int j = ; j < n; ++j)
{
if(mp[i][j] == '@' && !vis[i][j])
{
vis[i][j] = ;
dfs(i, j);
ans++;
} }
}
cout << ans << endl;
} return ;
}
Oil Deposits HDU - 1241 (dfs)的更多相关文章
- (深搜)Oil Deposits -- hdu -- 1241
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- Oil Deposits HDU 1241
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241
题目链接:https://vjudge.net/problem/HDU-1241 题意:问有几个油田,一个油田由相邻的‘@’,组成. 思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索 ...
- HDU 1241 DFS
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- hdu 1241(DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1241 Oil Deposits(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...
- hdu 1241 Oil Deposits (一次dfs搞定有某有)
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- HDU 1241 (DFS搜索+染色)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...
- POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)
题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...
随机推荐
- 解决mysql中无法修改事务隔离级别的问题
使用SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;修改数据库隔离级别, 然后执行SELECT @@TX_ISOLATION;后发现数据库的隔离级别并 ...
- 二.ES6新的声明方式
前言: 以前我们在声明时只有一种方法,就是使用var来进行声明,ES6对声明的进行了扩展,现在可以有三种声明方式了. 字面理解ES6的三种声明方式: var:它是variable的简写,可以理解成变量 ...
- 廖雪峰Java15JDBC编程-1关系数据库基础-1关系数据库简介
1.数据库 1.1 定义 数据库是按照数据结构来组合.存储和管理数据的软件. 1.2 数据库模型 数据库有层次模型.网状模型.关系模型三种模型. 2 关系数据库 关系数据库是建立在关系模型上的数据库, ...
- css - 常见知识点
1. 盒模型 页面渲染时,dom 元素所采用的 布局模型.可通过box-sizing进行设置.根据计算宽高的区域可分为: content-box (W3C 标准盒模型) border-box (IE ...
- Codeforces Round #599 (Div. 2)的简单题题解
难题不会啊…… 我感觉写这个的原因就是因为……无聊要给大家翻译题面 A. Maximum Square 简单题意: 有$n$条长为$a_i$,宽为1的木板,现在你可以随便抽几个拼在一起,然后你要从这一 ...
- Windows的SEH机理简要介绍
1.异常分类 一般来说,我们把Exception分为2类,一类是CPU产生的异常,我们称之为CPU异常(或者硬件异常).另一类为是通过调用RaiseException API产生的软件异常,我们称之为 ...
- opencv-VS2010配置opencv2.4.8
详细教程可参考:http://blog.csdn.net/huang9012/article/details/21811129/ 原文在这里:[OpenCV入门教程之一] 安装OpenCV:OpenC ...
- x64:x64
ylbtech-x64:x64 “x86-64”,有时会简称为“x64”,是64位微处理器架构及其相应指令集的一种,也是Intel x86架构的延伸产品.“x86-64”1999由AMD设计,AMD首 ...
- 【solr】Solr与JDK对应版本关系,Tomcat与JDK
Solr与JDK对应版本关系,Tomcat与JDK版本对应关系 最新在部署solrCloud集群,由于自己机器上用的JDK都是JDK1.7的,然后我就从网上下载了最新下载了最先的solr6.6.0和最 ...
- gstore安装
gstore要求安装在linux系统中,如果你的电脑是windows系统,最好安装一个linux虚拟机或者安装一个Docker 在我的Docker安装中已经讲了Docker的安装过程,下面接着讲如何安 ...