题目描述:

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.

输入:

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.

输出:

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.

样例输入:
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
样例输出:
0
1
2
2
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
char maze[][];
bool mark[][];
int go[][]={
,,
-,,
,,
,-,
-,-,
,,
-,,
,-
};
int m,n;
void dfs(int x,int y){
for (int i=;i<;i++){
int nx=x+go[i][];
int ny=y+go[i][];
if (nx<||nx>=m||ny<||ny>=n) continue;
if (maze[nx][ny]=='*' || mark[nx][ny]==true) continue;
mark[nx][ny]=true;
dfs(nx,ny);
}
return;
} int main (){ while (cin>>m>>n && !(m==&&n==)){
for (int i=;i<m;i++){
//getchar();
for (int j=;j<n;j++){
//scanf("%c",maze[i][j]);
cin>>maze[i][j];
mark[i][j]=false;
}
} int ans=;
for (int i=;i<m;i++){
for (int j=;j<n;j++){
if (maze[i][j]=='*') continue;
if (mark[i][j]==true) continue;
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
} return ;
}

这种将相邻的点合成块的算法有一个专有名词,floodfill

Oil Deposit的更多相关文章

  1. 题目1460:Oil Deposit(递归遍历图)

    题目链接:http://ac.jobdu.com/problem.php?pid=1460 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  2. 九度oj 题目1460:Oil Deposit

    题目描述: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. ...

  3. Oil Deposits

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

  4. Oil Deposits(dfs)

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

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

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

  6. uva 572 oil deposits——yhx

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

  7. hdu 1241:Oil Deposits(DFS)

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

  8. hdu1241 Oil Deposits

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

  9. 杭电1241 Oil Deposits

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

随机推荐

  1. 4 - BFS & Topological Algorithm

    615. Course Schedule https://www.lintcode.com/problem/course-schedule/description?_from=ladder&& ...

  2. .class 缓存

    项目用的是Ant. 场景: Class A{ private static final String HHH="hello"; } Class B{ public void met ...

  3. mybatis-generator自动生成Mapper.dao.entity

    在pom.xml中添加依赖 <plugin> <groupId>org.mybatis.generator</groupId> <artifactId> ...

  4. 为何存在requests库,pycharm依然报错解决方法 --转载

    原文地址:https://www.jianshu.com/p/e28a72ba7809 今天在使用pycharm的时候,用到了第三档库requests,提示有错误,报错显示 No module nam ...

  5. angular6 input节流

    一直以为   pipe(debounceTime(1000), distinctUntilChanged())  不起作用 原因:使用方法错误 <input type="text&qu ...

  6. CSS3特性

    2018-08-20 CSS3:用于控制网页的样式和布局 1.transform:rotate(30deg);      CSS3 模块 选择器 盒模型 背景和边框 文字特效 2D/3D转换 动画 多 ...

  7. 关于sqlserver数据库max()方法的使用

    1.传送门:http://www.codesky.net/article/201009/144935.html 2.copy自传送门里的内容: max(字符串内容): 说明对字符型数据的最大值,是按照 ...

  8. idea新建maven工程没有artifacts

    原理,是因为没把新创建好的maven项目给设置成一个可被tomcat部署的web项目 选择个项目路径 配置artifacts,引入到tomcat就可以

  9. Maximum Sum of Digits(CodeForces 1060B)

    Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...

  10. 不同版本的IDE ,对应的选项 有变化

    xx.dproj对应的项目级选项 也不同,所以,要分别保存为不同的文件. 如果不同的IDE,打开同一个 .dproj文件,会因为 选项界面的选项不同,提示一些错误.