Interesting Punch-Bowl(优先队列)
/**
http://acm.nyist.net/JudgeOnline/problem.php?pid=547
题意:
有一个正方形的区域 区域上面有高度不同的1*1的立方体自然有凸有凹
凹的地方可以储水
问一共可以储存多少水
分析:
每次都从最低的边界向内部注水(仅与边界相邻的内部)
如果能够注水 就把注水后的柱子当作新的边界
也就是把边界放到一个优先队列中 每次都让高度最小的边界出队(优先队列)
*/
include
include
include
using namespace std;
int n,m,sum=0;
int bowl[350][350];
int vis[350][350];
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
struct node
{
int x;
int y;
int h;
friend bool operator <(node a,node b)
{
return a.h>b.h;
}
};
priority_queueq;
void chuShiHua()///首先把边界放入队列中
{
memset(vis,0,sizeof(vis));
node b;
for(int i=1; i<=m; i++)
{
b.x=i;
b.y=1;
b.h=bowl[i][1];
vis[i][1]=1;
q.push(b);
b.x=i;
b.y=n;
b.h=bowl[i][n];
vis[i][n]=1;
q.push(b);
}
for(int i=2; i<=n-1; i++)
{
b.x=1;
b.y=i;
b.h=bowl[1][i];
vis[1][i]=1;
q.push(b);
b.x=m;
b.y=i;
b.h=bowl[m][i];
vis[m][i]=1;
q.push(b);
}
}
void bfs()
{
chuShiHua();
while(!q.empty())
{
node a,b;
a=q.top();
q.pop();
for(int i=0; i<4; i++)
{
int x=a.x+dir[i][0];
int y=a.y+dir[i][1];
if(x<1||x>m||y<1||y>n)
continue;
if(vis[x][y]==0)
{
if(bowl[x][y]<a.h)///可以向内部注水
{
sum+=a.h-bowl[x][y];
b.x=x;
b.y=y;
b.h=a.h;
vis[x][y]=1;
q.push(b);
}
else///不可以
{
b.x=x;
b.y=y;
b.h=bowl[x][y];
vis[x][y]=1;
q.push(b);
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(bowl,0,sizeof(bowl));
for(int i=1; i<=m; i++)
for(int j=1; j<=n; j++)
scanf("%d",&bowl[i][j]);
sum=0;
bfs();
printf("%d\n",sum);
}
return 0;
}
Interesting Punch-Bowl(优先队列)的更多相关文章
- I - Interesting Calculator (bfs使用优先队列求步数最小或者花费最小)
题目链接:https://cn.vjudge.net/contest/245287#problem/I 代码: 使用普通的队列和优先队列相比,优先队列能更快地找到目的变量. #include<i ...
- D - Interesting Calculator 【数值型BFS+优先队列】
There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...
- POJ2227(优先队列)
The Wedding Juicer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3440 Accepted: 155 ...
- ZOJ 3865 Superbot(优先队列--模板)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...
- 中南大学oj:1336: Interesting Calculator(广搜经典题目)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...
- CSU-1336: Interesting Calculator,最短路思想!
1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
- 数据结构:优先队列 基于list实现(python版)
#!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...
- python优先队列,队列和栈
打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...
随机推荐
- Wpf 鼠标拖动元素实例
1.Wpf中鼠标捕获和释放 //以矩形为例 //创建鼠标捕获 Mouse.Capture(rectOne); //释放鼠标捕获 rectOne.ReleaseMouseCapture(); 2.Wpf ...
- Win10安卓模拟器Visual Studio Emulator for Android使用简介(转)
Visual Studio Emulator for Android是微软官方发布的独立版本的安卓模拟器,这款软件可以让安卓应用开发者更加轻松的用Visual Studio编写Android应用,据说 ...
- oracle约束条件状态
Oracle完整性约束有一下4种: • DISABLE NOVALIDATE • ENABLE NOVALIDATE • DISABLE VALIDATE • ENABLE VALIDATE • ...
- Orace数据库锁表的处理与总结<摘抄与总结三>
当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...
- hibernate加载实体映射文件 及映射文件auto-import
第一种方法: 在hibernate.cfg.xml中<mapping resource="包名/Xxx.hbm.xml"/>包名为路径形式( x/x/x这种形式) 第二 ...
- Linux(Debian)下Maven的安装
Maven的下载地址:http://maven.apache.org/download.cgi这里以最新的3.3.9版本为例进行安装,在这之前需要确保机器上已经安装了JDK. -- 在home文件夹中 ...
- ASP.NET页面跳转的三种方法比较
在ASP.NET下,经常需要在页面之间跳转,下面我们来分别介绍一下关于.NET中Response.Redirect(),Sever.Execute(),Server.Transfer() 三种页面跳转 ...
- 使用Raphael 画图(三) 事件 (javascript)
这章展示事件例子. 下图是官方API的事件: 例子: var butt1 = paper.set(); var a1 = paper.circle(24.833, 26.917, 26.667).at ...
- vsftpd.conf 联机手册
vsftpd.conf - vsftpd 的配置文件 描述vsftpd.conf 可以用于控制 vsftpd, 以实现各种各样的功能. vsftpd 缺省到 /etc/vsftpd.conf 处查找此 ...
- 常用jQuery选择器总结【转】
在Dom 编程中我们只能使用有限的函数根据id 或者TagName 获取Dom 对象. 然而在jQuery 中则完全不同,jQuery 提供了异常强大的选择器用来帮助我们获取页面上的对象, 并且将对象 ...