728x90
반응형
https://www.acmicpc.net/problem/1018
1018번: 체스판 다시 칠하기
첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.
www.acmicpc.net
public static void main(String[] args) throws IOException {
String input;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
StringTokenizer st = new StringTokenizer(input);
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new char[N][M];
for (int i = 0; i < N; i++) {
map[i] = br.readLine().toCharArray();
}
for (int i = 0; i <= N - 8; i++) {
for (int j = 0; j <= M - 8; j++) {
//첫번쨰 B, W 중 더 적은 경우
min = Math.min(black(i, j), white(i, j));
if (result > min)
result = min;
}
}
System.out.println(result);
}
체스판을 입력하고 모든 8X8의 경우를 확인한다.
확인할 때 첫번째 칸을 W로 하는 경우, B로 하는 경우 2가지로 나뉘어 경우를 계산한다.
public static int black(int a, int b) {
int cnt = 0;
for (int i = a; i < a + 8; i++) {
for (int j = b; j < b + 8; j++) {
if (i % 2 == 0) { // 홀수 줄
if (j % 2 == 0) {
if (map[i][j] != 'B')
cnt++;
} else {
if (map[i][j] != 'W')
cnt++;
}
} else { // 짝수 줄
if (j % 2 == 0) {
if (map[i][j] != 'W')
cnt++;
} else {
if (map[i][j] != 'B')
cnt++;
}
}
}
}
return cnt;
}
첫번째 칸을 B로 맞추는 경우이다. 홀수 줄, 짝수 줄로 나누고 그 줄에서 홀수 칸 짝수 칸으로 나누어 확인한다.
바꿔야 하는 경우의 수를 구한다.
public static int white(int a, int b) {
int cnt = 0;
for (int i = a; i < a + 8; i++) {
for (int j = b; j < b + 8; j++) {
if (i % 2 == 0) { // 홀수 줄
if (j % 2 == 0) {
if (map[i][j] != 'W')
cnt++;
} else {
if (map[i][j] != 'B')
cnt++;
}
} else { // 짝수 줄
if (j % 2 == 0) {
if (map[i][j] != 'B')
cnt++;
} else {
if (map[i][j] != 'W')
cnt++;
}
}
}
}
return cnt;
}
첫번째 칸을 W로 맞추는 경우이다. B일 경우와 칸만 반대이다.
전체코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, M, min, result = Integer.MAX_VALUE;
static char[][] map;
public static void main(String[] args) throws IOException {
String input;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input = br.readLine();
StringTokenizer st = new StringTokenizer(input);
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
map = new char[N][M];
for (int i = 0; i < N; i++) {
map[i] = br.readLine().toCharArray();
}
for (int i = 0; i <= N - 8; i++) {
for (int j = 0; j <= M - 8; j++) {
//첫번쨰 B, W 중 더 적은 경우
min = Math.min(black(i, j), white(i, j));
if (result > min)
result = min;
}
}
System.out.println(result);
}
public static int black(int a, int b) {
int cnt = 0;
for (int i = a; i < a + 8; i++) {
for (int j = b; j < b + 8; j++) {
if (i % 2 == 0) { // 홀수 줄
if (j % 2 == 0) {
if (map[i][j] != 'B')
cnt++;
} else {
if (map[i][j] != 'W')
cnt++;
}
} else { // 짝수 줄
if (j % 2 == 0) {
if (map[i][j] != 'W')
cnt++;
} else {
if (map[i][j] != 'B')
cnt++;
}
}
}
}
return cnt;
}
public static int white(int a, int b) {
int cnt = 0;
for (int i = a; i < a + 8; i++) {
for (int j = b; j < b + 8; j++) {
if (i % 2 == 0) { // 홀수 줄
if (j % 2 == 0) {
if (map[i][j] != 'W')
cnt++;
} else {
if (map[i][j] != 'B')
cnt++;
}
} else { // 짝수 줄
if (j % 2 == 0) {
if (map[i][j] != 'B')
cnt++;
} else {
if (map[i][j] != 'W')
cnt++;
}
}
}
}
return cnt;
}
}
728x90
반응형
'코테 > JAVA' 카테고리의 다른 글
[백준 4796 / JAVA] 캠핑 (0) | 2024.01.16 |
---|---|
[백준 1182 / JAVA] 부분수열의 합 (0) | 2024.01.16 |
[백준 2503 / JAVA] 숫자 야구 (1) | 2024.01.15 |
[백준 10448 / JAVA] 유레카 이론 (1) | 2024.01.15 |
[백준 3085 / JAVA] 사탕 게임 (1) | 2024.01.15 |