Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 8, 2016 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/137807675ba27fd532ffaed4f98a68df to your computer and use it in GitHub Desktop.
Save jianminchen/137807675ba27fd532ffaed4f98a68df to your computer and use it in GitHub Desktop.
HackerRank Matrix Rotation - Google - study code, C Programming, Julia likes the idea inside two for loop, for any element in the matrix, determine the position after rotations.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
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;
}
@jianminchen
Copy link
Author

Line 31 - Line 45 - Julia likes the idea and implementation in C++.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment