/**
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(优先队列)的更多相关文章

  1. I - Interesting Calculator (bfs使用优先队列求步数最小或者花费最小)

    题目链接:https://cn.vjudge.net/contest/245287#problem/I 代码: 使用普通的队列和优先队列相比,优先队列能更快地找到目的变量. #include<i ...

  2. D - Interesting Calculator 【数值型BFS+优先队列】

    There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...

  3. POJ2227(优先队列)

    The Wedding Juicer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3440   Accepted: 155 ...

  4. ZOJ 3865 Superbot(优先队列--模板)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...

  5. 中南大学oj:1336: Interesting Calculator(广搜经典题目)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1336 There is an interesting calculator. It has 3 r ...

  6. CSU-1336: Interesting Calculator,最短路思想!

    1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...

  7. 堆排序与优先队列——算法导论(7)

    1. 预备知识 (1) 基本概念     如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...

  8. 数据结构:优先队列 基于list实现(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: Minion-Xu #list实现优先队列 class ListPriQueueValueE ...

  9. python优先队列,队列和栈

    打印列表的疑问 class Node: def __str__(self): return "haha" print([Node(),Node()]) print(Node()) ...

随机推荐

  1. maven提示错误的解决办法

    import或者new一个的maven project的时候,提示如下错误 Description    Resource    Path    Location    TypeCannot read ...

  2. sql根据'/'截取最后的字符串

    filpath字段值:/DataFile/UpLoad/Logo/NoPhoto.jpg select filpath,REVERSE((SUBSTRING(REVERSE(FilPath),0,CH ...

  3. move file create directory.

    If we want to move file to the directory that does not exist,and if we perform a File.Move,it will r ...

  4. Android布局管理器(贞布局)

    贞布局有FrameLayout所代表,它直接继承了ViewGroup组建 贞布局为每个加入其中的组件创建一个空白区域(一帧),所以每个子组件占用一帧,这些贞都会根据gravity属性执行自动对齐 贞布 ...

  5. OSX安装nginx和rtmp模块(rtmp直播服务器搭建)

    1.安装Homebrew,执行命令 1 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  6. jQuery慢慢啃之CSS(六)

    1.css(name|pro|[,val|fn])//访问匹配元素的样式属性 $("p").css("color");//获取 $("p") ...

  7. linux svn启动和关闭(转)

    1,启动SVN sudo svnserve -d -r /home/data/svn/ 其中 -d 表示守护进程, -r 表示在后台执行 /home/data/svn/  为svn的安装目录 2,关闭 ...

  8. maven 常用插件总结

    maven-javadoc-plugin (1) 说明:该插件生成项目的javadoc.对于构建jar目标,javadoc会首先生成并打包放入jar文件中. (2) 默认用法: pom.xml配置 & ...

  9. &nbsp; 与 空格的区别

    nbsp 是 Non-Breaking SPace的缩写,即“不被折断的空格”,当两个单词使用   连接时,这两个单词就不会被分隔为2行,如下面 <div id="div1" ...

  10. js事件处理 —— 详解

    对于JS事件处理分为四部分: 1.html事件处理程序 直接添加到HTML结构中 解析:用html处理程序可以直接在button元素里直接调用,但是维护性不是很方便 <!DOCTYPE html ...