-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
61 lines (49 loc) Β· 1.86 KB
/
Main.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
package Simulation.P16927;
import java.io.*;
import java.util.StringTokenizer;
public class Main {
static int N, M, R;
static int[][] origin, result;
static int[] di = {1, 0, -1, 0};
static int[] dj = {0, 1, 0, -1};
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/P16927/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
origin = new int[N][M];
result = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
origin[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int s = 0; s < Math.min(N, M)/2; s++) {
int n = N - s*2, m = M - s*2;
int cnt = (n + m) * 2 - 4;
int i = s, j = s, dir = 0, r = R % cnt;
int[] pos = new int[cnt];
for (int c = 0; c < cnt + r; c++) {
if (c < cnt) pos[c] = origin[i][j];
if (c >= r) result[i][j] = pos[c-r];
if (!canGo(i + di[dir % 4], j + dj[dir % 4], s, n, m)) dir++;
i += di[dir % 4];
j += dj[dir % 4];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
sb.append(result[i][j]).append(" ");
}
sb.append("\n");
}
System.out.print(sb.toString());
}
static boolean canGo(int i, int j, int s, int n, int m) {
return s <= i && i < s+n && s <= j && j < s+m;
}
}