【转】http://blog.csdn.net/zxy_snow/article/details/5947541
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <memory.h>
using namespace std;
int map[110][110],visit[110][110];
int m,n,num,mmax,fi,fj,cou;
int dir[8] = {0,1,0,-1,1,0,-1,0};
int find()
{
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
if( !visit[i][j] && map[i][j] )
{
fi = i;
fj = j;
return 1;
}
return 0;
}
void DFS(int i,int j)
{
for(int k=0; k<8; k+=2)
{
int a = i + dir[k];
int b = j + dir[k+1];
if( a>=1 && a<=m && b>=1 && b<=n && !visit[a][b] && map[a][b] )
{
visit[a][b] = 1;
DFS(a,b);
cou++;
}
}
}
int main(void)
{
int x,y;
while( cin >> m >> n >> num )
{
mmax = 0; cou = 0;
memset(map,0,sizeof(map));
memset(visit,0,sizeof(visit));
for(int i=1; i<=num; i++)
{
cin >> x >> y;
map[x][y] = 1;
}
while( find() )
{
visit[fi][fj] = 1;
cou = 1;
DFS(fi,fj);
if( cou > mmax )
mmax = cou;
}
cout << mmax << endl;
}
return 0;
}