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; |
| } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步