Skip to content

Commit d9c9822

Browse files
committed
[20260223] BOJ / G4 / 배열 돌리기 4 / 김민진
1 parent bcf65e5 commit d9c9822

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class BJ_17406_배열_돌리기_4 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static StringTokenizer st;
10+
11+
private static int N, M, K, ans;
12+
private static int[] order;
13+
private static int[][] matrix, ops;
14+
private static boolean[] visited;
15+
16+
public static void main(String[] args) throws IOException {
17+
init();
18+
sol();
19+
bw.write(ans + "\n");
20+
bw.flush();
21+
bw.close();
22+
}
23+
24+
private static void init() throws IOException {
25+
st = new StringTokenizer(br.readLine());
26+
N = Integer.parseInt(st.nextToken());
27+
M = Integer.parseInt(st.nextToken());
28+
K = Integer.parseInt(st.nextToken());
29+
ans = Integer.MAX_VALUE;
30+
31+
matrix = new int[N][M];
32+
for (int i = 0; i < N; i++) {
33+
st = new StringTokenizer(br.readLine());
34+
for (int j = 0; j < M; j++) {
35+
matrix[i][j] = Integer.parseInt(st.nextToken());
36+
}
37+
}
38+
39+
ops = new int[K][3];
40+
for (int i = 0; i < K; i++) {
41+
st = new StringTokenizer(br.readLine());
42+
ops[i][0] = Integer.parseInt(st.nextToken()) - 1;
43+
ops[i][1] = Integer.parseInt(st.nextToken()) - 1;
44+
ops[i][2] = Integer.parseInt(st.nextToken());
45+
}
46+
47+
order = new int[K];
48+
visited = new boolean[K];
49+
}
50+
51+
private static void sol() throws IOException {
52+
perm(0);
53+
}
54+
55+
private static void perm(int depth) {
56+
if (depth == K) {
57+
int[][] tmp = new int[N][M];
58+
for (int i = 0; i < N; i++) {
59+
System.arraycopy(matrix[i], 0, tmp[i], 0, M);
60+
}
61+
62+
// order 순서대로 회전
63+
for (int i = 0; i < K; i++) {
64+
int idx = order[i];
65+
rotate(tmp, ops[idx][0], ops[idx][1], ops[idx][2]);
66+
}
67+
68+
int minRowSum = Integer.MAX_VALUE;
69+
for (int i = 0; i < N; i++) {
70+
int sum = 0;
71+
for (int j = 0; j < M; j++) {
72+
sum += tmp[i][j];
73+
}
74+
minRowSum = Math.min(minRowSum, sum);
75+
}
76+
ans = Math.min(ans, minRowSum);
77+
return;
78+
}
79+
80+
for (int i = 0; i < K; i++) {
81+
if (!visited[i]) {
82+
visited[i] = true;
83+
order[depth] = i;
84+
perm(depth + 1);
85+
visited[i] = false;
86+
}
87+
}
88+
}
89+
90+
private static void rotate(int[][] arr, int r, int c, int s) {
91+
for (int l = 1; l <= s; l++) {
92+
int top = r - l;
93+
int bottom = r + l;
94+
int left = c - l;
95+
int right = c + l;
96+
97+
int tmp = arr[top][left];
98+
99+
// 하 -> 상
100+
for (int i = top; i < bottom; i++) {
101+
arr[i][left] = arr[i + 1][left];
102+
}
103+
// 오 -> 왼
104+
for (int j = left; j < right; j++) {
105+
arr[bottom][j] = arr[bottom][j + 1];
106+
}
107+
// 상 -> 하
108+
for (int i = bottom; i > top; i--) {
109+
arr[i][right] = arr[i - 1][right];
110+
}
111+
// 왼 -> 오
112+
for (int j = right; j > left + 1; j--) {
113+
arr[top][j] = arr[top][j - 1];
114+
}
115+
arr[top][left + 1] = tmp;
116+
}
117+
}
118+
119+
}
120+
```

0 commit comments

Comments
 (0)