Oil Deposits

HDU - 1241

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 

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)的更多相关文章

  1. (深搜)Oil Deposits -- hdu -- 1241

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  2. Oil Deposits HDU 1241

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  3. kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241

    题目链接:https://vjudge.net/problem/HDU-1241 题意:问有几个油田,一个油田由相邻的‘@’,组成. 思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索 ...

  4. HDU 1241 DFS

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. hdu 1241(DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  6. HDU 1241 Oil Deposits(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 很经典的一道dfs,但是注意每次查到一个@之后,都要把它变成“ * ”,然后继续dfs ...

  7. hdu 1241 Oil Deposits (一次dfs搞定有某有)

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  8. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  9. POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)

    题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...

随机推荐

  1. es6 + 笔记整理

    1. ES6提供了默认参数值机制,允许你为参数设置默认值,防止在函数被调用时没有传入这些参数: const required = () => {throw new Error('Missing ...

  2. leetcode--81-搜索旋转排序数组②

    题目描述: 33题 方法一: class Solution: def search(self, nums: List[int], target: int) -> bool: l, r = 0, ...

  3. order方法属于模型的连贯操作方法之一

    order方法属于模型的连贯操作方法之一,用于对操作的结果排序. 用法如下: $Model->where('status=1')->order('id desc')->limit(5 ...

  4. Error configuring application listener of class [org.springframework.web.util.Log4jConfigListener]

    1.启动项目发现如下错误: 严重: Error configuring application listener of class [org.springframework.web.util.Log4 ...

  5. Joomla - K2组件(文章管理扩展)

    一.下载 K2 进入 https://getk2.org/ ,点击DOWNLOAD K2 下载K2 下载完毕得到一个安装包 二.安装 K2 进入看后台,点击顶栏主菜单 扩展管理 -> 扩展安装 ...

  6. Cannot find module '@babel/plugin-proposal-class-properties'

    cnpm install --save-dev @babel/plugin-proposal-class-properties

  7. iOS开发CoreData的简单使用

    1.简介 CoreData是iOS5后,苹果提供的原生的用于对象化管理数据并且持久化的框架.iOS10苹果对CoreData进一步进行了封装,而且效率更高!相关类的简单介绍: NSManagedObj ...

  8. <每日一题>题目24:冒泡排序

    ''' 冒泡排序:比较相邻元素,顺序错误就交换顺序 ''' import random import cProfile def bubble_Sort(nums): for i in range(le ...

  9. docker上构建redis容器

    1.查看docker上的镜像 [root@holly ~]# docker images 2.搜索docker上的redis镜像,选择下载的版本 [root@holly ~]# docker sear ...

  10. VC:不支持尝试执行的操作

    问题描述: 基于CDialogEx的对话框工程.VS2010开发环境. 调试运行到OnInitDialog()的CDialogEx::OnInitDialog()方法的时候弹出提示窗口"不支 ...