Avoid The Lakes
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 194 Accepted Submission(s) : 108
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.
* 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题意:有N行M列田地,并且有K块田地里面有水坑。接下来输入的是每块水坑的坐标。每相连的小水坑可以构成一个大水坑。不算斜对角的水坑,只有上下左右。 求构成最大水坑有多少个小水坑构成。AC代码#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define max 100 using namespace std; int a[max+10][max+10]; int s,t,m,n; void dfs(int x,int y) { if(x<1||x>m||y<1||y>n||a[x][y]==0) return ; t++; a[x][y]=0; dfs(x-1,y); dfs(x+1,y); dfs(x,y-1); dfs(x,y+1); } int main() { int k,i,j,p; //memste(a,0,sizeof(a)); while(scanf("%d%d%d",&m,&n,&k)!=EOF) { int q,w; //int s=0; memset(a,0,sizeof(a)); for(i=0;i<k;i++) { scanf("%d %d",&q,&w); a[q][w]=1; } s=0; for(j=1;j<=m;j++) { for(p=1;p<=n;p++) { if(a[j][p]==1) { t=0; dfs(j,p); s=s>t?s:t; } } } printf("%d\n",s); } return 0; }