博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DFS or BFS --- 连通块
阅读量:6495 次
发布时间:2019-06-24

本文共 2530 字,大约阅读时间需要 8 分钟。

Oil Deposits

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64

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

 

【题目来源】

Mid-Central USA 1997

 

【题目大意】

在一个郊区的空地里,分散着很多油田,要你求油田的数量。

【题目分析】

就是一个简单的图搜索,不断标记,不断统计。

#include
#include
#define MAX 150using namespace std;char Map[MAX][MAX];int n,m;int cnt;int dir[8][2]={-1,0, -1,1, 0,1, 1,1, 1,0, 1,-1, 0,-1, -1,-1};void dfs(int x,int y){ Map[x][y]='*'; int xx; int yy; for(int i=0;i<8;i++) { xx=x+dir[i][0]; yy=y+dir[i][1]; if(Map[xx][yy]=='@') dfs(xx,yy); }}int main(){ while(cin>>n>>m,m) { getchar(); int i,j; cnt=0; for(i=1;i<=n;i++) { scanf("%s",Map[i]+1); } for(i=0;i<=n+1;i++) Map[i][0]=Map[i][m+1]='*'; for(i=0;i<=m+1;i++) Map[0][i]=Map[n+1][i]='*'; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(Map[i][j]=='@') {dfs(i,j); cnt++;} } } cout<
<

 

 

 

转载于:https://www.cnblogs.com/crazyacking/p/3748890.html

你可能感兴趣的文章
下面简要介绍软件工程的七条原理
查看>>
java POI实现excel实现表格导出
查看>>
Lua(三)——语句
查看>>
TensorFlow的基本运算01
查看>>
怎么看电脑有没有安装USB3.0驱动
查看>>
overflow清除浮动的原理
查看>>
Spring Boot 使用parent方式引用时 获取值属性方式默认@
查看>>
Elasticsearch之中文分词器插件es-ik(博主推荐)
查看>>
解决maven下载jar慢的问题(如何更换Maven下载源)
查看>>
linux安装gitLab
查看>>
concurrent包的实现示意图
查看>>
golang os.Args
查看>>
Linux常用命令
查看>>
【重磅】云栖社区2017年度内容特辑
查看>>
Java WEB开发时struts标签 显示set内容
查看>>
spring-data-elasticsearch 概述及入门(二)
查看>>
Solr启动和结束命令
查看>>
1.12 xshell密钥认证
查看>>
3.2 用户组管理
查看>>
awk
查看>>