Hdu1241 (Oil Deposits)
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
A map is used to find out the number of @ combinations that are not connected (eight directions ).
Solution: First traverse the entire map, find an entry, and then use DFS for in-depth search, and mark it as * (this can avoid re-marking the traversed map with visit)
Import java. util. extends; public class P1241 {public static int [] [] dir = {}, {-}, {}, {0,-1 }, {-1, 1}, {-1,-1}, {1, 1}, {1,-1 }}; // traverse public static char [] [] map = new char [105] [105] in eight directions; // map public static int n, m; public static void main (String [] args) {nation SC = new Nation (System. in); String s; while (SC. hasNext () {n = SC. nextInt (); m = SC. nextInt (); int count = 0; if (n = 0 & m = 0) {break;} for (int I = 0; I
= 0 & x
= 0 & y