1411272217-hd-Oil Deposits
Oil DepositsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission (s): 13068 Accepted Submission (s): 7571
Problem DescriptionThe 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.
InputThe 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.
OutputFor 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
0122
Calculate the number of @ In the question. If you calculate a group around it, calculate the number of @ groups in total. This question uses deep search knowledge. Find a @, total number + 1, and then use a recursive method to determine the situation around it. If there is @ around it, convert it *. Code
# Include
Char a [110] [110]; int m, n; // save it outside as the global Variable void dfs (int x, int y) {if (x <0 | x> = n | y <0 | y> = m) return; if (a [x] [y] = '@') {a [x] [y] = '*'; dfs (x-1, Y-1); dfs (x, y-1); dfs (x + 1, Y-1); dfs (x-1, y); dfs (x + 1, y); dfs (x-1, y + 1 ); dfs (x, y + 1); dfs (x + 1, y + 1); // multiple values can be recursion at the same time. } // Use recursion to search. Return;} int main () {int I, j, k; int sum; while (scanf ("% d", & n, & m), n + m) {getchar (); for (I = 0; I