Avoid The Lakes
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 154 Accepted Submission(s) : 80
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 andM (1 ≤ M ≤ 100) columns. Each cell in the grid is either dry or submerged, and exactlyK (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.
* 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
* Line 1: The number of cells that the largest lake contains.
3 4 5 3 2 2 2 3 1 2 3 1 1
4 和水池数目题目差不多,但是这个是求最大水池数是多少 ac代码:#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define MAXN 110 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; using namespace std; int n,m,k,num; int map[MAXN][MAXN]; int check(int xx,int yy) { if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]==1) return 1; return 0; } void dfs(int x,int y) { if(check(x,y)) return; map[x][y]=1; num++; for(int i=0;i<4;i++) { int nx=x+dir[i][0]; int ny=y+dir[i][1]; dfs(nx,ny); } } int main() { int i,j,a,b; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { int max=0; for(i=0;i<n;i++)//刚开始用memset赋值,怎么都不对= =! for(j=0;j<m;j++) map[i][j]=1; for(i=0;i<k;i++) { scanf("%d%d",&a,&b); map[a-1][b-1]=0; } for(i=0;i<n;i++) { for(j=0;j<m;j++) { num=0; if(map[i][j]==0) { dfs(i,j); } if(num>max)//比较取最大 max=num; } } printf("%d\n",max); } return 0; }