Oil Deposits(DFS,基础题)

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5776    Accepted Submission(s): 3360

原题链接:点击打开链接


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
 
开始时想用并查集。。。后来还是深搜算了,程序很快写出来,可是测试例子的时候却发现有误,debug半天才发现原来是复制网站上的例子的时候换行符不知道为何没复制过去。。。
AC CODE
 1 //Memory: 344 KB         Time: 0 MS
 2 //Language: C++         Result: Accepted
 3 
 4 #include <iostream>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <queue>
11 #define LL long long
12 #define MAXI 2147483647
13 #define MAXL 9223372036854775807
14 #define dg(i) cout << "*" << i << endl;
15 using namespace std;
16 
17 typedef struct Node
18 {
19     int x, y;
20     Node(int i, int j)
21     {
22         x = j; y = i;
23     }
24 }P;
25 queue<Node> Q;
26 int n, m;
27 char grid[101][101];
28 int move[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
29 
30 void DFS(int i, int j)
31 {
32     int ii, jj, k;
33     for(k = 0; k < 8; k++)
34     {
35         ii = i + move[k][0];
36         jj = j + move[k][1];
37         if(ii > -1 && jj > -1 && ii < m && jj < n && grid[ii][jj] == '@')
38         {
39             grid[ii][jj] = '*';
40             DFS(ii, jj);
41         }
42     }
43 }
44 
45 int main()
46 {
47     int i, j, cnt;
48     char c;
49     while(scanf("%d %d", &m, &n) && m)
50     {
51         cnt = 0;
52         for(i = 0; i < m; i++)
53         {
54             scanf("%c", &c); //吃掉换行符
55             for(j = 0; j < n; j++)
56             {
57                 scanf("%c", &grid[i][j]);
58                 if(grid[i][j] == '@')
59                 {
60                     P t(i, j);
61                     Q.push(t);
62                 }
63             }
64         }
65         while(!Q.empty())
66         {
67             P t = Q.front();
68             Q.pop();
69             if(grid[t.y][t.x] == '@')
70             {
71                 cnt++;
72                 DFS(t.y, t.x);
73             }
74         }
75         printf("%d\n", cnt);
76     }
77     return 0;
78 }

原文地址:https://www.cnblogs.com/cszlg/p/2910464.html