DFS遍历整个连通的区域

DFS在搜索的过程中,可以搜索一大片的

连通的区域。

题目:http://acm.swust.edu.cn/oj/problem/1/

View Code
#include<iostream>
#include<string.h>
using namespace std;
int direction[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
int used[1005][85];
char map[1005][85];
int W, H, i, j, count, Max, tx, ty;
void dfs(int i, int j)
{
int k;
count++;
used[i][j] = 1;
for(k=0; k<4; k++)
{
tx = i + direction[k][0]; ty = j + direction[k][1];
if(tx>=0 && tx<H && ty>=0 && ty<W && used[tx][ty]==0 && map[tx][ty]=='*')
dfs(tx, ty);
}
}
int main()
{
while(cin>>W>>H)
{
memset(used, 0, sizeof(used));
Max = 0;
for(i=0; i<H; i++)
for(j=0; j<W; j++)
cin>>map[i][j];
for(i=0; i<H; i++)
for(j=0; j<W; j++)
{
if(map[i][j]=='*' && used[i][j]==0)
{
count = 0;
dfs(i, j);
if(count>Max)
Max = count;
}
}
cout<<Max<<endl;
}
return 0;
}



转载于:https://www.cnblogs.com/o8le/archive/2011/12/01/2270368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值