https://www.acmicpc.net/problem/1018
import java.util.*;
import java.io.*;
public class Main
{
public static int calcStartBlack(String[][] map){
int count=0;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(i%2==0){
if(j%2==0){
if(map[i][j].equals("B")) count++;
}else{
if(map[i][j].equals("W")) count++;
}
}else{
if(j%2==0){
if(map[i][j].equals("W")) count++;
}else{
if(map[i][j].equals("B")) count++;
}
}
}
}
return count;
}
public static int calcStartWhite(String[][] map){
int count=0;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
if(i%2==0){
if(j%2==0){
if(map[i][j].equals("W")) count++;
}else{
if(map[i][j].equals("B")) count++;
}
}else{
if(j%2==0){
if(map[i][j].equals("B")) count++;
}else{
if(map[i][j].equals("W")) count++;
}
}
}
}
return count;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
String[] input = br.readLine().split(" ");
int N = Integer.valueOf(input[0]);
int M = Integer.valueOf(input[1]);
String[][] map = new String[N][M];
for(int i=0;i<N;i++){
map[i] = br.readLine().split("");
}
int minCount=2501;
for(int i =0;i<=N-8;i++){
for(int j=0;j<=M-8;j++){
String[][] chessMap = new String[8][8];
for(int k=0;k<8;k++){
for(int l=0;l<8;l++){
chessMap[k][l]=map[i+k][j+l];
}
}
int tempCount=Math.min(calcStartWhite(chessMap),calcStartBlack(chessMap));
if(minCount>tempCount) minCount=tempCount;
}
}
System.out.println(minCount);
}
}
import java.util.Scanner;
class Main
{
private static int countDifferent(char[][] map, int starRow, int startColumn, String pattern) {
int cnt = 0;
for (int i = starRow; i < starRow + 8; i++)
for (int j = startColumn; j < startColumn + 8; j++)
if (map[i][j] != pattern.charAt((i + j) % 2))
cnt++;
return cnt;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
char[][] map = new char[N][M];
for(int i = 0; i < N; i++) {
String line = sc.next();
map[i] = line.toCharArray();
}
int ans = N * M;
for (int i = 0; i <= N - 8; i++)
for (int j = 0; j <= M - 8; j++) {
int countBW = countDifferent(map, i, j, "BW");
int countWB = countDifferent(map, i, j,"WB");
ans = Math.min(ans, Math.min(countBW, countWB));
}
System.out.println(ans);
}
}
'Problem Solving > 구현' 카테고리의 다른 글
BOJ14891 톱니바퀴 (0) | 2024.04.12 |
---|---|
BOJ16236 아기상어 (0) | 2024.04.12 |
BOJ1120 문자열 (2) | 2024.02.01 |
BOJ4673 셀프넘버 (0) | 2024.02.01 |
BOJ1110 더하기 사이클 (0) | 2024.02.01 |