문제 출처
https://www.acmicpc.net/problem/1018
※ 주의할 점
아래와 같은 경우 맨 처음칸만 바꾸면 되는데 잘못하면 63번을 바꿔버리는 경우가 있다
8
BBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
※ 소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
//input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int Y = Integer.parseInt(st.nextToken());
int X = Integer.parseInt(st.nextToken());
//init
char[][] map = new char[Y][X];
for (int i = 0; i < map.length; i++) {
String word = br.readLine();
for (int j = 0; j < map[i].length; j++) {
map[i][j] = word.charAt(j);
}
}
//do
int count = 64;
for (int i = 0; i < Y - 7; i++) {
for (int j = 0; j < X - 7; j++) {
int compare = find(map, j, i, X, Y);
count = Math.min(count, compare);
}
}
System.out.println(count);
}
public static int find(char[][] map, int x, int y, int X, int Y) {
int count = 0;
//count
char next = map[y][x];
count = getDrawCount(map, x, y, next, count);
int compare = 64 - count;
count = Math.min(count, compare);
return count;
}
private static int getDrawCount(char[][] map, int x, int y, char next, int count) {
for (int i = y; i < y + 8; i++) {
for (int j = x; j < x + 8; j++) {
if (next != map[i][j]) {
count++;
}
next = reverse(next);
}
next = reverse(next);
}
return count;
}
public static char reverse(char c) {
return (c == 'B') ? 'W' : 'B';
}
}
* 반례
8
BBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
답 : 1
10 10
WWBBWWWBBW
WBBWBWWWWB
WBWBWWBBWW
WBBBBBBBWW
WBBWWWBWWW
WBBBBBWWBB
WWBWWBWWBB
BWWBBWWWBB
BBWBBBBBWB
WWWBBBWWWB
답 : 29
8 16
BBBBBBBBBWBWBWBW
BBBBBBBBWBWBWBWB
BBBBBBBBBWBWBWBW
BBBBBBBBWBWBWBWB
BBBBBBBBBWBWBWBW
BBBBBBBBWBWBWBWB
BBBBBBBBBWBWBWBW
BBBBBBBBWBWBWBWB
답 : 0
8 8
BBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
답 : 2
8 8
WWWWWWWB
WBBBBBBB
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
답 : 8
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 1085번 직사각형에서 탈출 - Java (0) | 2021.03.04 |
---|---|
[백준] 1181번 단어정렬(반례) - Java (0) | 2021.03.04 |
[백준] 1436번 영화감독 숌 - Java (0) | 2021.03.04 |
[백준] 1157번 단어 공부 - Java (0) | 2021.03.03 |
[백준] 2908번 상수 - Java (0) | 2021.03.03 |