题目:

 
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

题解:

  逐个遍历,遇到‘@’就深搜它的周围,逐层深搜,直到把相连的所有@都变成*。

代码:

#include <iostream>

using namespace std;
char a[][];
int n,m,sum=;
void dfs(int i,int j){
if(a[i][j]!='@'||i<||j<||i>=m||j>=n) return;
else{
a[i][j]='*';
dfs(i-, j-);
dfs(i-, j);
dfs(i-, j+);
dfs(i, j-);
dfs(i, j+);
dfs(i+, j-);
dfs(i+, j);
dfs(i+, j+);
}
} int main()
{
int i,j;
while(cin>>m>>n){
sum=;
if(n==||m==) break;
for(i=;i<m;i++)
for(j=;j<n;j++)
cin>>a[i][j];
for(i=;i<m;i++)
for(j=;j<n;j++){
if(a[i][j]=='@'){
dfs(i,j);
sum++;
}
}
cout<<sum<<endl;
}
return ;
}

hdu 1241 Oil Deposits (简单搜索)的更多相关文章

  1. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

  2. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  3. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  4. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  5. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  6. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  7. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  8. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  9. HDU 1241 Oil Deposits (DFS)

    题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...

随机推荐

  1. MySQL主从复制(一)

    whereis...命令的使用:

  2. 洛谷P2756 飞行员配对方案问题

    二分图裸题,找他的最大匹配即可 #include<bits/stdc++.h> using namespace std; int n,m,ans; ; int to[N]; struct ...

  3. LODOP打印用JS获取的当前日期

    该文详细一步步解释JS获取当前时间的方法,新手小白也看到懂,最后是实际的获取当前年月份的方法.JS中的Date()对象,包含很多当前系统时间的方法,首先建立一个Date()对象,这里取名为date,然 ...

  4. confluence6.x安装

    一 简介 confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实现团队成员之间的协作和知识共享. 网上有关confluence的教程比较多,在此我们以confl ...

  5. BlockQueue 解析

    生产者.消费者模式 https://www.jianshu.com/p/024a36b83099

  6. 我的小OJ

    NCOJ 欢迎大家来瓷瓷.出题哦QwQ 嗯,没了.

  7. Linux-进程管理

    关于进程 Process what is process ? 什么是进程 process life cycle 进程的生命周期 process states 进程状态 什么是进程? 进程是已启动的可执 ...

  8. HFSS在进行仿真时端口与激励设置细则

    最近发现在使用HFSS仿真天线时候在设置端口激励求解的时候,由于端口激励面积的大小和放置方式的不通最终的求解结果也有很多不同 在进行CPW结构的天线仿真中分别尝试了waveport 和lumpedpo ...

  9. LA4080/UVa1416 Warfare And Logistics 最短路树

    题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...

  10. Pushgateway 介绍

    Pushgateway是一个独立的服务,Pushgateway位于应用程序发送指标和Prometheus服务器之间. Pushgateway接收指标,然后将其作为目标被Prometheus服务器拉取. ...