E - Oil Deposits(石油库)(BFS)题解

【题】

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. 

??????????【如果让你看不懂,我写来何用】???????????

GeoSurvComp地质调查公司负责探测地下石油矿床。GeoSurvComp一次处理一个大的矩形区域,并创建一个网格,将土地划分为许多正方形地块。然后,它分别分析每个图,使用传感设备来确定图中是否含有石油。一块含有石油的地皮叫做口袋。如果两个凹陷相邻,则它们是同一个油藏的一部分。石油储量可能相当大,并可能包含许多口袋。你的工作是确定一个网格中有多少不同的石油储量。

输入

输入文件包含一个或多个网格。每个网格以包含m和n的行开始,m和n是网格中的行数和列数,由一个空格分隔。如果m=0,则表示输入结束;否则,1<=m<=100,1<=n<=100。接下来是m行,每行n个字符(不包括行尾字符)。每个字符对应一个图,或者是表示没有油的“*”,或者是表示油袋的“@”。

输出

对于每个网格,输出不同的石油储量数量。如果水平、垂直或对角相邻,则两个不同的凹陷是同一个油藏的一部分。一个储油罐的容量不能超过100个。

【解析】

1.嗯en,,,没记错的话,似乎是我做的第一个BFS题目,对于那时的我来说,@#¥%……&*()()*&……%¥#@。。。。

2.题目也就是把连起来(8个方向)的石油口袋算作一个石油库,问有多少个?

3.基本BFS + 遇到一个筛一遍8个方向的(就把那些都当作访问过的,或者改变石油口袋的储存字符使其不再被找到也行)。

4.没了,真没了。

【还是代码好说话】

复制代码
 1 #include<iostream>
 2 #include<cstring>
 3 #include<queue>
 4 using namespace std;
 5 char mp[101][101];
 6 int vis[101][101],m, n;
 7 int fx[] = { 1,1,1,0,0,-1,-1,-1 };
 8 int fy[] = { 1,0,-1,1,-1,1,0,-1 };
 9 struct ii
10 {
11     int x, y;
12 };
13 void bfs(int x, int y)
14 {
15     queue< ii > q;
16     q.push({ x,y });
17     while (q.size())
18     {
19         ii now = q.front();
20         q.pop();
21         for (int i = 0; i < 8; i++)
22         {
23             int a= now.x + fx[i];
24             int b= now.y + fy[i];
25             if (vis[a][b] && mp[a][b] == '@' && a >= 0 && a < m && b >= 0 && b < n)
26             {
27                 vis[a][b]=0;
28                 q.push({ a,b });
29             }
30         }
31     }
32     return ;
33 }
34 int main()
35 {
36     ios_base::sync_with_stdio(false);
37     cin.tie(0);
38     while (cin >> m >> n)
39     {
40         if(!n&&!m)break;
41         int sum=0;
42         memset(vis,1,sizeof vis);
43         
44         for (int i = 0; i < m; i++)
45         for (int k = 0; k < n; k++)
46         cin >> mp[i][k];
47         
48         for (int i = 0; i < m; i++)
49         for (int k = 0; k < n; k++)
50         if (mp[i][k] == '@' && vis[i][k])
51         bfs(i, k),sum++;
52         
53         cout<<sum<<endl;
54     }
55     return 0;
56 }
规规矩矩,工工整整
复制代码
OVER
还想看???不好意思,真没了。

posted @   Renhr  阅读(247)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!
点击右上角即可分享
微信分享提示