Skip to content

Commit 0f3ce27

Browse files
authored
Merge pull request #1819 from AlgorithmWithGod/JHLEE325
[20260123] BOJ / G4 / 배열 돌리기 4 / 이준희
2 parents 2a94b5c + 51af268 commit 0f3ce27

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
6+
public class Main {
7+
static int N, M, K;
8+
static int[][] originalBoard;
9+
static Operation[] ops;
10+
static boolean[] visited;
11+
static int[] p;
12+
static int minVal = Integer.MAX_VALUE;
13+
14+
static class Operation {
15+
int r, c, s;
16+
Operation(int r, int c, int s) {
17+
this.r = r; this.c = c; this.s = s;
18+
}
19+
}
20+
21+
public static void main(String[] args) throws IOException {
22+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
23+
StringTokenizer st = new StringTokenizer(br.readLine());
24+
25+
N = Integer.parseInt(st.nextToken());
26+
M = Integer.parseInt(st.nextToken());
27+
K = Integer.parseInt(st.nextToken());
28+
29+
originalBoard = new int[N + 1][M + 1];
30+
for (int i = 1; i <= N; i++) {
31+
st = new StringTokenizer(br.readLine());
32+
for (int j = 1; j <= M; j++) {
33+
originalBoard[i][j] = Integer.parseInt(st.nextToken());
34+
}
35+
}
36+
37+
ops = new Operation[K];
38+
for (int i = 0; i < K; i++) {
39+
st = new StringTokenizer(br.readLine());
40+
ops[i] = new Operation(
41+
Integer.parseInt(st.nextToken()),
42+
Integer.parseInt(st.nextToken()),
43+
Integer.parseInt(st.nextToken())
44+
);
45+
}
46+
47+
visited = new boolean[K];
48+
p = new int[K];
49+
permutation(0);
50+
51+
System.out.println(minVal);
52+
}
53+
54+
static void permutation(int cnt) {
55+
if (cnt == K) {
56+
int[][] copyBoard = copy();
57+
for (int i = 0; i < K; i++) {
58+
rotate(copyBoard, ops[p[i]]);
59+
}
60+
calculateMin(copyBoard);
61+
return;
62+
}
63+
for (int i = 0; i < K; i++) {
64+
if (visited[i]) continue;
65+
visited[i] = true;
66+
p[cnt] = i;
67+
permutation(cnt + 1);
68+
visited[i] = false;
69+
}
70+
}
71+
72+
static void rotate(int[][] board, Operation op) {
73+
int r = op.r;
74+
int c = op.c;
75+
int s = op.s;
76+
77+
for (int i = 1; i <= s; i++) {
78+
int top = r - i;
79+
int left = c - i;
80+
int bottom = r + i;
81+
int right = c + i;
82+
83+
int temp = board[top][left];
84+
85+
for (int row = top; row < bottom; row++) board[row][left] = board[row + 1][left];
86+
for (int col = left; col < right; col++) board[bottom][col] = board[bottom][col + 1];
87+
for (int row = bottom; row > top; row--) board[row][right] = board[row - 1][right];
88+
for (int col = right; col > left + 1; col--) board[top][col] = board[top][col - 1];
89+
90+
board[top][left + 1] = temp;
91+
}
92+
}
93+
94+
static int[][] copy() {
95+
int[][] res = new int[N + 1][M + 1];
96+
for (int i = 1; i <= N; i++) res[i] = originalBoard[i].clone();
97+
return res;
98+
}
99+
100+
static void calculateMin(int[][] board) {
101+
for (int i = 1; i <= N; i++) {
102+
int sum = 0;
103+
for (int j = 1; j <= M; j++) sum += board[i][j];
104+
minVal = Math.min(minVal, sum);
105+
}
106+
}
107+
}
108+
```

0 commit comments

Comments
 (0)