题目描述
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.
输入
The input file contains one or more grids. Each grid 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 <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or
@’, representing an oil pocket.
输出
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
样例输入
1 1
*
3 5
@@*
@
@@*
1 8
@@***@
5 5
****@
@@@
@**@
@@@@
@@**@
0 0
样例输出
0
1
2
2
分析:
简单的深搜问题, 不过我却卡的要死了。因为在样例输入的时候。给出的样例5 5后面多了一个空格,造成了我不知如何处理的尴尬场面。后来我通过用% s输入成功解决。但是切记不可将整形数n’,m用% s输入,否则过不了。
#include"stdio.h"
int n,m;
char a[101][101];
int x[8]={0,0,1,-1,-1,-1,1,1};
int y[8]={1,-1,0,0,1,-1,1,-1};
void dfs(int i,int j)
{ int k;
if(i<0||j<0||i>=n||j>=m||a[i][j]=='*')
return 0;
if(a[i][j]=='@')
a[i][j]='*';
for(k=0;k<8;k++)
dfs(i+x[k],j+y[k]);
}
int main()
{ char p;
int i,j,rel;
char t[5];
while(~scanf("%d%d",&n,&m))
{scanf("%c",&p);
if(n==0&m==0)
break;
rel=0;
// scanf("%c",&p);
//printf("n=%d,m=%d\n",n,m);
for(i=0;i<n;i++)//用%s输入
{
scanf("%s",a[i]);
}
scanf("%c",&p);
/* printf("a[0][0]=%c\n",a[0][0]);
printf("a[0][1]=%c\n",a[0][1]);
printf("a[0][2]=%c\n",a[0][2]);
printf("a[0][3]=%c\n",a[0][3]);
printf("a[0][4]=%c\n",a[0][4]);
printf("a[1][4]=%c\n",a[1][4]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%c",a[i][j]);
printf("\n");
}*/
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
if(a[i][j]=='@')
{
dfs(i,j);
rel++;
}
}
printf("%d\n",rel);
}
}