Garden's Code

Garden's Code

#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int maxn=1e3;
const int dx[5]={1,-1,0,0};
const int dy[5]={0,0,1,-1};
struct Node{
	int x,y,w;
	Node(int ix,int iy,int iw){x=ix,y=iy,w=iw;}
	Node(){}
	bool operator<(const Node &T)const{return w>T.w;}
};
int n,m;
int a[maxn+5][maxn+5];
int b[maxn+5][maxn+5];
priority_queue<Node> qu;
signed main(){
	memset(b,0x3f,sizeof(b));
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			scanf("%lld",&a[i][j]);
			if(1<i&&i<n&&1<j&&j<m) continue;
			qu.push(Node(i,j,a[i][j]));
			b[i][j]=a[i][j];
		}
	}
	Node T;
	int x,y,w;
	while(!qu.empty()){
		T=qu.top(),qu.pop();
		if(T.w>b[T.x][T.y]) continue;
		for(int i=0;i<4;i++){
			x=T.x+dx[i],y=T.y+dy[i];
			w=max(T.w,a[x][y]);
			if(x<1||y<1||x>n||y>m) continue;
			if(b[x][y]>w){
				b[x][y]=w;
				qu.push(Node(x,y,w));
			}
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			ans+=b[i][j]-a[i][j];
	printf("%lld",ans);
	return 0;
}
posted @ 2024-05-07 22:11  DeepSeaSpray  阅读(0)  评论(0编辑  收藏  举报