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

你可能感兴趣的文章
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT工作笔记0007---剩余长度
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>
Mqtt搭建代理服务器进行通信-浅析
查看>>
MS Edge浏览器“STATUS_INVALID_IMAGE_HASH“兼容性问题
查看>>