Oil deposits
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 15557 |
|
Accepted: 8431 |
Description
The Geosurvcomp Geologic Survey company are 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 Numer OUs square plots. It then analyzes each plot separately, using a sensing equipment to determine whether or not of the plot contains oil. A plot containing oil is called a pocket. If the pockets is adjacent, then they is part of the same oil deposit. Oil deposits can is quite large and may contain numerous pockets. Your job is to determine how many different oil deposits be contained in a grid.
Input
The input contains one or more grids. Each of the grids 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 <= and 1 <= n <= 100. Following this is m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and are either ' * ', representing the absence of oil, or ' @ ', representing a oil PO Cket.
Output
is adjacent horizontally, vertically, or diagonally. An oil deposit won't contain more than pockets.
Sample Input
1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output
0122
Java AC Code
ImportJava.util.Scanner; Public classMain {StaticString field[][]; Static introws; Static intcolumns; Static intpockets; Static Booleanmarked[][]; Static intDx[] = {-1, 0, 1,-1, 1,-1, 0, 1}; Static intDy[] = {1, 1, 1, 0, 0,-1,-1,-1}; Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); while((Rows =sc.nextint ())! = 0 && (columns = Sc.nextint ())! = 0) {Pockets= 0; Field=NewString[rows][columns]; Marked=New Boolean[Rows][columns]; for(inti = 0; i < rows; i++) {String line=Sc.next (); for(intj = 0; J < columns; J + +) {Field[i][j]= Line.substring (J, j + 1); } } for(inti = 0; i < rows; i++) for(intj = 0; J < columns; J + +) { if(!marked[i][j] && field[i][j].equals ("@") {DFS (i, j); Pockets++; }} System.out.println (pockets); } } Public Static voidDfsintRowintCol) { for(inti = 0; I < 8; i++) { int_row = row +Dy[i]; int_col = col +Dx[i]; if(_row < rows && _row >= 0 && _col < columns && _col >= 0 &&!marked[_row][_col ] && field[_row][_col].equals ("@") ) {Marked[_row][_col]=true; DFS (_row, _col); } } }}
POJ 1562 oil Deposits (DFS)