标签:
可以理解为判断连通块的个数~~DFS解决
#include"cstdio" #include"iostream" #include"cstring" #define MAXN 105 using namespace std; int mat[MAXN][MAXN],vis[MAXN][MAXN]; int dfs(int x,int y) { int ans=0; if(!mat[x][y]||vis[x][y]) return 0; ans++;vis[x][y]=1; ans+=dfs(x+1,y);ans+=dfs(x-1,y);ans+=dfs(x,y+1);ans+=dfs(x,y-1); return ans; } int main() { int n,m,k; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { memset(mat,0,sizeof(mat)); memset(vis,0,sizeof(vis)); for(int i=0;i<k;i++) { int a,b; scanf("%d%d",&a,&b); mat[a][b]=1; } int ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans=max(dfs(i,j),ans); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/luxiaoming/p/4670922.html