#include <iostream>
#include <vector>
using namespace std;
void rotateClockwise(vector<vector<int>>& matrix) {
int n = matrix.size();
vector<int> elements; // pentru a stoca elementele din afara diagonalelor
// Colectăm elementele care sunt în afara diagonalelor
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && i + j != n - 1) { // verificăm dacă nu sunt pe diagonala principală sau secundară
elements.push_back(matrix[i][j]);
}
}
}
// Rotim elementele în sensul acelor de ceasornic
int k = 0; // index pentru elementele rotite
for (int j = n - 1; j > 0; --j) { // pentru fiecare coloană de sus în jos
for (int i = 0; i < n; ++i) {
if (i != j && i + j != n - 1) { // verificăm din nou diagonalele
matrix[i][j] = elements[k++];
}
}
}
// Reintroducem elementele pe pozițiile corecte
k = 0;
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
if (i != j && i + j != n - 1) { // verificăm din nou diagonalele
matrix[i][j] = elements[k++];
}
}
}
}
int main() {
// Exemplu de matrice 4x4
vector<vector<int>> matrix = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 }
};
// Afișăm matricea originală
cout << "Matricea originală:" << endl;
for (const auto& row : matrix) {
for (const auto& elem : row) {
cout << elem << " ";
}
cout << endl;
}
rotateClockwise(matrix);
// Afișăm matricea după rotație
cout << "Matricea după rotație:" << endl;
for (const auto& row : matrix) {
for (const auto& elem : row) {
cout << elem << " ";
}
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgcm90YXRlQ2xvY2t3aXNlKHZlY3Rvcjx2ZWN0b3I8aW50Pj4mIG1hdHJpeCkgewogICAgaW50IG4gPSBtYXRyaXguc2l6ZSgpOwogICAgdmVjdG9yPGludD4gZWxlbWVudHM7IC8vIHBlbnRydSBhIHN0b2NhIGVsZW1lbnRlbGUgZGluIGFmYXJhIGRpYWdvbmFsZWxvcgoKICAgIC8vIENvbGVjdMSDbSBlbGVtZW50ZWxlIGNhcmUgc3VudCDDrm4gYWZhcmEgZGlhZ29uYWxlbG9yCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgKytqKSB7CiAgICAgICAgICAgIGlmIChpICE9IGogJiYgaSArIGogIT0gbiAtIDEpIHsgLy8gdmVyaWZpY8SDbSBkYWPEgyBudSBzdW50IHBlIGRpYWdvbmFsYSBwcmluY2lwYWzEgyBzYXUgc2VjdW5kYXLEgwogICAgICAgICAgICAgICAgZWxlbWVudHMucHVzaF9iYWNrKG1hdHJpeFtpXVtqXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8gUm90aW0gZWxlbWVudGVsZSDDrm4gc2Vuc3VsIGFjZWxvciBkZSBjZWFzb3JuaWMKICAgIGludCBrID0gMDsgLy8gaW5kZXggcGVudHJ1IGVsZW1lbnRlbGUgcm90aXRlCiAgICBmb3IgKGludCBqID0gbiAtIDE7IGogPiAwOyAtLWopIHsgLy8gcGVudHJ1IGZpZWNhcmUgY29sb2FuxIMgZGUgc3VzIMOubiBqb3MKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgICAgICBpZiAoaSAhPSBqICYmIGkgKyBqICE9IG4gLSAxKSB7IC8vIHZlcmlmaWPEg20gZGluIG5vdSBkaWFnb25hbGVsZQogICAgICAgICAgICAgICAgbWF0cml4W2ldW2pdID0gZWxlbWVudHNbaysrXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICAvLyBSZWludHJvZHVjZW0gZWxlbWVudGVsZSBwZSBwb3ppyJtpaWxlIGNvcmVjdGUKICAgIGsgPSAwOwogICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyArK2opIHsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgICAgICBpZiAoaSAhPSBqICYmIGkgKyBqICE9IG4gLSAxKSB7IC8vIHZlcmlmaWPEg20gZGluIG5vdSBkaWFnb25hbGVsZQogICAgICAgICAgICAgICAgbWF0cml4W2ldW2pdID0gZWxlbWVudHNbaysrXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4oKSB7CiAgICAvLyBFeGVtcGx1IGRlIG1hdHJpY2UgNHg0CiAgICB2ZWN0b3I8dmVjdG9yPGludD4+IG1hdHJpeCA9IHsKICAgICAgICB7IDEsIDIsIDMsIDQgfSwKICAgICAgICB7IDUsIDYsIDcsIDggfSwKICAgICAgICB7IDksIDEwLCAxMSwgMTIgfSwKICAgICAgICB7IDEzLCAxNCwgMTUsIDE2IH0KICAgIH07CgogICAgLy8gQWZpyJnEg20gbWF0cmljZWEgb3JpZ2luYWzEgwogICAgY291dCA8PCAiTWF0cmljZWEgb3JpZ2luYWzEgzoiIDw8IGVuZGw7CiAgICBmb3IgKGNvbnN0IGF1dG8mIHJvdyA6IG1hdHJpeCkgewogICAgICAgIGZvciAoY29uc3QgYXV0byYgZWxlbSA6IHJvdykgewogICAgICAgICAgICBjb3V0IDw8IGVsZW0gPDwgIiAiOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGVuZGw7CiAgICB9CgogICAgcm90YXRlQ2xvY2t3aXNlKG1hdHJpeCk7CgogICAgLy8gQWZpyJnEg20gbWF0cmljZWEgZHVwxIMgcm90YcibaWUKICAgIGNvdXQgPDwgIk1hdHJpY2VhIGR1cMSDIHJvdGHIm2llOiIgPDwgZW5kbDsKICAgIGZvciAoY29uc3QgYXV0byYgcm93IDogbWF0cml4KSB7CiAgICAgICAgZm9yIChjb25zdCBhdXRvJiBlbGVtIDogcm93KSB7CiAgICAgICAgICAgIGNvdXQgPDwgZWxlbSA8PCAiICI7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=