https://www.acmicpc.net/problem/1018
이것 또한 완전 탐색!!
단 문제를 제대로 읽어보면 전체 map은 항상 가로, 세로 8 이상이라는 조건이 있는데
제대로 읽지 않고 8보다 작을 때 생길 오류에 대해 고민하느라고 아주 많은 시간을 낭비하였다.
또한 흰-검/ 검-흰 이 규칙을 두개를 다 적용해봐야 어떤것이 더 작은 답을 내놓는지
알 수 있을 것이라 생각했는데, 한참 후에 다른 포스팅을 보고 나서야 이 규칙을 깨닫게 되었다.
오랜만의 알고리즘이라 그런지 감을 영 잡지 못해 실버 4 수준에서
한참 해매어 꾸준함의 중요성을 다시 깨닫고 매일 1솔을 다시금 다짐하였다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Q1018 {
static int n, m;
static String[][] map;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(reader.readLine()) ;
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
map = new String[n][m];
for(int i = 0; i < n; i++){
map[i] = reader.readLine().split("");
}
int answer = Integer.MAX_VALUE;
int count = 0;
for(int i = 8; i <= n; i++) {
for (int j = 8; j <= m; j++) {
count = check(i-8, i, j-8, j);
answer = Integer.min(answer, count);
}
}
System.out.println(answer);
}
public static int check(int startRaw, int endRaw, int startCol, int endCol){
int count = 0;
for(int i = startRaw; i < endRaw; i++) {
for (int j = startCol; j < endCol; j++) {
if(i % 2 == 0) {
if (j % 2 == 0 && map[i][j].equals("B")) {
count++;
continue;
}
if (j % 2 == 1 && map[i][j].equals("W")) {
count++;
}
}else{
if (j % 2 == 0 && map[i][j].equals("W")) {
count++;
continue;
}
if (j % 2 == 1 && map[i][j].equals("B")) {
count++;
}
}
}
}
if (count > 32){
count = 64 - count;
}
return count;
}
}
'알고리즘 > java' 카테고리의 다른 글
<java> 백준 15829 - BigInteger (0) | 2024.05.21 |
---|---|
<java> 프로그래머스 베스트 앨범 - Map과 정렬 (0) | 2024.05.20 |
<java> 백준 14889 - 완전탐색, DFS (0) | 2024.05.17 |
<java> 백준 1092- 그리디 (0) | 2024.03.21 |
<java> 백준 12904 - 그리디 (2) | 2024.03.19 |