Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38801    Accepted Submission(s): 22518

Problem Description
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.
 
Input
The 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.
 
Output
For 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
Source
 
Recommend
Eddy
 

Statistic | Submit | Discuss | Note

思路:'@'代表有石油,然后只要上下相邻或者左右相邻,或者对角相邻都算是一块石油,问有几块石油储备。

就是用dfs或者bfs就行了,得用栈或者队列来模拟。我写的是DFS版

不知道为什么在用scanf读入字符数组时,题目的第四组测试数据总是读入回车。。。getchar也不管用。

换cin好了。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <conio.h> using namespace std; typedef struct info{
int i,j;
}location; int m,n;
int countt;
char t;
char oilmap[][];
stack<location> s; int calx[]={-,-,-,,,,,};
int caly[]={-,,,-,,-,,}; void exploit(int i,int j){
location temp={i,j};
location now,next;
s.push(temp);
while(!s.empty()){
now=s.top();
s.pop();
oilmap[now.i][now.j]='*';
for(int k=;k<;k++){
next.i=now.i+calx[k];
next.j=now.j+caly[k];
if(next.i>= && next.i<m && next.j>= && next.j<n){
if(oilmap[next.i][next.j]=='@'){
s.push(next);
}
}
}
} } int main()
{
while(~scanf("%d %d",&m,&n)){
getchar();
if(m==) {break;}
for(int i=;i<m;i++){
for(int j=;j<n;j++){
cin>>oilmap[i][j];
}
}
countt=;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(oilmap[i][j]=='@'){
exploit(i,j);
countt++;
}
}
}
printf("%d\n",countt);
}
return ;
}

hdu 1241(DFS/BFS)的更多相关文章

  1. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  2. HDU 4771 (DFS+BFS)

    Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and hi ...

  3. HDU - 1241 dfs or bfs [kuangbin带你飞]专题一

    8个方向求联通块,经典问题. AC代码 #include<cstdio> #include<cstring> #include<algorithm> #includ ...

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

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

  5. hdu 1242 dfs/bfs

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  6. HDU 1241 DFS

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

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

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

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

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

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

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

随机推荐

  1. socket.io的websocket示例

    写了一个简单的demo,直接上代码吧.用的时候注意一下版本号,可能 socket.io 的 API 有修改~ 效果图 index.html <!DOCTYPE <!DOCTYPE html ...

  2. mysql 函数应用

    mod 取余是用函数mod(numer1,number2),其返回的值为其余数值 如:mod(id,2) = 1 返回id号是奇数的id Email后缀提取 emailorg=RIGHT(EMail, ...

  3. linux下的EDA——VCS使用

    原帖地址:https://blog.csdn.net/moon9999/article/details/75283926 在Linux下对verilogHDL进行功能仿真时非常必要的,下面提供两种常见 ...

  4. 好书推荐计划:Keras之父作品《Python 深度学习》

    大家好,我禅师的助理兼人工智能排版住手助手条子.可能非常多人都不知道我.由于我真的难得露面一次,天天给禅师做底层工作. wx_fmt=jpeg" alt="640? wx_fmt= ...

  5. 如何在 PhpStorm 使用 Code Generation?

    實務上開發專案時,有一些程式碼會不斷的出現,這時可靠 PhpStorm 的 Code Generation 幫我們產生這些 code snippet,除此之外,我們也可以將自己的 code snipp ...

  6. SSE图像算法优化系列七:基于SSE实现的极速的矩形核腐蚀和膨胀(最大值和最小值)算法。

    因未测试其他作者的算法时间和效率,本文不敢自称是最快的,但是速度也可以肯定说是相当快的,在一台I5机器上占用单核的资源处理 3000 * 2000的灰度数据用时约 20ms,并且算法和核心的大小是无关 ...

  7. jvm理论-class文件

    当JVM运行Java程序的时候,它会加载对应的class文件,并提取class文件中的信息存放在JVM的方法区内存中. Class文件组成 1.Class文件是一组以8位字节为基础单位的二进制流,各个 ...

  8. shell编程学习笔记(三):Shell中局部变量的使用

    现在我们看一下Shell中局部变量的使用 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script03.sh 开始编写script0 ...

  9. 程序运行在.Net 4.0低版本上 报“System.NullReferenceException”错误

    因为程序仅在个别机器上出现“ System.NullReferenceException”问题,而在其他机器上一切运行正常,所以认为是环境问题 具体错误信息如下: 2018-09-14 10:12:1 ...

  10. 浅谈 CSS 预处理器: 为什么要使用预处理器?

    CSS 自诞生以来,基本语法和核心机制一直没有本质上的变化,它的发展几乎全是表现力层面上的提升.最开始 CSS 在网页中的作用只是辅助性的装饰,轻便易学是最大的需求:然而如今网站的复杂度已经不可同日而 ...