알고리즘/일반(단순구현)

[백준] 1018번 체스판 다시 칠하기 #Java

VIPeveloper 2022. 5. 6. 23:31
728x90
반응형

 

import java.util.*;

public class Main {

    // 1. 두개의 체스판
    static char [][] board = {
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray()};
    static char [][] board2 = {
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray(),
            "BWBWBWBW".toCharArray(),
            "WBWBWBWB".toCharArray()};

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int N = kb.nextInt();
        int c = kb.nextInt();

        char[][] arr = new char[N][c];
        for (int i = 0; i < N; i++) {
            arr[i] = kb.next().toCharArray();
        }

        solution(arr,N,c);

    }

    private static void solution(char[][] arr, int x, int y) {
        // 2. 8*8의 체스판임으로 첫번째 좌표가 시작될 곳을 지정한다.
        int lenX = x-8+1;
        int lenY = y-8+1;
        int answer = Integer.MAX_VALUE;
        for (int i = 0; i < lenX; i++) {
            for (int j = 0; j < lenY; j++) {
                int i1 = find(arr, i, j);
                answer = Math.min(answer,i1);
            }
        }
        System.out.println(answer);
    }

    private static int find(char[][] arr, int x, int y) {
        int tmp1=0,tmp2=0;
        tmp1 = getTmp2(arr, x, y, tmp1, board);
        tmp2 = getTmp2(arr, x, y, tmp2, board2);
        return Math.min(tmp1,tmp2);
    }

    private static int getTmp2(char[][] arr, int x, int y, int tmp, char[][] board) {
        // 3. 지정된 좌표로부터 8칸
        for (int i = x; i < x+8; i++) {
            for (int j = y; j < y+8; j++) {
                if(arr[i][j] != board[i-x][j-y]){
                    tmp++;
                }
            }
        }
        return tmp;
    }
}
728x90
반응형