POJ - 3620 Avoid The Lakes

来源:互联网 发布:南京高新沿江网络问政 编辑:程序博客网 时间:2024/06/06 00:28
Avoid The Lakes
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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: NM, 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 53 22 23 12 31 1

Sample Output

4

英语伤不起的可要注意啦!我试着翻译翻译闭嘴John的农场被一场暴风雨淹了,更糟糕的是,他养的奶牛非常怕水。。。无关的就跳过吧!
农场里有一块N行M列的网格地,这N*M个小网格有K个被水淹了。而保险公司的赔偿金是由这些被淹的网格中连在一起的最大值决定的.所以你的任务就是求出被淹的网格中最多有多少个是连在一起的。用dfs解题,用一个二维数组记录相应的被水淹的网格,在用一辅助数组,对农场遍历一遍,记录下相连的最大值就OK了!
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int n,m,k,ans,sum;int map[110][110];//模拟农场 bool vis[110][110];//辅助数组 int px[4]={-1,0,1,0}; int py[4]={0,1,0,-1};void dfs(int a,int b){for(int i=0;i<4;i++){int x=a+px[i];int y=b+py[i];if(map[x][y]&&!vis[x][y]){vis[x][y]=true;ans++;//当前情况下网格连在一起的个数 dfs(x,y); }}if(sum<ans)sum=ans;//记录各种情况的最大值 }int main(){while(~scanf("%d%d%d",&n,&m,&k)){memset(vis,false,sizeof(vis));memset(map,0,sizeof(map));//全部初始化为0 sum=0;for(int i=1;i<=k;i++){//i从1开始,省去后面遍历时判断越界的麻烦 int p,q;//局部变量 scanf("%d %d",&p,&q);map[p][q]=1;//被水淹的标记为1 }for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(map[i][j]){ans=0;//每一组都要初始化!!在这WA了n遍。。 dfs(i,j);}        printf("%d\n",sum);}return 0;}

0 0
原创粉丝点击