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. 常见问题2:html+css效果综合整理

    1.---文本框提示文字颜色--placeholder属性 <input type="text" placeholder="请输入提示文字" /> ...

  2. 【jQuery Demo】图片切换效果整理

    图片的切换效果有很多,比较常见的有水平滚动.垂直滚动.淡入淡出等.我们接下来一一实现这些效果. 1.水平滚动 1) 我们先来实现HTML页面,代码很简单: <div id="conta ...

  3. ionic使用iframe时无法显示网页或报错

    ionic使用iframe时无法显示网页或报错 Uncaught DOMException: Blocked a frame with origin 在config.xml中添加 <access ...

  4. springcloud如何实现服务的平滑发布

    在之前的文章中我们提到服务的优雅下线,见: SpringCloud服务如何在Eureka安全优雅的下线 但这个对于ribbon调用其实是不平滑的,shutdown请求到后服务就马上关闭了,服务消费此时 ...

  5. Mac下的Chrome或Safari访问跨域设置,MBP上使用模拟器Simulator.app或iphone+Safari调试网页

    Mac下的Chrome或Safari访问跨域设置: mac下终端启动Chrome $ open -a Google\ Chrome --args --disable-web-security 或 /A ...

  6. java 定时任务之一 @Scheduled注解(第一种方法)

    使用spring @Scheduled注解执行定时任务: 运行!!! 关于Cron表达式(转载) 表达式网站生成: http://cron.qqe2.com/  直接点击 作者:http://blog ...

  7. [windows+cocos2dx]文本类

    文字也是游戏最重要的元素之中的一个,以下就看看使用动态字体和静态字体库怎样创建文字.使用动态字体库创建的文字能够包括经常使用的不论什么字符,一般假设用户要输入名字,这就最好用动态字体库.由于你不知道用 ...

  8. JVM类加载机制与反射-转

    一.Java类加载机制 1.概述 Class文件由类装载器装载后,在JVM中将形成一份描述Class结构的元信息对象,通过该元信息对象可以获知Class的结构信息:如构造函数,属性和方法等,Java允 ...

  9. 基于Python37配置图片文字识别

    以管理员权限打开cmd控制台. 1.如何安装PIL 输入下面命令:pip install Pillow 参考:https://www.cnblogs.com/mrgavin/p/8177841.htm ...

  10. C#通过用户名与密码访问共享目录

    C#通过用户名与密码访问共享目录 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...