#P11816. Three-Dimensional Chessboard Shift
Three-Dimensional Chessboard Shift
Three-Dimensional Chessboard Shift
Given a three-dimensional chessboard of size \(A \times B \times C\), each cell is represented by a triplet \((i,j,k)\) for \(1 \le i \le A\), \(1 \le j \le B\), and \(1 \le k \le C\). Initially, cell \((i,j,k)\) contains \(a_{i,j,k}\) pieces. In each move, you can select a cell that has at least one piece and move one piece to one of the three adjacent cells: \((i+1,j,k)\), \((i,j+1,k)\), or \((i,j,k+1)\), as long as the destination is within the board boundaries.
The goal is to achieve a target configuration where cell \((i,j,k)\) contains \(b_{i,j,k}\) pieces. Note that every move increases one or more of the indices, so pieces can only flow in non-decreasing order of coordinates.
A necessary and sufficient condition for the transformation to be possible is that for every cell \((i,j,k)\), the cumulative sum of pieces from \((1,1,1)\) to \((i,j,k)\) in the initial configuration is at least as much as that in the target configuration:
[ \sum_{p=1}^{i}\sum_{q=1}^{j}\sum_{r=1}^{k} a_{p,q,r} \ge \sum_{p=1}^{i}\sum_{q=1}^{j}\sum_{r=1}^{k} b_{p,q,r} \quad \text{for all } 1 \le i \le A,, 1 \le j \le B,, 1 \le k \le C, ]
and the total number of pieces must be the same:
[ \sum_{i=1}^{A}\sum_{j=1}^{B}\sum_{k=1}^{C} a_{i,j,k} = \sum_{i=1}^{A}\sum_{j=1}^{B}\sum_{k=1}^{C} b_{i,j,k}. ]
Determine if it is possible to transform the initial configuration into the target configuration using the allowed moves.
inputFormat
The first line contains three integers \(A\), \(B\), and \(C\).
The next \(A \times B\) lines describe the initial configuration: for each \(i=1\) to \(A\), there are \(B\) lines where each line contains \(C\) integers \(a_{i,j,k}\) representing the number of pieces in row \(j\) and column \(k\) of that layer.
After that, the following \(A \times B\) lines describe the target configuration in the same format (each line contains \(C\) integers \(b_{i,j,k}\)).
All numbers in the input are separated by spaces.
outputFormat
Output a single line containing "Yes" if it is possible to reach the target configuration using the allowed moves; otherwise, output "No".
sample
1 1 1
1
1
Yes