Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 7813 Accepted Submission (s): 4583
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 in 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 in more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
Source
Mid-Central USA 1997
Recommend
Eddy
Question: an oilfield. @ Indicates there is oil, * indicates there is no oil. How many blocks of land with oil (horizontal, vertical, or diagonal lines with @ together can only be regarded as a whole ).
Analysis: There are two loops directly. When @ is met, the counter is + 1, and dfs changes all adjacent @ around this position to * to avoid repeated counting.
Code:
# Include <iostream> # include <cstdio> # include <cstring> using namespace std; char maps [105] [105]; int dx [] =, -,-}; int dy [] = {,-,-, 1,-1}; int r, c, flag; void dfs (int x, int y) {maps [x] [y] = '*'; int xx, yy; for (int I = 0; I <8; I ++) {xx = x + dx [I]; yy = y + dy [I]; if (xx> = 1 & xx <= r & yy> = 1 & yy <= c & maps [xx] [yy] = '@') {// printf ("test: % d \ n", xx, yy); maps [xx] [yy] = '*'; dfs (xx, yy) ;}} int main () {int I, j, res; char ch [30]; while (scanf ("% d", & r, & c) & r & c) {// getchar (); gets (ch); // you can use getchar () twice () res = 0; for (I = 1; I <= r; I ++) gets (maps [I] + 1); for (I = 1; I <= r; I ++) for (j = 1; j <= c; j ++) {if (maps [I] [j] = '@') {++ res; dfs (I, j) ;}} printf ("% d \ n", res) ;}return 0 ;}