B: Mirror Images

https://open.kattis.com/problems/mirror

We need to reflect a matrix twice: once left-to-right and once top-to-bottom.

To reflect left-to-right, M[i][j] becomes M[i][c-1-j].

To reflect top-to-bottom, M[i][j] becomes M[r-1-i][j].

So, we want to translate all elements in the input matrix M[i][j] to M[r-1-i][c-1-j].

We can do this while reading input:

vector<vector<char>> M(r, vector<char>(c));
for (int i = 0; i < r; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < c; j++) {
        M[r-1-i][c-1-j] = s.at(j);
    }
}

Last updated