版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38013346/article/details/82262865
Description
Input
Output
总的的排水量
Solution
水往低处流。
于是需要优先最低的,即水位最深的。
Codes
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 550;
struct Node{
int x,y;
ll val;
Node(){}
Node(int _x,int _y,ll _val) {x = _x;y = _y;val = _val;}
bool operator < (const Node &a)const {
return val < a.val;
}
};
priority_queue<Node> qu;
int n,m,maze[maxn][maxn];
bool vis[maxn][maxn];
int main()
{
while(~scanf("%d%d",&n,&m)) {
while(!qu.empty()) qu.pop();
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
scanf("%d",&maze[i][j]);
if(maze[i][j] >= 0) {
maze[i][j] = 0;
}
else {
maze[i][j] = -maze[i][j];
}
}
}
int x,y;
scanf("%d%d",&x,&y);x--;y--;
qu.push(Node(x,y,maze[x][y]));
vis[x][y] = true;
ll res = 0;
while(!qu.empty()) {
Node now = qu.top();qu.pop();
res += now.val;
for(int i=-1;i<=1;i++) for(int j=-1;j<=1;j++) {
int mx = now.x + i,my = now.y + j;
if(mx<0 || mx >= n || my<0 || my >= m || vis[mx][my] || maze[mx][my]==0) continue;
vis[mx][my] = 1;
// maze[mx][my] = min((ll)maze[mx][my],now.val);
qu.push(Node(mx,my,min(now.val,(ll)maze[mx][my])));
}
}
// for(int i=0;i<n;i++) {
// for(int j=0;j<m;j++) if(maze[i][j] && vis[i][j]) res += maze[i][j];
// }
printf("%lld\n",res);
}
return 0;
}