Problem Statement:
There is a grid with N rows and N columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell contains an integer. Initially, cell (i,j) contains the integer A[i][j].
You can repeatedly perform the following operation:
Choose integers i and j such that 1<=i,j<=N and an integer x, and add x to A[i][j]. The cost of this operation is |x|.
You are given a positive integer d. Your goal is to satisfy the following condition:
The difference between the integers written in any two cells that are vertically or horizontally adjacent is at least d. More formally, the following two conditions are satisfied:
|A[i][j]-A[i+1][j]|>=d for all integers i and j such that 1<=i<=N-1 and 1<=j<=N.
|A[i][j]-A[i][j+1]|>=d for all integers i and j such that 1<=i<=N and 1<=j<=N-1.
Achieve this goal with a total cost of 1/2*dN^2 or less.
Constraints:
2<=N<=500
1<=d<=1000
-1000≤A[i][j]<=1000
Input:
N d
A[1][1] A[1][2] …A[1][N]
…
A[N][1] A[N][2] …A[N][N]
Output:
A[1][1] A[1][2] …A[1][N]
…
A[N][1] A[N][2] …A[N][N]
Sample Input 1:
3 5
-2 1 3
3 -4 -4
0 1 3
Sample Output 1:
-2 8 3
3 -9 -4
-2 8 3
Sample Input 2:
5 2
1 5 5 0 3
2 0 2 5 1
5 2 0 5 5
3 7 2 0 1
6 0 4 3 6
Sample Output 2:
0 4 6 1 3
3 1 3 6 1
5 3 0 3 5
2 6 3 1 3
4 0 5 3 6
帮我用C++做一下,谢谢!