1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=305,Dirx[]= {0,1,-1,0,0},Diry[]= {0,0,0,1,-1}; int n,m,a[maxn][maxn],dist[maxn][maxn],vst[maxn][maxn]; struct HeapNode { int x,y,d; bool operator < (const HeapNode& b) const { return d>b.d; } }; void Bfs() { memset(dist,0x3f,sizeof(dist)); priority_queue<HeapNode>Q; for(int i=1; i<=n; i++) { dist[i][0]=dist[i][m+1]=0; Q.push((HeapNode) {i,0,0}); Q.push((HeapNode) {i,m+1,0}); } for(int i=1; i<=m; i++) { dist[0][i]=dist[n+1][i]=0; Q.push((HeapNode) {0,i,0}); Q.push((HeapNode) {n+1,i,0}); } while(!Q.empty()) { HeapNode Now=Q.top(); Q.pop(); if(vst[Now.x][Now.y])continue; vst[Now.x][Now.y]=1; for(int k=1; k<=4; k++) { HeapNode Next= {Now.x+Dirx[k],Now.y+Diry[k],max(Now.d,a[Next.x][Next.y])}; if(vst[Next.x][Next.y]||Next.x<1||Next.x>n||Next.y<1||Next.y>m)continue; if(dist[Next.x][Next.y]>Next.d) { dist[Next.x][Next.y]=Next.d; Q.push(Next); } } } } int main() { n=Get_Int(); m=Get_Int(); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) a[i][j]=Get_Int(); Bfs(); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++)printf("%d ",dist[i][j]-a[i][j]); putchar('\n'); } return 0; }
|