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.
Input
Line 1: Three space-separated integers: N, M, and K
Lines 2…K+1: Line i+1 describes one submerged location with two space separated integers that are its row and column: R and C
Output
Line 1: The number of cells that the largest lake contains.
Sample
Input Output
3 4 5
3 2
2 2
3 1
2 3
1 1
4
翻译:
Farmer John 的农场在最近的一场风暴中被淹,这一事实因他的奶牛极度怕水的消息而更加严重。然而,他的保险公司只会根据他农场上最大的“湖”的大小来偿还他。
农场表示为具有N (1 ≤ N ≤ 100) 行和M (1 ≤ M ≤ 100) 列的矩形网格。网格中的每个单元要么是干的,要么是淹没的,并且恰好K (1 ≤ K ≤ N × M) 的细胞被淹没。正如人们所期望的那样,一个湖有一个中央单元,其他单元通过共享一个长边(而不是一个角落)连接到该单元。任何与中心单元共享长边或与任何连接单元共享长边的单元成为连接单元,并且是湖的一部分。
输入
第 1 行:三个空格分隔的整数:N、M和K
第 2 行… K +1:第i +1 行描述了一个水下位置,其中有两个空格分隔的整数,即行和列:R和C
输出
第 1 行:最大湖泊包含的单元数。
样例
输入;
3 4 5
3 2
2 2
3 1
2 3
1 1
输出;
4
#include<iostream>
#include<cstring>
using namespace std;int g[110][110];
int dx[4]= {
1,0,-1,0},dy[4]= {
0,-1,0,1};
int n,m,k,c;void dfs(int x,int y)
{
g[x][y]=0;//没找到一个湖泊就定义为0,防止重复计算for(int i=0; i<4; i++){
int nx=x+dx[i],ny=y+dy[i];if(g[nx][ny]==1){
c++;//找到一个数量加1dfs(nx,ny);//进行下一轮查找}}return ;
}int main()
{
memset(g,0,sizeof g);//初始化都为0int a,b,max=-10//定义最大值;cin>>n>>m>>k;for(int i=0; i<k; i++){
cin>>a>>b;g[a][b]=1;}for(int i=1; i<=n; i++)for(int j=1; j<=m; j++){
if(g[i][j]==1){
c=1;dfs(i,j);if(c>max)max=c;}}cout<<max;return 0;
}