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. Oracle 删除表中记录 如何释放表及表空间大小

    1.查看一个表所占的空间大小:SELECT bytes/1024/1024 ||'MB' TABLE_SIZE ,u.* FROM USER_SEGMENTS U WHERE U.SEGMENT_NA ...

  2. List(T)类的方法

    List<T>.Clear 方法 List<T>.RemoveAll 方法 http://msdn.microsoft.com/zh-cn/library/s6hkc2c4(v ...

  3. 【安卓】给gallery内&quot;控件&quot;挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决、!

    思路: 1.gallery内控件挂载事件(如:onClickListener)的方法类似listview,可直接在baseAdapter.getView内给控件挂载(详细方法百度). 2.貌似没问题, ...

  4. MySQL Desc指令相关

    MySQL Desc指令相关   2011-08-09 11:25:50|  分类: my基本命令 |举报 |字号 订阅 1.desc tablename; 例如 :mysql> desc jo ...

  5. 金山PDF

    金山是个很不错的软件公司,金山出PDF,纯粹是完善生态圈!毕竟没FoxitReader专业对PDF的处理上! 官网:芝麻开门 下载:http://wdl1.cache.wps.cn/wps/downl ...

  6. 获取网站资源 getResourceAsStream

    获取网站资源(重点) public void doGet(HttpServletRequest request, HttpServletResponse response)throws Servlet ...

  7. HDUOJ---(2203)亲和串

    亲和串 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  8. Fn键

    需求分析 我想开机禁用触摸板. 方案设计 安装驱动:比较麻烦,驱动也不一定支持开机禁用触摸板. 编程实现,让一段代码开机禁用触摸板 编程实现也分好几种方法: 使用windows API禁用触摸板,这需 ...

  9. eclipse 在weblogic部署的工程项目开启远程调试remote config eclipse远程调试配置

    确认你的工程在weblogic中跑的起来,然后再结合eclipse debug配置+java debug运行模式搞个调试. 工程能跑起来没问题后,先在eclipse中,点击debug图标 然后点击De ...

  10. Android JUnit 入门指南

    自动化单元测试可以做许多的事,并帮你节省时间.它也可以被用作快速检验新建工程或进行冒烟测试.始终,单元测试是作为一种有效的.系统的检验应用程序各功能执行的方式.Android SDK支持JUnit的自 ...