HDU 3329 The Flood

来源:互联网 发布:淘宝店铺如何提高排名 编辑:程序博客网 时间:2024/06/05 19:40

Global warming has us all thinking of rising oceans — well, maybe only those of us who live near the ocean. The small island nation of Gonnasinka has employed you to answer some questions for them. In particular they want to know how high the water has to get before their island becomes two islands (or more). 

Given a grid of integers giving the altitudes of the island, how high must the ocean rise before the land splits into pieces?
Input
Each test case begins with a line containing two positive integers n,m giving the dimensions of the igrid, then n lines each containing mpositive integers. The integers indicate the original altitude of the grid elements. Grid elements are considered to be adjacent only if they share a horizontal or vertical edge. Values of zero (0) along the perimeter, and all zero cells connected to these, are ocean at its initial level. Cells of 0 not connected to the perimeter (that is, surrounded by higher land) are simply sea level elevations. Furthermore, assume the ocean initially surrounds the given grid. The island is initially connected. Neither n nor m will exceed 100 and heights will never exceed 1000. A line with 0 0 follows the last test case.
Output
For each test case output one of the two following lines. 
Case n: Island splits when ocean rises f feet.
or 
Case n: Island never splits.
Our convention here is if your answer is, say, 5 feet, you more accurately mean “5 feet plus a little more.” That is, at least a little water will be flowing over the originally 5 foot high portion of land.
Sample Input
5 53 4 3 0 03 5 5 4 32 5 4 4 31 3 0 0 01 2 1 0 05 55 5 5 5 74 1 1 1 44 1 2 1 37 1 0 0 47 3 4 4 40 0
Sample Output
Case 1: Island never splits.Case 2: Island splits when ocean rises 3 feet.


DFS先对海洋进行标记,在对陆地之一进行标记,判断是否还有没被标记的陆地,若有则输出答案。


#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int mapp[101][101];int n,m;int vis[101][101];int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};int total;void dfs(int r,int c,int num){    int x,y;    vis[r][c]=1;    total--;    for(int i=0;i<4;i++)    {        x=r+dir[i][0];        y=c+dir[i][1];        if(x>=0&&x<n&&y>=0&&y<m&&mapp[x][y]<=num&&vis[x][y]==0)        {            dfs(x,y,num);        }    }}void dfs_num(int r,int c){    int x,y;    vis[r][c]=1;    total--;    for(int i=0;i<4;i++)    {        x=r+dir[i][0];        y=c+dir[i][1];        if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==0)        {            dfs_num(x,y);        }    }}int border(int*u,int*v,int l){    int i,j;    for(i=0;i<n;i++)    {        if(!vis[i][0]&&l>=mapp[i][0])        {            *u=i;            *v=0;            return 1;        }        if(!vis[i][m-1]&&l>=mapp[i][m-1])        {            *u=i;            *v=m-1;            return 1;        }    }    for(j=0;j<m;j++)    {        if(!vis[0][j]&&l>=mapp[0][j])        {            *u=0;            *v=j;            return 1;        }        if(!vis[n-1][j]&&l>=mapp[n-1][j])        {            *u=n-1;            *v=j;            return 1;        }    }    return 0;}int trans(int i){    memset(vis,0,sizeof(vis));    total=n*m;    int flag;    //将能联通海洋的全设为1    for(int j=0;j<n;j++)    {        if(!vis[j][0]&&i>=mapp[j][0])        {            dfs(j,0,i);        }        if(!vis[j][m-1]&&i>=mapp[j][m-1])        {            dfs(j,m-1,i);        }    }    for(int j=0;j<m;j++)    {        if(!vis[0][j]&&i>=mapp[0][j])        {            dfs(0,j,i);        }        if(!vis[n-1][j]&&i>=mapp[n-1][j])        {            dfs(n-1,j,i);        }    }//剩下的陆地用进行染色    flag=0;    for(int j=0;j<n;j++)    {        for(int k=0;k<m;k++)        {            if(vis[j][k]==0)            {                //printf("%d %d",j,k);                dfs_num(j,k);                flag=1;                break;            }        }        if(flag==1)break;    }    //判断染色后还有无没有染色的土地,若有则可以分为几个陆地    if(total)return 1;    return 0;}int main(){    int maxmapp;    int cnt=0;    while(~scanf("%d%d",&n,&m)&&n&&m)    {        cnt++;        maxmapp=0;        for(int i=0;i<n;i++)        {            for(int j=0;j<m;j++)            {                scanf("%d",&mapp[i][j]);                if(maxmapp<mapp[i][j])                    maxmapp=mapp[i][j];            }        }        int ans=0;        for(int i=0;i<maxmapp;i++)        {            if(trans(i))            {                printf("Case %d: Island splits when ocean rises %d feet.\n",cnt,i);                ans=1;                break;            }        }        if(!ans)        {            printf("Case %d: Island never splits.\n",cnt);        }    }    return 0;}


原创粉丝点击