|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int n, m, r; |
| 8 | + static int[][] arr; |
| 9 | + |
| 10 | + public static void main(String[] args) throws IOException { |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 13 | + |
| 14 | + n = Integer.parseInt(st.nextToken()); |
| 15 | + m = Integer.parseInt(st.nextToken()); |
| 16 | + r = Integer.parseInt(st.nextToken()); |
| 17 | + |
| 18 | + arr = new int[n][m]; |
| 19 | + |
| 20 | + for (int i = 0; i < n; i++) { |
| 21 | + st = new StringTokenizer(br.readLine()); |
| 22 | + for (int j = 0; j < m; j++) { |
| 23 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + rotate(); |
| 28 | + |
| 29 | + for (int i = 0; i < n; i++) { |
| 30 | + for (int j = 0; j < m; j++) { |
| 31 | + System.out.print(arr[i][j]+" "); |
| 32 | + } |
| 33 | + System.out.println(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static void rotate() { |
| 38 | + int layers = Math.min(n, m) / 2; |
| 39 | + |
| 40 | + for (int layer = 0; layer < layers; layer++) { |
| 41 | + int top = layer; |
| 42 | + int left = layer; |
| 43 | + int bottom = n - 1 - layer; |
| 44 | + int right = m - 1 - layer; |
| 45 | + |
| 46 | + for (int i = 0; i < r; i++) { |
| 47 | + int temp = arr[top][left]; |
| 48 | + |
| 49 | + for (int j = left; j < right; j++) |
| 50 | + arr[top][j] = arr[top][j + 1]; |
| 51 | + |
| 52 | + for (int j = top; j < bottom; j++) |
| 53 | + arr[j][right] = arr[j + 1][right]; |
| 54 | + |
| 55 | + for (int j = right; j > left; j--) |
| 56 | + arr[bottom][j] = arr[bottom][j - 1]; |
| 57 | + |
| 58 | + for (int j = bottom; j > top + 1; j--) |
| 59 | + arr[j][left] = arr[j - 1][left]; |
| 60 | + |
| 61 | + arr[top + 1][left] = temp; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
0 commit comments