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
题意:简单的求@的连通块的个数。然后相邻的算8个方向的,上下左右+4个斜角。
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN=100+7;
const int inf=1e9;
int n,m;
int sx,sy;
char tu[MAXN][MAXN];
int vis[MAXN][MAXN];
int dx[8]= {0,0,1,-1,-1,-1,1,1};
int dy[8]= {1,-1,0,0,-1,1,-1,1};
void dfs(int x,int y,int cnt)
{
vis[x][y]=cnt;
for(int i = 0; i < 8; ++i)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && tu[nx][ny] == '@')
{
dfs(nx, ny, cnt);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m)&&n)
{
memset(vis,0,sizeof(vis));
for(int i = 0 ; i < n ; ++i)
{
scanf("%s",tu[i]);
}
int cnt=0;
for(int i = 0 ; i < n ; ++i)
for(int j = 0; j < m ; ++j)
{
if(tu[i][j] == '@' && !vis[i][j])dfs(i,j,++cnt);
}
printf("%d\n",cnt);
}
return 0;
}