书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!

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;
}



posted on 2011-12-01 12:11  More study needed.  阅读(302)  评论(0编辑  收藏  举报

导航

书山有径勤为路>>>>>>>>

<<<<<<<<学海无涯苦作舟!