๋ฌธ์
ํด๋น ํฌ์คํ ์ ๋ฐฑ์ค์ ์ฒด์คํ ๋ค์ ์น ํ๊ธฐ ์ ์ ๊ทผ๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๊ธ ์ ๋๋ค.
์ ๋ต ์์ค ์ฝ๋๋ฅผ ํ์ธํ์๋ ค๋ฉด solve url ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋ค ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋์ง๋ฅผ ๋จผ์ ์๊ฐํด๋ณด์.
๋ฌธ์ ์ ๊ทผ
์ด๋ฒ ๋ฌธ์ ๋ Brute Force ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ ์ด๋ค.
์ฒด์คํ์ 2๊ฐ์ง์ ์ข ๋ฅ๊ฐ ์กด์ฌํ๋ค.
- B๊ฐ ๋จผ์ ์ค๋ ๊ฒฝ์ฐ
- W๊ฐ ๋จผ์ ์ค๋ ๊ฒฝ์ฐ
์ด ๋ ๊ฒฝ์ฐ์์ ์๋กญ๊ฒ ์น ํด์ผ ํ๋ ํ์ ์ต์๊ฐ์ ์ถ๋ ฅํ๋ฉด ์ ๋ต์ด ๋๋ค.
ํด๊ฒฐ๋ฒ
๋๋ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ W๋ก ์์ํ๋ ๊ฒฝ์ฐ์ B๋ก ์์ํ๋ ๊ฒฝ์ฐ์ ๋ฐฐ์ด์ ๋ง๋ค์๋ค.
์ฒด์คํ ์ ์ฒด๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ ํ๋์ฉ ๋น๊ตํด๊ฐ๋ฉฐ ๋ค๋ฅธ ๊ฒฝ์ฐ count ๋ฅผ ์ฆ๊ฐ์ํค๋ฉด ๋๋ค.
String[][] bw = {
{ "BWBWBWBW" },
{ "WBWBWBWB" },
{ "BWBWBWBW" },
{ "WBWBWBWB" },
{ "BWBWBWBW" },
{ "WBWBWBWB" },
{ "BWBWBWBW" },
{ "WBWBWBWB" }
};
String[][] wb = {
{ "WBWBWBWB" },
{ "BWBWBWBW" },
{ "WBWBWBWB" },
{ "BWBWBWBW" },
{ "WBWBWBWB" },
{ "BWBWBWBW" },
{ "WBWBWBWB" },
{ "BWBWBWBW" }
};
ํ์ง๋ง ์ด์ฐจํผ WB๊ฐ ๋ฐ๋ณต๋ ๊ฒ์ ์๊ธฐ ๋๋ฌธ์ 1์ฐจ์ ๋ฐฐ์ด๋ก ํด๊ฒฐํ ์ ์๋ค.
char[] bw = {'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'};
char[] wb = {'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'};
์ ๋ต ์ฝ๋
public class Main {
static int n;
static int m;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] nm = br.readLine().split(" ");
n = Integer.parseInt(nm[0]);
m = Integer.parseInt(nm[1]);
char[][] arr = new char[n][m];
for (int i = 0; i < n; i++) {
char[] line = br.readLine().toCharArray();
for (int j = 0; j < m; j++) {
arr[i][j] = line[j];
}
}
int answer = 32;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
answer = Math.min(answer, countRepaintNum(arr, j, i));
}
}
bw.write(String.valueOf(answer));
bw.flush();
bw.close();
}
private static int countRepaintNum(char[][] arr, int x, int y) {
int count = 0;
int xx = x + 7;
int yy = y + 7;
char[] bw = {'B', 'W', 'B', 'W', 'B', 'W', 'B', 'W'};
char[] wb = {'W', 'B', 'W', 'B', 'W', 'B', 'W', 'B'};
int jIndex;
if(isValidPoint(xx, yy)) {
for (int i = y; i <= yy; i++) {
jIndex = 0;
for (int j = x; j <= xx; j++) {
if(i % 2 != 0) {
if(arr[i][j] != bw[jIndex++]) count++;
}else {
if(arr[i][j] != wb[jIndex++]) count++;
}
}
}
int temp = count;
count = 0;
for (int i = y; i <= yy; i++) {
jIndex = 0;
for (int j = x; j <= xx; j++) {
if(i % 2 != 0) {
if(arr[i][j] != wb[jIndex++]) count++;
}else {
if(arr[i][j] != bw[jIndex++]) count++;
}
}
}
return Math.min(count, temp);
}else return 100;
}
private static boolean isValidPoint(int x, int y) {
return (0 <= x && 0 <= y && x < m && y < n);
}
}
๋ฌธ์ ํ๊ณ
ํ๊ณ
๋๊ธ