The 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 numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
0 1 2 2
dfs的简单应用
#include<cstdio>
int n,m;
char grid[105][105];//存储网格;
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{0,-1},{1,1},{1,0},{1,-1}};
void DFS(int x,int y)//从(x,y)位置进行DFS
{
int i,xx,yy;
grid[x][y]='*';//进过后设置成*保证不会在经过了
for(i=0;i<8;i++)
{
xx=x+dir[i][0];
yy=y+dir[i][1];
if(xx>=n||yy>=m||xx<0||yy<0)//判断
continue;
if(grid[xx][yy]!='*')
DFS(xx,yy);
}
}
int main()
{
int i,j;
int count;//统计数目
while(scanf("%d%d",&n,&m),n)
{
for(i=0;i<n;i++)
{
scanf("%s",grid[i]);
}
count=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(grid[i][j]=='@')
{
DFS(i,j);//从(i,j)位置进行DFS;
count++;
}
}
}
printf("%d\n",count);
}
return 0;
}