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()) ...
随机推荐
- TextView使用Spannable设置复合文本
http://blog.csdn.net/linghu_java/article/details/32053167 Spannable 对文字的编辑减少TextView的拼接并且达到改变一串字符中的部 ...
- C#获取类中所有方法
var t = typeof(HomeController); //获取所有方法 System.Reflection.MethodInfo[] methods = t.GetMethods(); // ...
- oracle 中查看一张表是否有主键,主键在哪个字段上的语句怎么查如要查aa表,
select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.constra ...
- 强制关闭myeclipse出现的问题
重启时,可能会出现打不开关闭前所在的workspace.其他workspace可以正常打开. 今天遇到这个问题,以前就遇到过,但是忘记如何解决了.今天在我等了十多分钟后,神奇的myeclipse自己起 ...
- angularjs 遇到Error: [$injector:unpr] Unknown provider: tdpicnews-serviceProvider <- tdpicnews-service <- tdpic-controller 错误
define(['modules/tdpic-module', 'services/news-service', 'utilities/cryto'], function (app) { 'use s ...
- CoreBluetooth
Core Bluetooth的基本常识 每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的 一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征 ...
- ejs 基本语法
1.基本语法.<% code %> 无缓冲的条件语句元素.<%= code %> 转义HTML,该code并且会打印出来.<%- code %> ...
- C#中Predicate的一点理解
本人喜欢代码看起来比较优雅,而C#真的是一种很优雅的语言.比如我们New List<string> StrList; 如果我们想查找StrList,可以使用C#提供的 StrList.Fi ...
- Java学习----this和super(在继承中)
public class Base { /*public Base() { System.out.println("Base 类的初始构造方法"); }*/ public Base ...
- Bootstrap_网格系统
首先添加CSS样式: [class *= col-]{ background-color: #eee; border: 1px solid #ccc; } [class *= col-] [class ...