1. 문제 링크
1018번: 체스판 다시 칠하기
첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.
www.acmicpc.net
2. 문제 및 입출력예제
3. 문제 풀이
W인 경우를 true, B인 경우를 false로 배열을 입력 받는다.
8x8 배열 안에서 처음 값을 저장하고
반복문 돌면서 비교해서 같은 값일 때면 다시 칠해야 하니까 count를 1 더한다.
다음 값으로 비교하기 전에 boolean으로 만든 변수를 바꾸고 돌린다.
4. 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static boolean[][] map;
static int N, M, result;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String[] split = br.readLine().split(" ");
N = Integer.parseInt(split[0]);
M = Integer.parseInt(split[1]);
map = new boolean[N][M];
for(int i=0;i<N;i++) {
String str = br.readLine();
for(int j=0;j<M;j++) {
if(str.charAt(j)=='W') {
map[i][j] = true;
}
else {
map[i][j] = false;
}
}
}
result = Integer.MAX_VALUE;
for(int i=0;i<N-7;i++) {
for(int j=0;j<M-7;j++) {
check(i, j);
}
}
System.out.println(result);
}
private static void check(int x, int y) {
boolean first = map[x][y];
int count = 0;
for(int i=x;i<x+8;i++) {
for(int j=y;j<y+8;j++) {
if(map[i][j] == first) {
count++;
}
first = !first;
}
first = !first;
}
count = Math.min(count, 64-count);
result = Math.min(result, count);
}
}
'ALGORITHM' 카테고리의 다른 글
백준 17144 미세먼지 안녕! (Java) (0) | 2023.03.23 |
---|---|
백준 15683 감시 (Java) (0) | 2023.03.22 |
백준 1495 기타리스트 (Java) (0) | 2023.03.21 |
백준 10026 적록색약 (Java) (0) | 2023.03.20 |
백준 4153 직각 삼각형(Java) (0) | 2023.03.20 |