题目: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. <br>
思路:典型的深度搜索 可能搜索中最简单的 不过对我来说 有点难 c++ 忘得有点多 深搜也是开始自己第一写!!
代码:#include <string.h>
#include <stdio.h>
int a[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,-1,1,0,1,1};
char tu[101][101];
void suan(int p,int q)
{
tu[p][q]='*';
for(int i=0;i<8;i++)
if(tu[p+a[i][0]][q+a[i][1]]=='@')
suan(p+a[i],q+a[i][1]);
}
int main()
{
int m,n,i,j;
int count;
while(scanf("%d %d",&m,&n)&&m!=0)
{
mement(tu,0,sizeof(tu));
for( i=0;i<m;i++)
scanf("%s",tu[i]);
count=0;
for( i=0;i<m;i++)
for( j=0;j<n;j++)
{
if(tu[i][j]=='@')
{
// tu[i][j]='*';
count++;
suan(i,j);
}
}
printf("%d\n",count);
}
return 0;
}