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. 在双重for循环内部使用async异步请求axios中遇到的问题

    在methods中的方法 async getPro () { let _this = this let newArr = [] await axios.get(`api/v1/dailyProTbms ...

  2. Python-包与常用模块(2)

    目录 包 导入包内包 相对导入和绝对导入 注意事项 time模块 datetime模块 random模块 argparse 模块 configparser模块 hashlib模块和hmac模块 typ ...

  3. 微信公众号 SVG长按互动

    <section class="" style="display: block;width: 100%;height:667px;overflow:hidden;m ...

  4. c语言学习笔记 - 指针和数组

    结合内存存储数据的机制,c语言里指针的出现和使用也就不奇怪了,如果先学了内存的一些知识,以及程序运行机制,到了c指针这块就会清晰很多. #include <stdio.h> int mai ...

  5. Java笔记 - 输入输出流

    java.io包中定义了各式各样的"流(stream)" 类型(类或抽象类),通过标准的方法实现对于数据的输入/输出操作. 一.流类型分类 以从不同的角度对其进行分类:按数据流的方 ...

  6. 左神算法进阶班3_1构造数组的MaxTree

    题目 一个数组的MaxTree定义: 数组必须没有重复元素 MaxTree是一棵二叉树,数组的每一个值对应一个二叉树节点 包括MaxTree树在内且在其中的每一棵子树上,值最大的节点都是树的头 给定一 ...

  7. Linux 启动、关闭、重启服务的两种方式

    1.一种是可以使用service脚本来调度,如: service 服务名 startservice 服务名 stopservice 服务名 restart 2.第二种可以直接进入/etc/init.d ...

  8. axios接口封装

    axios封装 import JsonP from 'jsonp' import axios from 'axios' import { Modal } from 'antd' export defa ...

  9. PostgreSQL的架构

    是最先进的数据库.他的第一个版本在1989年发布,从那时开始,他得到了很多扩展.根据db-enginers上的排名情况,PostgreSQL目前在数据库领域排名第四. 本篇博客,我们来讨论一下Post ...

  10. 如何通过EditPlus远程连接Linux

    1. File - FTP - FTP Settings 2. Add 3. 填写Linux的ip地址及用户名和密码 4. OK