HDU 1241 Oil Depositsl-dfsTime
limit:1000MS
Memory Limit:10000KB
64bit IO Format:%i64d &%i64 U
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
A classic Dfs search problem, search the number of connected blocks
AC Code:
#include <cstdio>#include<cstring>Const intmaxn= -+5;CharPIC[MAXN][MAXN];intM,N,IDX[MAXN][MAXN];voidDfsintRintCintID) { if(r<0|| r>=m| | c<0|| c>=N)//Determine out of boundsreturn; if(idx[r][c]>0|| pic[r][c]!='@')//not ' @ ' or already visited latticereturn; IDX[R][C]=ID; Connected Component Number for(intdr=-1; dr<=1; dr++) for(intdc=-1; dc<=1; Dc++) if(dr!=0|| dc!=0) DFS (R+dr,c+dc,id);}intMain () {inti,j; while(SCANF ("%d%d", &m,&n) = =2&&m&&N) { for(i =0; i<m; i++) scanf ("%s", Pic[i]); memset (IDX,0,sizeof(IDX)); intq=0; for(i=0; i<m; i++) for(j=0; j<n; J + +) if(idx[i][j]==0&&pic[i][j]=='@') Dfs (I,J,++q); printf ("%d\n", q); } return 0;}
Hdu 1241 Oil deposits (Dfs seek connectivity block)