Let be a square matrix . The folding of a matrix consists of folding the matrix of integers first along the horizontal line that divides the matrix, and then along the vertical line.
For example, let the following matrix have four quadrants , the folding causes the top part () to overlap the bottom part (), and the second folding causes the right part () to overlap the left part (A,C).
============= ============= ===========
| | | | | | | |
| A | B | ==> | A,C | B,D | ==> | A,B,C,D |
| | | | | | | |
plego --> ------------- ------------- -----------
| | | ^
| C | D | |
| | | |
============= plego
Notice that in this case, the matrix goes from being to a matrix in which each position has 4 numbers from the original matrix overlapped.
The following example shows which positions overlap with others according to the number. This means that all four positions that have the number overlap, those that have the number also, etc.
<---- 2N ------>
################ ^
# 1 2 | 2 1 # |
# 3 4 | 4 3 # |
#--------------# 2N
# 3 4 | 4 3 # |
# 1 2 | 2 1 # |
################ v
You have to implement the action
void plegaMatriu(const Matriu& m, Matriu& r) with
the following specification:
PRE:
a matrix
of integers.
POST:
is a matrix
in which in each position it has the sum of the
elements of
superimposed by the fold of
.
IMPORTANT: You only need to submit the requested
function, and possibly other necessary actions and functions. However,
you must keep the type definitions and #includes.
A matrix .
A matrix where at each position it has the sum of the elements of superimposed by the fold of .
ENTRADA 1: 4 1 2 2 1 3 4 4 3 3 4 4 3 1 2 2 1 SORTIDA 1: 4 8 12 16 ENTRADA 2: 8 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 SORTIDA 2: 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 ENTRADA 3: 2 1 3 2 4 SORTIDA 3: 10