HDU_3329
We can enumerate the height of the rising water in two parts, then perform a deep search on the peripheral layer, mark all the drowned points, and then perform a deep search on the unmarked points, if two or more pieces of land exist after the deep search, the current depth of water will meet the requirements, otherwise it will not meet the requirements, then, change the values of max and min.
#include<string.h>
#include<stdio.h>
int a[110][110],vis[110][110];
int n,m,max,min,mid;
int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
void dfs1(int x,int y)
{
int i,newx,newy;
for(i=0;i<4;i++)
{
newx=x+dx[i];
newy=y+dy[i];
if(newx>=0&&newx<=n+1&&newy>=0&&newy<=m+1
&&a[newx][newy]<=mid&&!vis[newx][newy])
{
vis[newx][newy]=1;
dfs1(newx,newy);
}
}
}
void dfs2(int x,int y)
{
int i,newx,newy;
for(i=0;i<4;i++)
{
newx=x+dx[i];
newy=y+dy[i];
if(newx>=1&&newx<=n&&newy>=1&&newy<=m
&&!vis[newx][newy])
{
vis[newx][newy]=1;
dfs2(newx,newy);
}
}
}
int main()
{
int i,j,k,t,flag;
t=0;
while(1)
{
scanf("%d%d",&n,&m);
if(!n)
break;
memset(a,0,sizeof(a));
max=0;
min=1000000000;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]>max)
max=a[i][j];
if(i==1||i==n||j==1||j==m)
if(a[i][j]<min)
min=a[i][j];
}
max++;
flag=max;
while(1)
{
mid=(min+max)/2;
memset(vis,0,sizeof(vis));
dfs1(0,0);
k=0;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(!vis[i][j])
{
vis[i][j]=1;
k++;
dfs2(i,j);
}
if(k>1)
max=mid;
if(mid==min)
break;
if(k<=1)
min=mid;
}
printf("Case %d: ",++t);
if(max==flag)
printf("Island never splits.\n");
else
printf("Island splits when ocean rises %d feet.\n",max);
}
return 0;
}