HDU 3329 The Flood

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.