Avoid The Lakes
Description 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. Input * Line 1: Three space-separated integers: N, M, and K Output * Line 1: The number of cells that the largest lake contains. Sample Input 3 4 5 3 2 2 2 3 1 2 3 1 1 Sample Output 4 Source |
=====================================题目大意=====================================
给出有水的地域单元坐标,输出最大的水域大小。
=====================================算法分析=====================================
DFS水过。
=======================================代码=======================================
#include<stdio.h>
#include<string.h>
int N,M,K,MaxNum;
bool HashMap[105][105];
int DFS(int Row,int Col)
{
if(!(1<=Row&&Row<=N)) { return 0; }
if(!(1<=Col&&Col<=M)) { return 0; }
if(!HashMap[Row][Col]) { return 0; }
HashMap[Row][Col]=0;
int Cnt=1;
Cnt+=DFS(Row+1,Col);
Cnt+=DFS(Row-1,Col);
Cnt+=DFS(Row,Col+1);
Cnt+=DFS(Row,Col-1);
MaxNum=(MaxNum>Cnt?MaxNum:Cnt);
return Cnt;
}
void ReaDataAndSolve()
{
int row,col;
scanf("%d%d",&row,&col);
HashMap[row][col]=1;
if(--K) { ReaDataAndSolve(); }
DFS(row,col);
}
int main()
{
while(scanf("%d%d%d",&N,&M,&K)==3)
{
memset(HashMap,0,sizeof(HashMap));
MaxNum=-1;
ReaDataAndSolve();
printf("%d\n",MaxNum);
}
return 0;
}