Avoid The Lakes
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7023 | Accepted: 3735 |
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.
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
3 4 5 3 2 2 2 3 1 2 3 1 1
Sample Output
4 这个题大意是,给出一串坐标,上下左右连着的算一个,求最大的一个里面有几个元素!主要运用深搜,搜索一次,标记这个点在其周围找到符合的点就进行递归,这样一块都会被标记,再计算其个数!取最大的个数
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int N,K,M,a,b,cot,best; 6 int map[101][101]; 7 int mov[4][2]={0,1,0,-1,1,0,-1,0}; 8 bool can(int x,int y)/判断能否符合条件 9 { 10 if(x<0||x>=N||y<0||y>=K||!map[x][y]) 11 return false; 12 return true; 13 } 14 void dfs(int x,int y)//深搜 15 { 16 int xx,yy,i; 17 map[x][y]=0;//标记走过 18 for(i=0;i<4;i++) 19 { 20 xx=x+mov[i][0]; 21 yy=y+mov[i][1]; 22 if(can(xx,yy)) 23 { 24 cot++; 25 dfs(xx,yy); 26 } 27 } 28 } 29 int main() 30 { 31 int i,j; 32 while(scanf("%d%d%d",&N,&K,&M)!=EOF) 33 { 34 memset(map,0,sizeof(map)); 35 for(i=0;i<M;i++) 36 { 37 scanf("%d%d",&a,&b); 38 map[a-1][b-1]=1; 39 } 40 int sum=0; 41 best=0; 42 for(i=0;i<N;i++) 43 for(j=0;j<K;j++) 44 { 45 cot=1; 46 if(map[i][j]) 47 { 48 dfs(i,j); 49 } 50 best=best>cot?best:cot;//更新最优解 51 } 52 printf("%d\n",best); 53 } 54 return 0; 55 }