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. ConfigParser读取记事本、notepad++修改后的配置文件会出现:ConfigParser.MissingSectionHeaderError

    使用ConfigParser来读取配置文件,经常会发现经过记事本.notepad++修改后的配置文件读取时出现下面的问题: ConfigParser.MissingSectionHeaderError ...

  2. ffmpeg编译 --enable :没有命令

    参照官方推荐的编译:http://www.roman10.net/how-to-build-ffmpeg-with-ndk-r9/ build_config.sh总是不过, 问题原因:./config ...

  3. 两个表,一个表中的两列关联另一个表的id,如何将这个表中的两列显示为另一个表id对应的内容

    表A name user owner machine1 1 2 machine2 3 4 表B userid username 1 aaa 2 bbb 3 ccc 4 ddd 以上两个表,表A 设备的 ...

  4. configure文件中判断某函数或库是否存在的一个方法

    echo " #include<stdio.h> #include<openssl/ssl.h> int main() { return 0; } " &g ...

  5. C语言考试第一题详细过程

    1.计算 ,并输出其结果. 思路是先设计一个函数计算阶乘,再用循环,逐个求和. #include<stdio.h> int mul(int n) { int num,i; num=; ;i ...

  6. iOS8新增加的frameworks, 在目前基于7以上开发的情况下, 使用下列sdk要注意设置成optional

    Added frameworks: AVKitCloudKitCoreAudioKitCoreAuthenticationHealthKitHomeKitLocalAuthenticationMeta ...

  7. C#中使用自定义的纸张大小

    using System.Drawing.Printing; using System.Drawing; private void Test() { PrintDocument m_pdoc = ne ...

  8. [Irving]DateTime格式处理大全

    DateTime dt = DateTime.Now;//    Label1.Text = dt.ToString();//2005-11-5 13:21:25//    Label2.Text = ...

  9. 性能测试vs负载测试vs压力测试-概念普及

    下面我们主要介绍性能测试.负载测试和压力测试. 效率作为ISO 9126内部和外部质量的重要质量属性之一,其含义是在规定条件下,相对于所用的资源的数量,软件产品可提供适当性能的能力.资源可能包括其他软 ...

  10. win7下安装 WINDRIVER.TORNADO.V2.2.FOR.ARM

    [风河VxWorks].WINDRIVER.TORNADO.V2.2.FOR.ARM下载 http://115.com/file/dlfo8zpy http://115.com/file/c4r01l ...