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

你可能感兴趣的文章
multi_index_container
查看>>
mutiplemap 总结
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>