Problem Y
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 5
Farmer John's farm was flooded in the most recent storm, a fact only aggravated by the information that his cows are deathly afraid of water. His insurance agency will only repay him, however, an amount depending on the size of the largest "lake" on his farm.
The farm is represented as a rectangular grid with N (1 ≤ N ≤ 100) rows and M (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactly K (1 ≤ K ≤ N × M) of the cells are submerged. As one would expect, a lake has a central cell to which other cells connect by sharing a long edge (not a corner). Any cell that shares a long edge with the central cell or shares a long edge with any connected cell becomes a connected cell and is part of the lake.
题意:求出边相连的最大联通块
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int map1[101][101],sum,maxn=0;
void dfs(int i,int j)
{
if(map1[i][j]==0) return;
else
{
map1[i][j]=0;
sum++;
dfs(i-1,j);
dfs(i+1,j);
dfs(i,j+1);
dfs(i,j-1);
}
}
int main()
{
memset(map1,0,sizeof(map1));
int n,m,k,x,y;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=k;i++)
{
scanf("%d%d",&x,&y);
map1[x][y]=1;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
sum=0;
dfs(i,j); //以一个点为起点深搜,在比较求出最大值即可
maxn=max(maxn,sum);
}
printf("%d",maxn);
return 0;
}