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. Install-DedupCore.component 的内容

    [amd64_microsoft-windows-dedup-common_31bf3856ad364e35_6.3.9600.16384_none_24924b7b049f1064] CF=0000 ...

  2. java 将Map拷贝到另一个Map对象当中

      java 将Map拷贝到另一个Map对象当中 CreateTime--2018年6月4日09点46分 Author:Marydon 1.需求说明 将一个MapA对象中所有的键值对完全拷贝到另一个M ...

  3. xmlhttp.readyState==4 && xmlhttp.status==200的探究

    作为一个后端人员,很惭愧,对Ajax的使用只局限在功能实现层面的交互,对底层通过XMLHttpRequest对象来使用的知识却没有仔细研究过.现总结如下 1. XMLHttpRequest 对象的相关 ...

  4. C-C和指针作业题(第一章)

    在Linux下输入EOF使用Ctrl+D, Windows下输入EOF使用Ctrl+Z 编写一个程序,从标准输入读取几行输入,每行输入都要打印到标准输出上,前面要加上行号,在编写这个程序时要试图让程序 ...

  5. python函数中把列表(list)当参数时的"入坑"与"出坑"

    在Python函数中,传递的参数如果默认有一个为 列表(list),那么就要注意了,此处有坑!! 入坑 def f(x,li=[]): for i in range(x): li.append(i*i ...

  6. Flask接通微信公众号

    import hashlib import xml.etree.ElementTree as ET from flask import Flask, request import time app = ...

  7. POJ 3007 Organize Your Train part II (字典树 静态)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6478   Acce ...

  8. 解决spring el表达式不起作用

    el表达式不起作用,如下图所示 现象: 在显示页面中加入: <%@ page isELIgnored="false" %>就OK了 参考:http://bbs.csdn ...

  9. JavaScript和JQuery中的事件\委托链\事件冒泡\事件捕获,兼容所有浏览器

    有做过北大青鸟培训讲师经验的我,如今在一家公司做技术部经理的职位,发现有很多程序员的基本功相当糟糕,在组织企业内部培训时讲解了一些案例,总结了一些经典代码,希望对自己和有需要的人提供一些帮助吧: Ja ...

  10. Python练习笔记——通讯录查询V1.0

    作业: 编写一个代码,实现手机通讯录管理 实现功能:增.删.改.查 字典,列表 [扩展]不要求一定做出来 实现拼音首字母查找 phone = {} while True: num = input(&q ...