[백준] 1018 체스판 다시 칠하기 자바

ChoRong0824·2023년 7월 18일
1

백준

목록 보기
4/14

문제출처: https://www.acmicpc.net/problem/1018

접근

문제만 잘 읽어본다면, 손쉽게 이해할 수 있는 문제였습니다.
문제에 체스판을 색칠하는 경우는 총 2가지.--> 맨왼쪽위 흰색 || 검은색
즉, 체스판을 만들기 위해서는 한 칸 기준으로 상하좌우의 색이 다르면 된다.
또한, 체스판이 잘못 칠해질 경우도 생각해야한다. --> 최소 개수로 칠할 수 있는 부분을 생각하면된다.

경우의수

(가로칸 개수-7) x (세로칸개수 -7)이다.
--> 이유 : 최소크기가 8x8일 때, 경우의 수가 1이기 때문에 각변에는 -7을 해줘야한다.
따라서, 앞서 말했듯이 체스판을 색칠하는 경우는 총 2가지였다.
따라서 2를 곱해주면 끝이다. --> 이것은 2를 곱해서 사용하거나 반복문을 2번 돌리면 해결되는 것이다.

코드

필자가 처음 접근한 방법은 dfs다.

--> 이유? -->없다.
필자는 요즘 dfs에 빠졌기 때문에, 모든 문제를 일단 dfs로 구현하고 본다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    static String[] board;
    static int min = Integer.MAX_VALUE;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int m = Integer.parseInt(input[1]);
        board = new String[n];

        for (int i = 0; i < n; i++) {
            board[i] = br.readLine();
        }

        dfs(0, 0, n, m);

        System.out.println(min);
    }

    public static void dfs(int x, int y, int n, int m) {
        if (x + 7 < n && y + 7 < m) {
            calculateMinChanges(x, y);
            dfs(x + 1, y, n, m);
            dfs(x, y + 1, n, m);
        }
    }

    public static void calculateMinChanges(int startX, int startY) {
        int cnt1 = 0;
        int cnt2 = 0;
        char[] colors = {'B', 'W'};
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (board[startX + i].charAt(startY + j) != colors[(i + j) % 2]) {
                    cnt1++;
                } else {
                    cnt2++;
                }
            }
        }
        min = Math.min(min, Math.min(cnt1, cnt2));
    }
}

기본

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int n = Integer.parseInt(input[0]);
        int m = Integer.parseInt(input[1]);
        int[][] board = new int[n][m];

        for (int i = 0; i < n; i++) {
            String line = br.readLine();
            for (int j = 0; j < m; j++) {
                board[i][j] = line.charAt(j);
            }
        }

        int min = Integer.MAX_VALUE;

        for (int i = 0; i < n - 7; i++) {
            for (int j = 0; j < m - 7; j++) {
                int cnt1 = 0;
                int cnt2 = 0;
                for (int k = 0; k < 8; k++) {
                    for (int l = 0; l < 8; l++) {
                        if ((k + l) % 2 == 0) { 
                            if (board[i + k][j + l] == 'B') cnt1++;
                            else cnt2++;
                        } else { 
                            if (board[i + k][j + l] == 'W') cnt1++;
                            else cnt2++;
                        }
                    }
                }
                min = Math.min(min, Math.min(cnt1, cnt2));
            }
        }

        System.out.println(min);
    }
}
profile
백엔드를 지향하며, 컴퓨터공학과를 졸업한 취준생입니다. 많이 부족하지만 열심히 노력해서 실력을 갈고 닦겠습니다. 부족하고 틀린 부분이 있을 수도 있지만 이쁘게 봐주시면 감사하겠습니다. 틀린 부분은 댓글 남겨주시면 제가 따로 학습 및 자료를 찾아봐서 제 것으로 만들도록 하겠습니다. 귀중한 시간 방문해주셔서 감사합니다.

3개의 댓글

comment-user-thumbnail
2023년 7월 18일

..

답글 달기
comment-user-thumbnail
2023년 7월 18일

소중한 정보 감사드립니다!

1개의 답글