[FloodFill] BZOJ 2936 [Poi1999]降 水 Water
来源:互联网 发布:php app 编辑:程序博客网 时间:2024/06/06 07:12
详见:http://blog.csdn.net/clover_hxy/article/details/52956172
久闻Floodfill算法
对于矩形最外围的点必然是存不住水的,那我们可以从边界开始扩展。考虑什么样的地方能够积水,一定是他周围的格子都比他高,而最大的存水量,一定是根据他周围最低的格子确定的。
所以我们每次从队列中选取积水量最小的点,用它积水的高度更新它周围的高度,因为每次都是从小的开始更新,所以格子第一次被更新,一定是被他周围最低的格子更新,那么这一定是这个格子的积水高度。所以我们可以保证每个点只遍历一次,时间复杂度O(n*m*log(nm)).
#include<cstdio>#include<cstdlib>#include<algorithm>#include<queue> using namespace std;const int N=105;int n,m; int vst[N][N],a[N][N],h[N][N];const int dx[]={0,1,0,-1};const int dy[]={1,0,-1,0};struct abcd{ int x,y,h; abcd(int x,int y,int h):x(x),y(y),h(h) { } bool operator < (const abcd &B) const { return h>B.h; } };priority_queue<abcd> Q;int main(){ freopen("t.in","r",stdin); freopen("t.out","w",stdout); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){ scanf("%d",&a[i][j]); h[i][j]=a[i][j]; if (i==1||j==1||i==n||j==m)vst[i][j]=1,Q.push(abcd(i,j,h[i][j])); } while (!Q.empty()){ abcd u=Q.top(); Q.pop(); for (int k=0;k<4;k++){ int x=u.x+dx[k],y=u.y+dy[k]; if (x<0 || x>n || y<0 || y>m || vst[x][y]) continue; h[x][y]=max(h[x][y],u.h); vst[x][y]=1; Q.push(abcd(x,y,h[x][y])); } } int Ans=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) Ans+=h[i][j]-a[i][j]; printf("%d\n",Ans); }
0 0
- [FloodFill] BZOJ 2936 [Poi1999]降 水 Water
- bzoj 2936: [Poi1999]降 水 (floodfill算法)
- 【bzoj 2936】[Poi1999]降 水(floodfill算法(BFS))
- BZOJ 2936 [Poi1999]降 水 (FloodFill)优先队列+BFS
- 2936: [Poi1999]降 水
- 【POI1999】【BZOJ2936】降 水
- [BZOJ2936][POI1999] 降水(堆+bfs:floodfill)
- BZOJ 2933([Poi1999]地图-区间Dp)
- 【博弈】【bzoj 2927】: [Poi1999]多边形之战
- BZOJ 2926 Poi1999 空立方体问题
- BZOJ 2927 POI1999 多边形之战 博弈论
- BZOJ 2929:[Poi1999]洞穴攀行
- [高精度 递归] BZOJ 2930 [Poi1999]梦游者 Sleepwalker
- [几何 分治] BZOJ 2928 [Poi1999]飞弹 Rockets
- BZOJ 2927: [Poi1999]多边形之战
- bzoj 2927: [Poi1999]多边形之战 博弈论
- floodfill
- floodfill
- 第七章 事务、过程、触发器
- Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable
- Android Studio常用快捷键
- 7.5 Swift枚举关联值
- Eclipse中配置 JNI开发的自动提示功能
- [FloodFill] BZOJ 2936 [Poi1999]降 水 Water
- 7.6 Swift中switch提取枚举关联值
- 第八章—进度对话框ProgressDialog、日期选择器DatePickerDialog、时间选择器TimePickerDialog
- 网络编程(39)—— 使用信号量semaphore进行多线程同步
- java中mian方法中为什么要添加参数
- IO学习之使用字符流读取数据
- (一)JDBC连接数据库
- Oracle 使用MERGE INTO 语句更新数据
- 关于使用MyEclipse启动web项目时工具报 The specified JRE installation does not exist 错误提示解决方法