博客
关于我
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/

你可能感兴趣的文章
Mysql学习总结(80)——统计数据库的总记录数和库中各个表的数据量
查看>>
Mysql学习总结(81)——为什么MySQL不推荐使用uuid或者雪花id作为主键?
查看>>
Mysql学习总结(82)——MySQL逻辑删除与数据库唯一性约束如何解决?
查看>>
Mysql学习总结(83)——常用的几种分布式锁:ZK分布式锁、Redis分布式锁、数据库分布式锁、基于JDK的分布式锁方案对比总结
查看>>
Mysql学习总结(84)—— Mysql的主从复制延迟问题总结
查看>>
Mysql学习总结(85)——开发人员最应该明白的数据库设计原则
查看>>
Mysql学习总结(8)——MySql基本查询、连接查询、子查询、正则表达查询讲解
查看>>
Mysql学习总结(9)——MySql视图原理讲解与使用大全
查看>>
Mysql学习笔记 - 在Centos7环境下离线安装Mysql
查看>>
MySQL学习笔记十七:复制特性
查看>>
Mysql学习第一课-mysql的定义及sql语句
查看>>
mysql安全模式: sql_safe_updates
查看>>
mysql安装,卸载,连接
查看>>
MySQL安装之没有配置向导
查看>>
mysql安装出现 conflicts with mysql*的解决办法
查看>>
mysql安装卡在最后一步解决方案(附带万能安装方案)
查看>>
mysql安装和启动命令小结
查看>>
Mysql安装教程(命令行)
查看>>
mysql安装版安装
查看>>
MySQL安装配置教程(非常详细),从零基础入门到精通,看完这一篇就够了
查看>>