|
| 1 | +package Simulation.P16927; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int N, M, R; |
| 9 | + static int[][] origin, result; |
| 10 | + |
| 11 | + static int[] di = {1, 0, -1, 0}; |
| 12 | + static int[] dj = {0, 1, 0, -1}; |
| 13 | + |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + System.setIn(new FileInputStream("src/Simulation/P16927/input.txt")); |
| 16 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 17 | + StringBuilder sb = new StringBuilder(); |
| 18 | + |
| 19 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 20 | + N = Integer.parseInt(st.nextToken()); |
| 21 | + M = Integer.parseInt(st.nextToken()); |
| 22 | + R = Integer.parseInt(st.nextToken()); |
| 23 | + |
| 24 | + origin = new int[N][M]; |
| 25 | + result = new int[N][M]; |
| 26 | + |
| 27 | + for (int i = 0; i < N; i++) { |
| 28 | + st = new StringTokenizer(br.readLine()); |
| 29 | + for (int j = 0; j < M; j++) { |
| 30 | + origin[i][j] = Integer.parseInt(st.nextToken()); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + for (int s = 0; s < Math.min(N, M)/2; s++) { |
| 35 | + int n = N - s*2, m = M - s*2; |
| 36 | + int cnt = (n + m) * 2 - 4; |
| 37 | + int i = s, j = s, dir = 0, r = R % cnt; |
| 38 | + int[] pos = new int[cnt]; |
| 39 | + for (int c = 0; c < cnt + r; c++) { |
| 40 | + if (c < cnt) pos[c] = origin[i][j]; |
| 41 | + if (c >= r) result[i][j] = pos[c-r]; |
| 42 | + if (!canGo(i + di[dir % 4], j + dj[dir % 4], s, n, m)) dir++; |
| 43 | + i += di[dir % 4]; |
| 44 | + j += dj[dir % 4]; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + for (int i = 0; i < N; i++) { |
| 49 | + for (int j = 0; j < M; j++) { |
| 50 | + sb.append(result[i][j]).append(" "); |
| 51 | + } |
| 52 | + sb.append("\n"); |
| 53 | + } |
| 54 | + |
| 55 | + System.out.print(sb.toString()); |
| 56 | + } |
| 57 | + |
| 58 | + static boolean canGo(int i, int j, int s, int n, int m) { |
| 59 | + return s <= i && i < s+n && s <= j && j < s+m; |
| 60 | + } |
| 61 | +} |
0 commit comments