-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
MatrixLayerRotation.java
100 lines (97 loc) · 2.56 KB
/
MatrixLayerRotation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static void matrixRotation(int[][] a, int r) {
int n = a.length;
int m = a[0].length;
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
int min = Math.min(n, m);
// for(int i=0;i<(min+1)/2;i++){
// list.add(new ArrayList<Integer>());
// }
for(int k=0;k<(min+1)/2;k++){
ArrayList<Integer> l = new ArrayList<>();
int i = k, j = k;
while(j<m-k){
l.add(a[i][j]);
j++;
}
j--;
i++;
while(i<n-k){
l.add(a[i][j]);
i++;
}
i--;
j--;
while(j>=k){
l.add(a[i][j]);
j--;
}
j++;
i--;
while(i>k){
l.add(a[i][j]);
i--;
}
// System.out.println(l);
list.add(l);
}
for(int k=0;k<(min+1)/2;k++){
ArrayList<Integer> l = list.get(k);
// System.out.println(l);
int len = l.size();
int count = 0;
int i = k, j = k;
while(j<m-k){
a[i][j] = l.get((count+r)%len);
count++;
j++;
}
j--;
i++;
while(i<n-k){
a[i][j] = l.get((count+r)%len);
count++;
i++;
}
i--;
j--;
while(j>=k){
a[i][j] = l.get((count+r)%len);
count++;
j--;
}
j++;
i--;
while(i>k){
a[i][j] = l.get((count+r)%len);
count++;
i--;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
int r = in.nextInt();
int[][] matrix = new int[m][n];
for(int matrix_i = 0; matrix_i < m; matrix_i++){
for(int matrix_j = 0; matrix_j < n; matrix_j++){
matrix[matrix_i][matrix_j] = in.nextInt();
}
}
matrixRotation(matrix, r);
in.close();
}
}