Oil deposits
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 23018 Accepted Submission (s): 13272
Problem DescriptionThe 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.
Inputthe input file 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.
Outputfor each grid, output the number of distinct oil deposits. The different pockets is part of the same oil deposit if they is adjacent horizontally, vertically, or diagonally. An oil deposit won't contain more than pockets.
Sample Input1 1*3 5*@*@***@***@*@*1 8@@****@*5 5 ****@*@@*@*@**@@@@*@@@**@0 0
Sample Output0122 just learned deep search, do a summary. DFS follow a road to go, do not hit the south wall does not look back, back to the words also go back to the last time the DFS () function, and then see if there are still satisfied with the condition, if there is the condition of the place has been searched down (to the bottom, always down), until the south wall hit, Deep Search is to all the places are traversed once, a novice summary, not to point out please correct me!! Attached code:
#include <stdio.h>
#include <string.h>
int m,n,total = 0,book[101][101];
Char c[101][101];
int next[8][2]=
{
{0,1},//Right
{1,1},//bottom Right
{1,0},//under
{1,-1},//lower Left
{0,-1},//Left
{ -1,-1},//upper Left
{ -1,0},//on
{ -1,1}//on top right
};
void Dfs (int x,int y)
{
int tx,ty,k;
for (k=0;k<8;k++)
{
tx = X+next[k][0];
ty = y+next[k][1];
if (tx<1| | ty<1| | tx>m| | Ty>n)
{
Continue
}
if (c[tx][ty]== ' @ ' &&book[tx][ty]==0)
{
total++;//can't add 1 here because it's an oil field that's keeping up with an oil field.
Book[tx][ty] = 1;
DFS (tx,ty);//8 one satisfies the condition, takes this one as the center, continues to search, if does not satisfy the condition, returns, then last did not search the search
Book[tx][ty] = 0;//Each of the lattice went once, searched no longer search
}
}
Return
}
int main ()
{
int i,j;
while (scanf ("%d%d", &m,&n) &&m!=0)
{
GetChar ();
Total = 0;
memset (book,0,sizeof (book));
for (i=1;i<=m;i++) {
for (j=1;j<=n;j++) {
scanf ("%c", &c[i][j]);
}
GetChar ();
}
for (i=1;i<=m;i++)
{
for (j=1;j<=n;j++)
{
if (c[i][j]== ' @ ' && book[i][j]==0)
{
total++;//Plus it itself
DFS (I,J);//search for its eight directions
}
}
}
printf ("%d\n", total);
}
return 0;
}
Don't hit the south wall don't look back ——— depth First search (DFS) Oil deposits