英语伤不起的可要注意啦!我试着翻译翻译: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;
}