Oil Deposits

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

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
讲解:@代表油田,寻找共有多少个油田,连在一起的算一个
解法一:不用队列,代码如下:
 #include<iostream>
#include<string>
#include<cstring>
using namespace std;
char s[][];
int dir[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
int n,m;
void dfs(int x,int y)
{ int i,xx,yy;
for(i=;i<;i++)
{
xx=x+dir[i][];yy=y+dir[i][];
if(xx<||xx>n || yy< || yy>m || s[xx][yy]=='*')
continue;
s[xx][yy]='*';
dfs(xx,yy);
}
}
int main()
{
int i,j;
while(cin>>n>>m && m+n)
{ int sum=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
cin>>s[i][j];
for(i=;i<=n;i++)
for(j=;j<=m;j++)
if(s[i][j]=='@')
{
sum++;
// s[i][j]='*';
dfs(i,j);
}
cout<<sum<<endl;
}
return ;
}

解法二:用队列来解

 #include<iostream>
#include<queue>
const int MAX=;
int fangxiang[][]={{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
using namespace std;
typedef struct dian
{
int x;
int y;
};
char map[MAX][MAX];
int n;
int m;
void BFS(int x,int y)
{
int i,j;
queue<dian>que;
dian in,out;
in.x=x;
in.y=y;
que.push(in);
while(!que.empty())
{
in=que.front();
que.pop();
dian next;
for(i=;i<;i++)
{
next.x=out.x=in.x+fangxiang[i][];
next.y=out.y=in.y+fangxiang[i][];
if(next.x<||next.x>n||next.y<||next.y>m)
continue;
if(map[next.x][next.y]=='@')
{
map[next.x][next.y]='*';
BFS(next.x,next.y);
}
}
}
}
int main()
{
int i,j,sum;
while(cin>>n>>m,m)
{
sum=;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
cin>>map[i][j];
}
}
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
if(map[i][j]=='@')
{
sum+=;
map[i][j]='*';
BFS(i,j);
}
}
}
cout<<sum<<endl;
}
return ;
}

hdoj1241 Oil Deposits的更多相关文章

  1. 【伪一周小结(没错我一周就做了这么点微小的工作)】HDOJ-1241 Oil Deposits 初次AC粗糙版对比代码框架重构版

    2016 11月最后一周 这一周复习了一下目前大概了解的唯一算法--深度优先搜索算法(DFS).关于各种细节的处理还是极为不熟练,根据题意判断是否还原标记也无法轻松得出结论.不得不说,距离一个准ACM ...

  2. Oil Deposits

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

  3. Oil Deposits(dfs)

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

  4. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  5. uva 572 oil deposits——yhx

    Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil d ...

  6. hdu 1241:Oil Deposits(DFS)

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

  7. hdu1241 Oil Deposits

    Oil Deposits                                                 Time Limit: 2000/1000 MS (Java/Others)  ...

  8. 杭电1241 Oil Deposits

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

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

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

随机推荐

  1. 微信小程序-开发入门(一)

    微信小程序已经火了一段时间了,之前一直也在关注,就这半年的发展来看,相对原生APP大部分公司还是不愿意将主营业务放到微信平台上,以免受制于腾讯,不过就小程序的应用场景(用完即走和二维码分发等)还是很值 ...

  2. eclipse 显示特殊符号

    windows --> Preferences --> General --> Editors --> Text Editors 右边 show white space let ...

  3. TransactionScope 的基本原理简介

    C# 的事务编程 1 Db事务 DbConnection 中创建基于当前连接的 DbTransaction 2  使用TransactionScope ,创建环境事务 一旦创建,在这个环境包含的DbC ...

  4. HTTP头返回码分析

    http协议通讯时,在客户端发送请求后(request),服务器端返回的状态码解释(response) http状态码 1**:请求收到,继续处理 2**:操作成功收到,分析.接受           ...

  5. TaskController.java 20160712

    package main.java.com.zte.controller.system; import java.io.PrintWriter; import java.util.ArrayList; ...

  6. mysql优化之参数优化

    1.优化方式 硬件优化=>系统优化=>mysql配置优化=>SCHEMA优化=>sql优化=>其他解决方案(redis or MongoDB or Cassandra o ...

  7. HashTable、List、ArrayList的经典使用和相互转换

    1.添加引用 using System.Collections; 2.创建并添加数据 Hashtable hs = new Hashtable(); hs.Add("Name1", ...

  8. Linux各主要发行版的包管理命令对照

    Linux各主要发行版的包管理命令对照 Debian使用的是apt和dpkg,Gentoo则用的是emerge,Redhat的yum.Suse的zypper.Arch的pacman.Slackware ...

  9. Swift代理造成内存泄漏的解决办法

    在swift中,使用代理 ,可能很多人会这样实现: .首先定义一份协议. protocol ToolProrocol{ //代理方法 func didRecieveResults(result:Int ...

  10. IE浏览器SCRIPT5拒绝访问,谷歌浏览器XMLHttpRequest can't load file:/......

    一.背景 在测试ajax时,写了一个ajax.html,目的是访问example.txt中的文本,写好后,右键该html选择在浏览器中打开,浏览器页面上无内容.调出调试窗口: IE浏览器:SCRIPT ...