체스판 다시 칠하기(1018)
체스판을 만드는 모든 경우를 시도하여 최적의 방법을 찾는 문제
문제
입력
출력
예제 입력
8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW예제 출력
예제 입력2
예제 출력2
풀이
Last updated
체스판을 만드는 모든 경우를 시도하여 최적의 방법을 찾는 문제
8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBWLast updated
110 13
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
WWWWWWWWWWBWB
WWWWWWWWWWBWB12import java.util.*;
public class Test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
//8x8 기준으로 다 바꿀때(최대값)은 64
int count=0, min = 64;
String arr[][] = new String[N][M];
for(int i=0; i<N; i++){
String[] tmp = sc.next().split("");
for(int j=0; j<M; j++){
arr[i][j] = tmp[j];
}
}
for(int i=0; i<N-7; i++){
for(int j=0; j<M-7; j++){
count = cc(arr, i, j);
if(min > count){
min = count;
}
}
}
System.out.println(min);
}
public static int cc(String arr[][], int x, int y){
int c1 = 0;
int c2 = 0;
//18번도는거 확인
//System.out.println("Starts With B");
for(int i=x; i<x+8; i++){
for(int j=y; j<y+8; j++){
if((i+j)%2==0) {
//이거 더해서.... 좋은꿀팁
//제대로된 판을 그려보면 arr[i][j]에 B가 있어야함
//근데 B가 아니라 W가 있다면 바꿔줘야하니까 ++
if (arr[i][j].equals("W") == true){
//System.out.println(arr[i][j]+" is W");
c1++;
}
}
else if((i+j)%2==1) {
//여기에는 arr[i][j]에 W가 있어야하지만
//B가 있다면 바꿔줘야하니까 ++
if (arr[i][j].equals("B") == true){
//System.out.println(arr[i][j]+" is B");
c1++;
}
}
}
}
for(int i=x; i<x+8; i++){
for(int j=y; j<y+8; j++){
if((i+j)%2==0) {
if (arr[i][j].equals("B") == true)
c2++;
}
else if((i+j)%2==1) {
if (arr[i][j].equals("W") == true)
c2++;
}
}
}
if(c1 > c2)
return c2;
else
return c1;
}
}