Problem Description:
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.
Input:
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.
Output:
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.
Sample Input:
1 1
*
3 5
**@@*
@
@@*
1 8
@@***@
5 5
***@
@@@
@**@
@@@@
@@**@
0 0
Sample Output:
0
1
2
2
题意:某公司要统计油藏的个数,网格中含油的情节被称为口袋,如果两个口袋是水平相邻、竖直相邻或对角线相邻的话就是同一个油藏的一部分,问:网格***有多少个不同的油藏?(网格中“”代表没有油,“@”代表有油)
思路:这道题要用深搜的方法,并且要对8个方向进行深搜。从有油的地方开始,然后对它的8个方向进行深搜,直至一个油藏所包含的油点全部深搜完,要记住每次深搜过的点都要标记。表示这些点已经都深搜过了。
My DaiMa:
#include<iostream>
#include<stdio.h>
using namespace std;
int n,m,ans;
char ch[102][102];
void dfs(int x,int y)//深搜
{
if(ch[x][y]=='*'||x<0||x>=n||y<0||y>=m) return;
ch[x][y]='*';//走过的油点要标记
dfs(x-1,y);//接下来就对8个方向进行深搜
dfs(x+1,y);
dfs(x,y-1);
dfs(x,y+1);
dfs(x-1,y-1);
dfs(x-1,y+1);
dfs(x+1,y-1);
dfs(x+1,y+1);
return;
}
int main()
{
while(~scanf("%d",&n)){
if(n==0) break;
ans=0;
scanf("%d",&m);
//getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
//scanf("%c",&ch[i][j]);
cin>>ch[i][j];
//getchar();
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(ch[i][j]=='@'){ //这就是从有油点的地方开始深搜
ans++;
dfs(i,j);
}
}
}
printf("%d\n",ans);
}
return 0;
}