• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • SWEA
    • LeetCode
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[BOJ] 1018. 체스판 다시 칠하기

15 Apr 2026

Reading time ~1 minute

  • 풀이

풀이

import java.util.Scanner;

public class Main {
	static Scanner sc=new Scanner(System.in);
	static char map[][];
	static char panel[][][];
	static int cnt,ans;
	public static void main(String[] args) {
		int N=sc.nextInt();
		int M=sc.nextInt();
		map = new char [N][M];
		panel=new char [2][8][8];
		ans=Integer.MAX_VALUE;
		for (int i = 0; i < N; i++) {
			String s=sc.next();
			for (int j = 0; j < M; j++) {
				map[i][j]=s.charAt(j);
			}
		}
		
		for (int p = 0; p < 2; p++) {
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if((p+8*i+j+i)%2==0) {
						panel[p][i][j] = 'B';
					}else {
						panel[p][i][j]= 'W';
					}
				}
			}			
		}
				
		for (int i = 0; i <= N-8; i++) {
			for (int j = 0; j <= M-8; j++) {
				for (int k = 0; k < 2; k++) {
					cnt=cmp(i,j,k);
					if(cnt<ans) {
						ans=cnt;
					}					
				}
			}
		}
		
		System.out.println(ans);
	}

	public static int cmp(int x,int y,int k) {
		cnt=0;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if(map[x+i][y+j]!=panel[k][i][j]) {
					cnt++;
				}
			}
		}
		return cnt;
	}
}


구현브루트포스 Share