Fold Matrix T34357


Statement
 

pdf   zip   main.cc

Let MM be a square matrix 2N×2N2N \times 2N. 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 A,B,C,DA,B,C,D, the folding causes the top part (A,BA,B) to overlap the bottom part (C,DC,D), and the second folding causes the right part (B,DB,D) 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 2N×2N2N \times 2N to a matrix N×NN \times N 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 11 overlap, those that have the number 22 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: mm a matrix 2N×2N2N \times 2N of integers.

POST: rr is a matrix N×NN \times N in which in each position it has the sum of the elements of mm superimposed by the fold of mm.

Observation

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.

Input

A matrix 2N×2N2N \times 2N.

Output

A N×NN \times N matrix where at each position it has the sum of the elements of mm superimposed by the fold of mm.

Sample session
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
Information
Author
PRO1
Language
English
Translator
Original language
Catalan
Other languages
Catalan Spanish
Official solutions
C++
User solutions
C++