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. 附近有什么?8款可以查周边的App

    如今科技发达的时代,手机的功能不仅仅只是能通讯聊天,而是逐渐的走进了人们的生活中.因为有了APP,我们的生活才更丰富,并且有很多是我们生活中不可缺少的软件,而这些软件便是根据手机中的GPS定位系统而来 ...

  2. Oralce sql (+) 补充

    Oracle  外连接 (1)左外连接 (左边的表不加限制)       (2)右外连接(右边的表不加限制)       (3)全外连接(左右两表都不加限制) 外连接(Outer Join) oute ...

  3. Unity 5.4版本 Application.systemLanguage 失效

    最近在上线双语版本(一个包支持中文.英文二种语言)时,遇到一个坑点 if (ToolUtils.isAndroid()) { if (Application.systemLanguage == Sys ...

  4. iOS:定制自适应大小的透明吐司弹框

    一.简单介绍 创建一个吐司消息的黑色透明弹框,可以根据消息长短自适应大小. 可以手动创建手动显示手动关闭,也可以手动创建自动显示自动关闭. 简单好用. 二.代码使用 .h文件 // // LiveHU ...

  5. tp5.1的安装与运行流程

    今天安装tp5.1的时候出现问题,我这里使用的是将tp直接解压缩后放在项目里面,结果提示下面的错误:Warning: require(D:\workspace\zhiyuedu\vendor/topt ...

  6. Spark(四十五):Schema Registry

    很多时候在流数据处理时,我们会将avro格式的数据写入到kafka的topic,但是avro写入到kafka的时候,数据有可能会与版本升级,也就是schema发生变化,此时如果消费端,不知道哪些数据的 ...

  7. MyBatis中使用实体中使用枚举,数据库中使用数值

    一.简介 本文主要讲MyBatis中使用实体中使用枚举,数据库中使用数值的解决方案.正常直接使用会报错,需要添加typeHandlers在mybatis-config.xml中. 二.解决方案 如下: ...

  8. zsh+on-my-zsh配置教程指南(程序员必备)

    本文以CentOS 7/Mac 为例,介绍zsh的配置使用教程. 准备 查看当前环境shell echo $SHELL <!-- more --> 查看系统自带哪些shell cat /e ...

  9. Linux下源码安装xz的方法

    xz是一个不太常见但又效率非常高的解压缩软件,安装方法如下 ,cd /usr/local/src ,wget https://tukaani.org/xz/xz-5.2.3.tar.gz //下载到/ ...

  10. HAProxy配置说明(转)

    原文地址:http://www.cnblogs.com/sagech/p/5695466.html global # 全局参数的设置 log 127.0.0.1 local0 info # log语法 ...