Skip to content

Commit 0e9073a

Browse files
authored
[20250920] BOJ / G4 / 체스판 다시 칠하기 2 / 김민진
[20250920] BOJ / G4 / 체스판 다시 칠하기 2 / 김민진
2 parents da21aaf + e6095d8 commit 0e9073a

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class BJ_25682_체스판_다시_칠하기_2 {
6+
7+
private static final boolean BLACK = true;
8+
private static final boolean WHITE = false;
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
12+
private static StringTokenizer st;
13+
14+
private static int N, M, K;
15+
16+
private static boolean[][] board;
17+
private static int[][] prefix;
18+
19+
public static void main(String[] args) throws IOException {
20+
init();
21+
sol();
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+
30+
board = new boolean[N + 1][M + 1];
31+
prefix = new int[N + 1][M + 1];
32+
33+
for (int i = 1; i <= N; i++) {
34+
String s = br.readLine();
35+
for (int j = 1; j <= M; j++) {
36+
board[i][j] = s.charAt(j - 1) == 'B' ? BLACK : WHITE;
37+
}
38+
}
39+
}
40+
41+
private static void sol() throws IOException {
42+
bw.write(Math.min(setPrefix(BLACK), setPrefix(WHITE)) + "\n");
43+
bw.flush();
44+
bw.close();
45+
br.close();
46+
}
47+
48+
// 열 + 행이 짝수일 때 color라고 가정
49+
private static int setPrefix(boolean color) {
50+
int val;
51+
52+
for (int i = 1; i <= N; i++) {
53+
for (int j = 1; j <= M; j++) {
54+
if ((i + j) % 2 == 0) {
55+
val = board[i][j] != color ? 1 : 0;
56+
} else {
57+
val = board[i][j] == color ? 1 : 0;
58+
}
59+
prefix[i][j] = prefix[i][j - 1] + prefix[i - 1][j] - prefix[i - 1][j - 1] + val;
60+
}
61+
}
62+
return getMinPrefix();
63+
}
64+
65+
private static int getMinPrefix() {
66+
int cnt = Integer.MAX_VALUE;
67+
68+
for (int i = K; i <= N; i++) {
69+
for (int j = K; j <= M; j++) {
70+
cnt = Math.min(cnt, prefix[i][j] - prefix[i - K][j] - prefix[i][j - K] + prefix[i - K][j - K]);
71+
}
72+
}
73+
return cnt;
74+
}
75+
76+
}
77+
```
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```java
2+
import java.io.*;
3+
4+
public class BJ_6443_애너그램 {
5+
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
9+
private static String input;
10+
private static int length;
11+
private static int[] alphabets;
12+
private static char[] result;
13+
14+
public static void main(String[] args) throws IOException {
15+
int T = Integer.parseInt(br.readLine());
16+
17+
while (T-- > 0) {
18+
sol();
19+
}
20+
bw.flush();
21+
bw.close();
22+
br.close();
23+
}
24+
25+
private static void sol() throws IOException {
26+
input = br.readLine();
27+
length = input.length();
28+
alphabets = new int[26];
29+
result = new char[length];
30+
31+
for (int i = 0; i < length; i++) {
32+
alphabets[input.charAt(i) - 'a']++;
33+
}
34+
35+
perm(0);
36+
}
37+
38+
private static void perm(int depth) throws IOException {
39+
if (depth == length) {
40+
bw.write(result);
41+
bw.write('\n');
42+
return;
43+
}
44+
45+
for (int i = 0; i < 26; i++) {
46+
if (alphabets[i] > 0) {
47+
alphabets[i]--;
48+
result[depth] = (char) (i + 'a');
49+
perm(depth + 1);
50+
alphabets[i]++;
51+
}
52+
}
53+
}
54+
}
55+
```

0 commit comments

Comments
 (0)