博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1241 Oil Deposits 题解
阅读量:6866 次
发布时间:2019-06-26

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

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 45017    Accepted Submission(s): 25972

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.
 

 

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
 

 

Sample Output
0 1 2 2
 

 

Source
 

 

Recommend
Eddy
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
本题应为DFS的题目
但由于博主学DFS学的比较菜
 
所以博主没有采用DFS
而是采用了多次BFS的方式
逐行逐列搜索map
一旦找到一个@就向八个方向BFS一次
每次BFS的时候把@变为*
这样搜完整片map
就结束
下面上我全是注释
谁都看的懂的代码
 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 //Author:LanceYu 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 #include
23 #include
24 #define ll long long25 using namespace std;26 const double clf=1e-8;27 //const double e=2.718281828;28 const double PI=3.141592653589793;29 const int MMAX=2147483647;30 //priority_queue
p;31 //priority_queue
,greater
>pq;32 int dir[8][2]={ {-1,0},{ 1,0},{ 0,-1},{ 0,1},{-1,-1},{-1,1},{ 1,1},{ 1,-1}};//此处应为八个方向 33 int n,m;34 char a[101][101];35 struct node36 {37 int x,y;38 };39 void bfs(int x,int y)40 { 41 int i;42 queue
q;43 node g;44 g.x=x;g.y=y;45 q.push(g);46 a[x][y]='*';47 while(!q.empty())48 {49 node t=q.front();50 q.pop();51 for(i=0;i<8;i++)//八个方向寻找是否成片 52 {53 int dx=t.x+dir[i][0];54 int dy=t.y+dir[i][1];55 if(dx>=0&&dy>=0&&dx
 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Notes:DFS的方法暂时还没有想好,但博主认为这样的方法更好
2018-11-20  02:11:33  Author:LanceYu

转载于:https://www.cnblogs.com/lanceyu/p/9986846.html

你可能感兴趣的文章
微软私有云POC部署文档
查看>>
云计算
查看>>
mysql中的主从复制slave-skip-errors参数使用方法
查看>>
永久关闭wps热点新闻的办法
查看>>
飞信机器人安装
查看>>
修改一个字段中的部分内容
查看>>
kubernetes-1.11.0集群部署之master集群 (二)
查看>>
POJ_2001_Shortest Prefixes
查看>>
Webpack 的 HtmlWebpackPlugin 如何控制某个 chunks 的 inject 位置?
查看>>
Silverlight C# 游戏开发:未写代码先设计
查看>>
return false
查看>>
BZOJ3769:BST again(记忆化搜索DP)
查看>>
第二章:演化架构师
查看>>
20165315 第八周考试课下补做
查看>>
学习CAS实现SSO单点登录
查看>>
同步异步的知识补充
查看>>
css关于定位那些事情
查看>>
WCF IIS上部署服务
查看>>
微软职位内部推荐-Software Development Engineering II
查看>>
Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK
查看>>