문제
브루트포스를 통해 이문제를 해결하면 된다.
먼저, 이렇게 W와 B로 시작하는 체스판을 만들어놓고
string w_start[8] = {
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW"
};
string b_start[8] = {
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB"
};
W와 B로 시작하는 것 중 어떤 것이 더 유리한지 count를 센다.
int w_check(int col, int row){
int count = 0;
int w_col = 0;
int w_row = 0;
for(register int i = col; i < col + 8; i++){
w_row = 0;
for(register int j = row; j < row + 8; j++){
if(input[i][j] != w_start[w_col][w_row++]) count++;
}
w_col++;
}
return count;
}
int b_check(int col, int row){
int count = 0;
int b_col = 0;
int b_row = 0;
for(register int i = col; i < col + 8; i++){
b_row = 0;
for(register int j = row; j < row + 8; j++){
if(input[i][j] != b_start[b_col][b_row++]) count++;
}
b_col++;
}
return count;
}
이렇게 두가지 경우를 하나하나 비교해보며 답을 도출하면 된다.
for(register int i = 0; i < N - 7; i++){
for(register int j = 0; j < M - 7; j++){
int temp = min(w_check(i, j), b_check(i, j));
if(result > temp) result = temp;
}
}
#include <bits/stdc++.h>
using namespace std;
char input[51][51];
int N, M;
string w_start[8] = {
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW"
};
string b_start[8] = {
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB"
};
int w_check(int col, int row){
int count = 0;
int w_col = 0;
int w_row = 0;
for(register int i = col; i < col + 8; i++){
w_row = 0;
for(register int j = row; j < row + 8; j++){
if(input[i][j] != w_start[w_col][w_row++]) count++;
}
w_col++;
}
return count;
}
int b_check(int col, int row){
int count = 0;
int b_col = 0;
int b_row = 0;
for(register int i = col; i < col + 8; i++){
b_row = 0;
for(register int j = row; j < row + 8; j++){
if(input[i][j] != b_start[b_col][b_row++]) count++;
}
b_col++;
}
return count;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for(register int i = 0; i < N; i++){
for(register int j = 0; j < M; j++){
cin >> input[i][j];
}
}
int result = 70;
for(register int i = 0; i < N - 7; i++){
for(register int j = 0; j < M - 7; j++){
int temp = min(w_check(i, j), b_check(i, j));
if(result > temp) result = temp;
}
}
cout << result << '\n';
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 1269번 (대칭 차집합) c++ (0) | 2023.04.14 |
---|---|
백준 1065번(한수) c++ (0) | 2023.04.09 |
백준 11866번 (요세푸스 문제 0) c++ (1) | 2023.02.16 |
백준 11723번 (집합) c++ (0) | 2023.02.15 |
백준 9655번 (돌 게임) c++ (0) | 2022.12.27 |
댓글