water

暑假大集训模拟赛12 T2

算法分析

  • 看起来很像个一眼题 直接枚举周围四个点 然后求最小差值就好了
  • 但是我们多手玩几组样例就会发现这个是不对的 因为有可能会出现一个大坑里面套了一个小坑的情况 类似 10 5 2 5 10这样
  • 而看数据范围300 那就搜呗
  • 当前这个点肯定是要向四周几个点搜 而真正限制这个点水面高度的其实是最矮的一个方块 (木桶效应)
  • 因此我们可以用一个堆来维护低的点 因为它一定是点的短板 然后进行bfs 去找出每一个符合条件 可以更新的块

Code

#include<bits/stdc++.h>
using namespace std;
const int maxn = 305;
int h[maxn][maxn];
int w[maxn][maxn];
int vis[maxn][maxn];
int n,m;
//w为水平面高度 h为实际高度
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

struct node{
	int x,y,v;//位置与实际高度
	node(){};
	node(int a,int b,int c){
		x = a,y = b,v = c;
	}
	bool operator < (const node &A)const{
		return v > A.v;
	}
};

priority_queue<node> q;
queue<node> Q;

void bfs(node s){
	Q.push(s);
	while(!Q.empty()){
		node u = Q.front();Q.pop();
		if(w[u.x][u.y] != -1)continue;
		w[u.x][u.y] = s.v;
		for(int k = 0;k < 4;++k){
			int tx = u.x + dx[k];
			int ty = u.y + dy[k];
			if(tx < 1 || tx > n || ty < 1 || ty > m)continue;
			if(w[tx][ty] != -1)continue;
			if(h[tx][ty] <= s.v)Q.push(node(tx,ty,0));
			else if(!vis[tx][ty])q.push(node(tx,ty,h[tx][ty])),vis[tx][ty] = 1;
		}
	}
}


int main(){
	scanf("%d%d",&n,&m);
	for(int i = 1;i <= n;++i){
		for(int j =1;j <= m;++j){
			scanf("%d",&h[i][j]);
			w[i][j] = -1;
			if(i == 1 || j == 1 || i == n || j == m)q.push(node(i,j,h[i][j] < 0 ? 0 : h[i][j])),vis[i][j] = 1;//如果到边界就入队 从i边界开始搜起
		}
	}
	while(!q.empty()){
		node now = q.top();
		q.pop();
		if(w[now.x][now.y] != -1)continue;//减枝
		bfs(now);
	}
	for(int i = 1;i <= n;++i){
		for(int j = 1;j <= m;++j)
			printf("%d ",w[i][j] - h[i][j]);
		printf("\n");
	}
	return 0;
}
posted @ 2020-08-02 10:48  HISKrrr  阅读(113)  评论(0编辑  收藏  举报