백준1018: 체스판 다시 칠하기
문제
https://www.acmicpc.net/problem/1018
문제 풀이
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
import java.util.*;
public class Main {
public static boolean[][] arr;
public static int min = 64;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
arr = new boolean[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
for (int j = 0; j < m; j++) {
if (s.charAt(j) == 'W') {
arr[i][j] = true;
}
}
}
//경우의 수 찾기
//경우의 수는 (가로 칸 개수 - 7) × (세로 칸 개수 - 7) 이다.
//최소 크기가 8×8 일 때 경우의 수가 1이기 때문에 각 가로 세로별 길이에 -7 을 해주는 것이다.
for (int i = 0; i < n - 7; i++) {
for (int j = 0; j < m - 7; j++) {
find(i, j);
}
}
System.out.println(min);
}
public static void find(int x, int y) {
int cnt = 0;
boolean firstBlock = arr[x][y];
for (int i = x; i < x + 8; i++) {
for (int j = y; j < y + 8; j++) {
if (arr[i][j] != firstBlock) {
cnt++;
}
firstBlock = !firstBlock;
}
firstBlock = !firstBlock;
}
cnt = Math.min(cnt, 64 - cnt);
min = Math.min(min, cnt);
}
}
댓글남기기