#include<iostream> usingnamespacestd; #define MAXSIZE 51 #define CHESSSIZE 8 int N,M,result=64;//8x8이므로 아무리 색을 다시칠해도 64의 결과가 나올 수 없음 char arr[MAXSIZE][MAXSIZE];
intcheck(int n,int m){ //8x8에서 인접한 위치와 충돌이 일어나는 수 int result1=0, result2=0; for(int i=0;i<CHESSSIZE;i++){ for(int j=0;j<CHESSSIZE;j++){ if(i%2==j%2){//0,0 || 1,1 if(arr[n+i][m+j]!='W') result1++; if(arr[n+i][m+j]!='B') result2++; }else{//0,1 || 1,0 if(arr[n+i][m+j]!='B') result1++; if(arr[n+i][m+j]!='W') result2++; } } } return (result1<result2)?result1:result2; }
intmain(){ scanf("%d %d",&N,&M); for(int i=0;i<N;i++) scanf("%s",&arr[i]); //풀이 1: 가장 간단한 방법, N-8, M-8의 경우의 수를 전부 조사하여 최소값 구하기 for(int i=0;i<=N-8;i++){//시작점 세팅 for(int j=0;j<=M-8;j++){ int temp = check(i,j); result = (result>temp)?temp:result; } } printf("%d\n",result); }