博客
关于我
dfs算法-例题油田
阅读量:168 次
发布时间:2019-02-28

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

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.

问题描述

GeoSurvComp地质调查公司负责检测地下石油储量。GeoSurvComp一次与一个大的矩形区域一起工作,并创建一个网格,将土地划分为多个方形图。然后,它使用传感设备分别分析每个图,以确定该图是否含有油。含有油的地块称为口袋。如果两个口袋相邻,则它们是相同油藏的一部分。油沉积物可能非常大并且可能包含许多口袋。您的工作是确定网格中包含多少不同的油藏。

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.

输入

输入文件包含一个或多个网格。每个网格都以包含m和n的行开头,即网格中的行数和列数,由单个空格分隔。如果m = 0,则表示输入结束;否则1 <= m <= 100并且1 <= n <= 100.此后是m行,每行n个字符(不计行行尾字符)。每个字符对应一个图,并且是’*’,表示没有油,或’@’,代表一个油口袋。

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.

输出

对于每个网格,输出不同的油藏数量。如果两个不同的口袋水平,垂直或对角相邻,则它们是相同油藏的一部分。油藏不得超过100个口袋。

Sample Input

1 1
*
3 5
@@*
@
@@*
1 8
@@***@
5 5
****@
@@@
@**@
@@@
@
@@**@
0 0

Sample Output

0
1
2
2
个人心得:这是dfs的经典入门题;
全局定义:

char map[101][101];int n, m, sum;

输入流:

for (i = 0; i < m; i++)   //输入流处理{	for (j = 0; j < n; j++)	{		cin >> map[i][j];	}}

然后就是判断一下越界条件,bfs函数里面作用是把有有油田的和它相邻的全部标记为另外的符号

ac代码:

#include 
#include
using namespace std;char map[101][101];int n, m, sum;void dfs(int i, int j){ if (map[i][j] != '@' || i < 0 || j < 0 || i >= m || j >= n) return; //若该点不可行或越界,返回 else //否则,标记该点为不可行,再往8个方向深搜 { map[i][j] = '!'; dfs(i - 1, j - 1); dfs(i - 1, j); dfs(i - 1, j + 1); dfs(i, j - 1); dfs(i, j + 1); dfs(i + 1, j - 1); dfs(i + 1, j); dfs(i + 1, j + 1); }}int main(){ int i, j; while (cin >> m >> n) //输入矩形边长 { if (m == 0 || n == 0) break;//越界条件 sum = 0; for (i = 0; i < m; i++) //输入流处理 { for (j = 0; j < n; j++) { cin >> map[i][j]; } } for (i = 0; i < m; i++) //搜索处理 { for (j = 0; j < n; j++) { if (map[i][j] == '@') { dfs(i, j); sum++; } } } cout << sum << endl; } return 0;}

转载地址:http://nztj.baihongyu.com/

你可能感兴趣的文章
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>