문제
문제접근
문제 이해
코드
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N, M, R;
cin >> N >> M >> R;
vector<vector<int>> a(N, vector<int>(M));
for (int y = 0; y < N; ++y)
for (int x = 0; x < M; ++x)
cin >> a[y][x];
int ng = min(N, M) / 2;
vector<vector<int>> groups;
for (int g = 0; g < ng; ++g) {
vector<int> group;
int xmax = M - g - 1, ymax = N - g - 1;
for (int x = g; x < xmax; ++x) group.push_back(a[g][x]);
for (int y = g; y < ymax; ++y) group.push_back(a[y][xmax]);
for (int x = xmax; x > g; --x) group.push_back(a[ymax][x]);
for (int y = ymax; y > g; --y) group.push_back(a[y][g]);
groups.push_back(group);
}
for (int g = 0; g < ng; ++g) {
vector<int> &group = groups[g];
int len = group.size(), idx = R % len;
int xmax = M - g - 1, ymax = N - g - 1;
for (int x = g; x < xmax; ++x, idx = (idx + 1) % len) a[g][x] = group[idx];
for (int y = g; y < ymax; ++y, idx = (idx + 1) % len) a[y][xmax] = group[idx];
for (int x = xmax; x > g; --x, idx = (idx + 1) % len) a[ymax][x] = group[idx];
for (int y = ymax; y > g; --y, idx = (idx + 1) % len) a[y][g] = group[idx];
}
for (int y = 0; y < N; ++y) {
for (int x = 0; x < M; ++x)
cout << a[y][x] << ' ';
cout << '\n';
}
}
결과