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?
Given a grid of integers giving the altitudes of the island, how high must the ocean rise before the land splits into pieces?
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.
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
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;}
阅读全文
0 0
- hdu 3329 The Flood
- HDU 3329 The Flood
- HDU 4127Flood-it!
- HDU 4127 Flood-it!
- USACO The Castle(flood fill)
- hdu 4127 Flood-it! 搜索
- hdu Flood-it!(IDA*算法)
- flood
- Flood
- poj 4007 \ hdu 4127 Flood-it!
- hdu 4127 Flood-it! (IDA+bfs)
- ID(dfs+bfs)-hdu-4127-Flood-it!
- HDU 4127 Flood-it! (IDA*+BFS)
- hdu-4127 Flood-it!(IDA*算法)
- HDU-2871 Flood-it!(IDA*)
- HDU 4127 Flood-it!(IDA*)
- HDU 4127 Flood-it!(IDA*搜索)
- Disabling the flood of kernel messages in Debian console
- markdown中需要转义的字符
- 我的学习说明
- webpack 完整配置文件 Webpack2完整踩坑教程(三)
- Linux学习—vim文本编辑器学习
- MySQL EXPLAIN详解
- HDU 3329 The Flood
- 逗号分割的集合互转
- CURL知识
- Docker Dockerfile详解
- C语言—指针—汉字倒置
- Java解读-ThreadLocal详解与应用
- NAT技术和代理服务器
- 文章标题
- mysql和eclipse都设置了utf8编码,保存时中文依然乱码