当前位置: 首页 > 工具软件 > Punch & Kick > 使用案例 >

nyoj 547- Interesting Punch-Bowl(优先队列模拟)

羊煜
2023-12-01

题目描述:

 

Dr.Kong has taken a side job designing interesting punch-bowl designs. The designs are created as follows:

      * A flat board of size W cm *  H cm is procured        (3 <= W <= 300, 3 <= H <= 300)

      * On every 1 cm x 1 cm square of the board, a 1 cm x 1 cm block is placed.  This block has some integer height B     (1 <= B <= 1,000,000,000)

The blocks are all glued together carefully so that punch will not drain  through them. They are glued so well, in fact, that the corner blocks really don't matter!

Dr.Kong can never figure out, however, just how much punch their bowl designs will hold. Presuming the bowl is freestanding (i.e., no special walls around the bowl), calculate how much juice the bowl can hold.  Some juice bowls, of course, leak out all the juice on the edges and will hold 0.

 

输入描述:

* Line 1: Two space-separated integers, W and H * Lines 2..H+1: Line i+1 contains row i of bowl heights: W space-separated integers each of which represents the height B of a square in the bowl. The first integer is the height of column 1, the second integers is the height of column 2, and so on. There are several test cases and end with the end of the file.

输出描述:

* Line 1: A single integer that is the number of cc's the described bowl will hold.

样例输入:

复制

4 5 
5 8 7 7
5 2 1 5
7 1 7 1
8 9 6 9
9 8 9 9

样例输出:

12

提示:

没有提示哦

来源:

第五届河南省程序设计大赛

题意是给出一个H*W大小的图,每个数字代表当前位置的高度,问最多能盛多少果汁

思路:用优先队列将每个点压进去,每一个点取四周最小的值,更新该点的高度,

并记录增加的数目,从边界不断缩圈;

#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
typedef long long ll;
ll a[310][310];
bool vis[310][310];
int n, m;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
struct node{
	int x, y, v;
	bool operator < (const node & oth)const
	{
		return v > oth.v;   // 每一层都按从小到大的值排
	}
};
priority_queue<node>pq;
void bfs()
{
	ll ans = 0;
	while (!pq.empty())
	{
		int x = pq.top().x, y = pq.top().y, v = pq.top().v;
		pq.pop();
		if (vis[x][y])
			continue;
		vis[x][y] = 1;
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dir[i][0];
			int yy = y + dir[i][1];
			int vv = a[xx][yy];
			if (xx < n && xx > 1 && yy < m && yy > 1 && !vis[xx][yy])
			{
				if (vv < v) //若该点小于当前点,则可升高
				{
					ans += v - vv;
					a[xx][yy] = v; //更新信息
				}
				pq.push({ xx, yy, a[xx][yy] });
			}		
		}
	}
	cout << ans << endl;
}
int main(){
	while (cin >> m >> n)
	{
		memset(vis, 0, sizeof vis);
		while (!pq.empty())
			pq.pop();
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
			{
				cin >> a[i][j];
				if (i == 1 || j == 1 || i == n || j == m)
					pq.push({ i, j, a[i][j] }); //先压入边界的点
			}
		bfs();
		/*for (int i = 1; i <= n; i++, cout << endl)
			for (int j = 1; j <= m; j++)
				cout << a[i][j] << " ";*/
	}
	return 0;
}

 

 

 类似资料: