Oil Deposits

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

Total Submission(s): 11842    Accepted Submission(s): 6873

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   |   We have carefully selected several similar problems for you:  1016 1010 1312 1242 1240 
简单搜索    

直接帖代码
dfs版:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char date[110][110];
bool map[110][110];
int n,m;
int dfs(int a,int b)
{
if(map[a][b]==0) return 0;
else {
map[a][b]=0;
if(a>0){
if(b>0) dfs(a-1,b-1);
if(b<n-1) {
dfs(a-1,b+1);
}
dfs(a-1,b);
}
if(a<m-1)
{
if(b>0) dfs(a+1,b-1);
if(b<n-1)dfs(a+1,b+1);
dfs(a+1,b);
}
if(b>0) dfs(a,b-1);
if(b<n-1) dfs(a,b+1);
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
//printf("%d %d\n",m,n);
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++) cin>>date[i];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++)
{
map[i][j]=date[i][j]=='@'?true:false;
}//puts("");
}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=dfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}

bfs版

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char map[110][110];
int n,m;
queue<pair<int,int> > q;
int bfs(int a,int b)
{
if(map[a][b]=='*') return 0;
else {
q.push(make_pair(a,b));
while (!q.empty())
{
int x,y;
x=q.front().first;
y=q.front().second;
if(x>0){
if(y>0) if(map[x-1][y-1]=='@'){
map[x-1][y-1]='*';
q.push(make_pair(x-1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x-1][y+1]=='@')
{
map[x-1][y+1]='*';
q.push(make_pair(x-1,y+1));
}//bfs(a-1,b+1);
if(map[x-1][y]=='@')
{
map[x-1][y]='*';
q.push(make_pair(x-1,y));
}//bfs(a-1,b);
}
if(x<m-1)
{
if(y>0) if(map[x+1][y-1]=='@'){
map[x+1][y-1]='*';
q.push(make_pair(x+1,y-1));
}//bfs(a-1,b-1);
if(y<n-1) if(map[x+1][y+1]=='@')
{
map[x+1][y+1]='*';
q.push(make_pair(x+1,y+1));
}//bfs(a-1,b+1);
if(map[x+1][y]=='@')
{
map[x+1][y]='*';
q.push(make_pair(x+1,y));
}//bfs(a-1,b);
}
if(y>0)
if(map[x][y-1]=='@')
{
map[x][y-1]='*';
q.push(make_pair(x,y-1));
}//bfs(a,b-1);
if(y<n-1)
if(map[x][y+1]=='@')
{
map[x][y+1]='*';
q.push(make_pair(x,y+1));
}//bfs(a,b+1);
q.pop();
}
return 1;
}
}
int main()
{
char chare;
int ans;
//freopen("in.txt","r",stdin);
while (~scanf("%d%d",&m,&n))
{
if(n==0&&m==0) break;
ans=0;
chare='a';
for (int i=0;i<m;i++){ cin>>map[i];}
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
ans+=bfs(i,j);
}
}
printf("%d\n",ans);
}
return 0;
}







hud 1241 Oil Deposits的更多相关文章

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

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

  2. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  3. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  4. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  6. hdu 1241:Oil Deposits(DFS)

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

  7. 杭电1241 Oil Deposits

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

  8. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

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

  9. HDU 1241 Oil Deposits (DFS/BFS)

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

随机推荐

  1. 【PHP框架CodeIgniter学习】使用辅助函数—建立自己的JSONHelper

    本文使用的是2.1.4版本,看的时候请注意. 官方文档:http://codeigniter.org.cn/user_guide/general/helpers.html(关于辅助函数Helper的使 ...

  2. SEO 网站页面SEO优化之页面title标题优化

    在seo优化中,标题的优化占着举足轻重的地位,无论是从用户体验的角度出发,还是从搜索引擎的排名效果出发,title标题都是页面优化最最重要的因素.笔者总结了优化title标题应该注意的六个方面: ①. ...

  3. linux下查看机器的硬件信息:

    查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c       8  Intel(R) Xeon(R) CPU    ...

  4. [状压dp]HDOJ3182 Hamburger Magi

    题意 大致是: 有n个汉堡 m块钱  (n<=15) 然后分别给n个汉堡的能量 再分别给n个汉堡所需的花费 然后下面n行 第i行有x个汉堡要在i汉堡之前吃 然后给出这x个汉堡的编号 输出 能获得 ...

  5. CodeChef A

    问题是求出斐波那契数列的第n个,这里要用大数加法预处理,然后就可以了 代码: #include <iostream> #include <sstream> #include & ...

  6. JAVA大数类练手

    今天突然看到了OJ上的大数类题目,由于学习了一点大数类的知识.果断水了6道题......都是非常基础的.就当的练手的吧. 学到的只是一些大数类的基本操作.以后多做点这样的题,争取熟练运用水大数题... ...

  7. MS提供的虚拟机IE测试

    https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/

  8. iOS,Android网络抓包教程之tcpdump

    现在的移动端应用几乎都会通过网络请求来和服务器交互,通过抓包来诊断和网络相关的bug是程序员的重要技能之一.抓包的手段有很多:针对http和https可以使用Charles设置代理来做,对于更广泛的协 ...

  9. Drawable(2)State list Drawable Resource介绍

    State List A StateListDrawable is a drawable object defined in XML that uses a several different ima ...

  10. PHP 'ext/gd/gd.c' gdImageCrop整数符号错误漏洞

    漏洞版本: PHP 5.5.x 漏洞描述: CVE ID:CVE-2013-7328 PHP是一种HTML内嵌式的语言. PHP 'ext/gd/gd.c' gdImageCrop函数存在多个整数符号 ...