E - Oil Deposits(石油库)(BFS)题解
【题】
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
还想看???不好意思,真没了。

浙公网安备 33010602011771号