We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int rows,cols,rot;
scanf("%d%d%d",&rows,&cols,&rot);
int arr[rows][cols];
int result[rows][cols];
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
scanf("%d", &arr[r][c]);
}
}
rows--;
cols--;
for (int r = 0; r <= rows; r++) {
for (int c = 0; c <= cols; c++) {
int x = r < rows - r ? r : rows - r;
int y = c < cols - c ? c : cols - c;
int min = x < y ? x : y;
int maxRow = rows - min;
int maxCol = cols - min;
int parameter = (maxRow + maxCol) * 2 - min * 4;
int row = r;
int col = c;
for (int a = 0; a < rot%parameter; a++) {
if (col == min && row < maxRow) {
row++;
}
else if (row == maxRow && col < maxCol) {
col++;
}
else if (row > min && col == maxCol) {
row--;
}
else if (row == min && col > min) {
col--;
}
}
result[row][col] = arr[r][c];
}
}
for (int r = 0; r <= rows; r++) {
for (int c = 0; c <= cols; c++) {
printf("%d ", result[r][c]);
}
printf("\n");
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Matrix Layer Rotation
You are viewing a single comment's thread. Return to all comments →
C Solution: