题目:http://acm.hdu.edu.cn/showproblem.php?pid=1241
题意:
GeoSurvComp 地质探测公司负责探测地下油田。每次 GeoSurvComp 公司都是在一块长方 形的土地上来探测油田。在探测时,他们把这块土地用网格分成若干个小方块,然后逐个分析每 块土地,用探测设备探测地下是否有油田。方块土地底下有油田则称为 pocket,如果两个 pocket 相邻,则认为是同一块油田,油田可能覆盖多个 pocket。你的工作是计算长方形的土地上有多少 个不同的油田。
题解:
计算连通快的个数。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=100+10;
char g[MAX][MAX];
int n,m;
int judgein(int x,int y)
{
return x>=0&&x<n&&y>=0&&y<m;
}
void dfs(int x,int y)
{
g[x][y]='*';
for(int i=-1;i<=1;i++)
{
for(int j=-1;j<=1;j++)
{
if(i==0&&j==0) continue;
if(judgein(x+i,y+j)&&g[x+i][y+j]=='@')
{
g[x+i][y+j]='*';
dfs(x+i,y+j);
}
}
}
return;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
getchar();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%c",&g[i][j]);
}
getchar();
}
int cou=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(g[i][j]=='@')
{
dfs(i,j);
cou++;
}
}
}
printf("%d\n",cou);
}
return 0;
}