概述
572 - Oil Deposits
Time limit: 3.000 seconds
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
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=513&mosmsg=Submission+received+with+ID+17772438
DFS搜索
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 using namespace std;
6 #define maxn 105
7 char pic[maxn][maxn];
8 int m,n;
9 int idx[maxn][maxn];
10
11 void dfs(int r, int c, int id)
12 {
13 if(r < 0 || r >= m || c < 0 || c >= n)//出界
14 return;
15 if(idx[r][c] > 0 || pic[r][c] != '@')
16 return;
17 idx[r][c] = id;//连通分量编号
18 for(int dr = -1; dr <= 1; dr++)
19 {
20 for(int dc = -1; dc <= 1; dc++)
21 {
22 if(dr != 0 || dc != 0)
23 dfs(r+dr, c+dc, id);
24 }
25 }
26 }
27 int main()
28 {
29 while(cin >> m >> n, m && n)
30 {
31 for(int i = 0; i < m; i++)
32 scanf("%s",pic[i]);
33 memset(idx,0,sizeof(idx));
34 int cnt = 0;
35 for(int i = 0; i < m; i++)
36 {
37 for(int j = 0; j < n; j++)
38 {
39 if(idx[i][j] == 0 && pic[i][j] == '@')
40 dfs(i,j,++cnt);
41 }
42 }
43 cout << cnt << endl;
44 }
45 return 0;
46 }
转载于:https://www.cnblogs.com/Mino521/p/5729414.html
最后
以上就是高兴枕头为你收集整理的UVa572 - Oil Deposits的全部内容,希望文章能够帮你解决UVa572 - Oil Deposits所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复