您的位置:首页 > 其它

poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

2015-05-20 21:00 435 查看
http://poj.org/problem?id=2386
http://acm.hdu.edu.cn/showproblem.php?pid=1241
求有多少个连通子图。复杂度都是O(n*m)。

#include <cstdio>

char filed[110][110];
int n,m;
void dfs(int x,int y)
{
for(int i=-1;i<=1;i++)
for(int j=-1;j<=1;j++)   //循环遍历8个方向
{
int xx=x+i,yy=y+j;
if(xx>=0&&xx<n&&yy>=0&&yy<m&&filed[xx][yy]=='W')
{
filed[xx][yy]='.';
dfs(xx,yy);
}
}
return;
}

int main()
{
//freopen("a.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
getchar();
int count=0;
for(int i=0;i<n;i++) scanf("%s",filed[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(filed[i][j]=='W')
{
dfs(i,j);
count++;
}
printf("%d\n",count);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: